summaryrefslogtreecommitdiff
path: root/test/test_resources.py
blob: 7e60dd13c713054bd6ce934cf51e8c903c9ec230 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import bottle
from tools import ServerTestBase
from bottle import ResourceManager
import os.path
import unittest

class TestResouceManager(unittest.TestCase):

    def test_path_normalize(self):
        tests = ('/foo/bar/', '/foo/bar/baz', '/foo/baz/../bar/blub')
        for test in tests:
            rm = ResourceManager()
            rm.add_path(test)
            self.assertEqual(rm.path, ['/foo/bar/'])

    def test_path_create(self):
        import tempfile, shutil
        tempdir = tempfile.mkdtemp()
        try:
            rm = ResourceManager()
            exists = rm.add_path('./test/', base=tempdir)
            self.assertEqual(exists, False)
            exists = rm.add_path('./test2/', base=tempdir, create=True)
            self.assertEqual(exists, True)
        finally:
            shutil.rmtree(tempdir)

    def test_path_absolutize(self):
        tests = ('./foo/bar/', './foo/bar/baz', './foo/baz/../bar/blub')
        abspath = os.path.abspath('./foo/bar/') + os.sep
        for test in tests:
            rm = ResourceManager()
            rm.add_path(test)
            self.assertEqual(rm.path, [abspath])

        for test in tests:
            rm = ResourceManager()
            rm.add_path(test[2:])
            self.assertEqual(rm.path, [abspath])

    def test_path_unique(self):
        tests = ('/foo/bar/', '/foo/bar/baz', '/foo/baz/../bar/blub')
        rm = ResourceManager()
        [rm.add_path(test) for test in tests]
        self.assertEqual(rm.path, ['/foo/bar/'])

    def test_root_path(self):
        tests = ('/foo/bar/', '/foo/bar/baz', '/foo/baz/../bar/blub')
        for test in tests:
            rm = ResourceManager()
            rm.add_path('./baz/', test)
            self.assertEqual(rm.path, ['/foo/bar/baz/'])

        for test in tests:
            rm = ResourceManager()
            rm.add_path('baz/', test)
            self.assertEqual(rm.path, ['/foo/bar/baz/'])

    def test_path_order(self):
        rm = ResourceManager()
        rm.add_path('/middle/')
        rm.add_path('/first/', index=0)
        rm.add_path('/last/')
        self.assertEqual(rm.path, ['/first/', '/middle/', '/last/'])

    def test_get(self):
        rm = ResourceManager()
        rm.add_path('/first/')
        rm.add_path(__file__)
        rm.add_path('/last/')
        self.assertEqual(None, rm.lookup('notexist.txt'))
        self.assertEqual(__file__, rm.lookup(os.path.basename(__file__)))

    def test_open(self):
        rm = ResourceManager()
        rm.add_path(__file__)
        fp = rm.open(os.path.basename(__file__))
        self.assertEqual(fp.read(), open(__file__).read())


if __name__ == '__main__': #pragma: no cover
    unittest.main()