summaryrefslogtreecommitdiffstats
path: root/test.py
blob: 32c68c9399bb9823399b972d203682a97a9b3729 (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
from ffs import Router, Dict, RouterError
from tempfile import mkdtemp
import os
import shutil
from unittest import TestCase


class FfsTest(TestCase):
    def setUp(self):
        self.root = mkdtemp(prefix='ffs_test_root')
        os.makedirs(os.path.join(self.root, 'lol'))
        with open(os.path.join(self.root, 'lol', 'cat'), 'w') as f:
            f.write("hello")

    def tearDown(self):
        if self.root:
            shutil.rmtree(self.root)

    def test_simpleAccess1(self):
        rtr = Router(lol=Router(hop=str))
        lst1 = Dict(self.root, rtr)
        assert 'hop' not in lst1
        assert 'lol' in lst1
        lst2 = lst1['lol']
        assert 'hop' not in lst2

    def test_simpleAccess2(self):
        rtr = Router(lol=Router(cat=str))
        lst1 = Dict(self.root, rtr)
        assert 'cat' not in lst1
        assert 'lol' in lst1
        lst2 = lst1['lol']
        assert 'cat' in lst2
        assert "hello" == lst2['cat']

    def test_globAccess1(self):
        with open(os.path.join(self.root, 'lol', 'caaaaat'), 'w') as f:
            f.write("hello2")
        with open(os.path.join(self.root, 'lol', 'longcat'), 'w') as f:
            f.write("hello3")

        rtr = Router(lol=Router({'c*t': str}))
        lst1 = Dict(self.root, rtr)
        assert 'cat' not in lst1
        assert 'lol' in lst1
        lst2 = lst1['lol']
        assert 'cat' in lst2
        assert 'caaaaat' in lst2
        assert 'cot' not in lst2
        assert 'longcat' not in lst2
        assert "hello" == lst2['cat']
        assert "hello2" == lst2['caaaaat']
        self.assertRaises(KeyError, lst2.__getitem__, 'cot')
        self.assertRaises(RouterError, lst2.__getitem__, 'longcat')

    def test_finalValueDelete(self):
        rtr = Router(lol=Router(cat=str))
        lst1 = Dict(self.root, rtr)
        lst2 = lst1['lol']
        assert 'cat' in lst2
        del lst2['cat']
        assert 'cat' not in lst2

    def test_finalValueSet(self):
        rtr = Router(lol=Router(cat=str, cot=str))
        lst1 = Dict(self.root, rtr)
        lst2 = lst1['lol']
        assert 'cot' not in lst2
        lst2['cot'] = "hello2"
        assert 'cot' in lst2
        assert lst2['cot'] == "hello2"

    def test_listDelete(self):
        rtr = Router(lol=Router(cat=str, cot=str))
        lst1 = Dict(self.root, rtr)
        assert 'lol' in lst1
        del lst1['lol']
        assert 'lol' not in lst1