79
|
1 |
"""
|
|
2 |
Unit tests for qmsk.web.urltree
|
|
3 |
"""
|
|
4 |
|
|
5 |
import unittest
|
|
6 |
import urltree
|
|
7 |
|
|
8 |
class TestLabelValue (unittest.TestCase) :
|
|
9 |
class dummylabel :
|
|
10 |
key = 'foo'
|
|
11 |
|
|
12 |
def test_str_default (self) :
|
|
13 |
self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', True)), "foo")
|
|
14 |
|
|
15 |
def test_str_value (self) :
|
|
16 |
self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', False)), "foo='bar'")
|
|
17 |
|
|
18 |
class TestLabel (unittest.TestCase) :
|
|
19 |
def setUp (self) :
|
|
20 |
self.config = urltree.URLConfig()
|
|
21 |
|
|
22 |
def _test_parse (self, mask, _type, defaults={}, **attrs) :
|
|
23 |
label = urltree.Label.parse(mask, defaults, self.config)
|
|
24 |
|
|
25 |
self.assertTrue(isinstance(label, _type))
|
|
26 |
|
|
27 |
for k, v in attrs.iteritems() :
|
|
28 |
self.assertEqual(getattr(label, k), v)
|
|
29 |
|
|
30 |
def test_parse_empty (self) :
|
|
31 |
self._test_parse("", urltree.EmptyLabel)
|
|
32 |
|
|
33 |
def test_parse_static (self) :
|
|
34 |
self._test_parse("foo", urltree.StaticLabel, name="foo")
|
|
35 |
|
|
36 |
def test_parse_static_valid (self) :
|
|
37 |
self._test_parse("foo_bar", urltree.StaticLabel, name="foo_bar")
|
|
38 |
self._test_parse("foo2", urltree.StaticLabel, name="foo2")
|
|
39 |
self._test_parse("2foo.q", urltree.StaticLabel, name="2foo.q")
|
|
40 |
|
|
41 |
def test_parse_static_invalid (self) :
|
|
42 |
self.assertRaises(urltree.URLError, urltree.Label.parse, "foo/bar", {}, self.config)
|
|
43 |
|
|
44 |
def test_parse_value_plain (self) :
|
|
45 |
self._test_parse("{foo}", urltree.SimpleValueLabel, key="foo", default=None, type_name=None, type=urltree.URLConfig.BUILTIN_TYPES[None])
|
|
46 |
|
|
47 |
def test_parse_value_default1 (self) :
|
|
48 |
self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, key="foo", default="bar1", type_name=None)
|
|
49 |
|
|
50 |
def test_parse_value_default2 (self) :
|
|
51 |
self._test_parse("{foo}", urltree.SimpleValueLabel, dict(foo="bar2"), key="foo", default="bar2", type_name=None)
|
|
52 |
|
|
53 |
def test_parse_value_default3 (self) :
|
|
54 |
self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, dict(foo="bar3"), key="foo", default="bar3", type_name=None)
|
|
55 |
|
|
56 |
def test_parse_value_type_str (self) :
|
|
57 |
self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str'])
|
|
58 |
|
|
59 |
def test_parse_value_type_int (self) :
|
|
60 |
self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int'])
|
|
61 |
|
|
62 |
class TestEmptyLabel (unittest.TestCase) :
|
|
63 |
def setUp (self) :
|
|
64 |
self.label = urltree.EmptyLabel()
|
|
65 |
|
|
66 |
def test_eq (self) :
|
|
67 |
self.assertTrue(self.label == urltree.EmptyLabel())
|
|
68 |
self.assertFalse(self.label == urltree.StaticLabel("foo"))
|
|
69 |
|
|
70 |
def test_match (self) :
|
|
71 |
self.assertFalse(self.label.match())
|
|
72 |
self.assertFalse(self.label.match("foo"))
|
|
73 |
self.assertTrue(self.label.match(""))
|
|
74 |
|
|
75 |
def test_build (self) :
|
|
76 |
self.assertEqual(self.label.build({}), "")
|
|
77 |
|
|
78 |
def test_build_default (self) :
|
|
79 |
self.assertEqual(self.label.build_default({}), (False, ""))
|
|
80 |
|
|
81 |
def test_str (self) :
|
|
82 |
self.assertEqual(str(self.label), "")
|
|
83 |
|
|
84 |
class TestStaticLabel (unittest.TestCase) :
|
|
85 |
def setUp (self) :
|
|
86 |
self.label = urltree.StaticLabel("test")
|
|
87 |
|
|
88 |
def test_eq (self) :
|
|
89 |
self.assertTrue(self.label == urltree.StaticLabel("test"))
|
|
90 |
self.assertFalse(self.label == urltree.StaticLabel("Test"))
|
|
91 |
self.assertFalse(self.label == urltree.EmptyLabel())
|
|
92 |
|
|
93 |
def test_match (self) :
|
|
94 |
self.assertFalse(self.label.match())
|
|
95 |
self.assertFalse(self.label.match("foo"))
|
|
96 |
self.assertTrue(self.label.match("test"))
|
|
97 |
|
|
98 |
def test_build (self) :
|
|
99 |
self.assertEqual(self.label.build({}), "test")
|
|
100 |
|
|
101 |
def test_build_default (self) :
|
|
102 |
self.assertEqual(self.label.build_default({}), (False, "test"))
|
|
103 |
|
|
104 |
def test_str (self) :
|
|
105 |
self.assertEqual(str(self.label), "test")
|
|
106 |
|
|
107 |
class TestSimpleValueLabel (unittest.TestCase) :
|
|
108 |
def setUp (self) :
|
|
109 |
self.label = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], None)
|
|
110 |
self.label_default_0 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 0)
|
|
111 |
self.label_default_1 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 1)
|
|
112 |
self.label_str = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], None)
|
|
113 |
self.label_str_default = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], 'def')
|
|
114 |
|
|
115 |
def test_eq (self) :
|
|
116 |
self.assertTrue(self.label == urltree.SimpleValueLabel("test", 'str', None, 1))
|
|
117 |
self.assertFalse(self.label == urltree.StaticLabel("Test"))
|
|
118 |
self.assertFalse(self.label == urltree.EmptyLabel())
|
|
119 |
|
|
120 |
def _check_value (self, label, label_value, value, is_default) :
|
|
121 |
self.assertTrue(isinstance(label_value, urltree.LabelValue))
|
|
122 |
self.assertEqual(label_value.label, label)
|
|
123 |
self.assertEqual(label_value.value, value)
|
|
124 |
self.assertEqual(label_value.is_default, is_default)
|
|
125 |
|
|
126 |
def test_match_default_none (self) :
|
|
127 |
self.assertEquals(self.label.match(), None)
|
|
128 |
|
|
129 |
def test_match_default_value (self) :
|
|
130 |
self._check_value(self.label_default_0, self.label_default_0.match(), 0, True)
|
|
131 |
self._check_value(self.label_default_1, self.label_default_1.match(), 1, True)
|
|
132 |
|
|
133 |
def test_match_invalid (self) :
|
|
134 |
self.assertEquals(self.label.match("foo"), False)
|
|
135 |
self.assertEquals(self.label_default_0.match("foo"), False)
|
|
136 |
self.assertEquals(self.label_default_1.match("foo"), False)
|
|
137 |
|
|
138 |
def test_match_valid (self) :
|
|
139 |
self._check_value(self.label, self.label.match("0"), 0, False)
|
|
140 |
self._check_value(self.label, self.label.match("1"), 1, False)
|
|
141 |
self._check_value(self.label_default_0, self.label_default_0.match("1"), 1, False)
|
|
142 |
self._check_value(self.label_default_1, self.label_default_1.match("0"), 0, False)
|
|
143 |
|
|
144 |
def test_build (self) :
|
|
145 |
self.assertEqual(self.label.build(dict(test=1)), "1")
|
|
146 |
|
|
147 |
def test_build_nodefault (self) :
|
|
148 |
self.assertRaises(urltree.URLError, self.label.build, {})
|
|
149 |
|
|
150 |
def test_build_none (self) :
|
|
151 |
self.assertRaises(urltree.URLError, self.label.build, dict(test=None))
|
|
152 |
|
|
153 |
def test_build_default (self) :
|
|
154 |
self.assertEqual(self.label_default_0.build_default({}), (True, "0"))
|
|
155 |
self.assertEqual(self.label_default_1.build_default({}), (True, "1"))
|
|
156 |
|
|
157 |
def test_build_nonedefault (self) :
|
|
158 |
self.assertEqual(self.label_default_1.build_default(dict(test=None)), (True, "1"))
|
|
159 |
|
|
160 |
def test_build_value (self) :
|
|
161 |
self.assertEqual(self.label.build_default(dict(test=0)), (False, "0"))
|
|
162 |
self.assertEqual(self.label.build_default(dict(test=1)), (False, "1"))
|
|
163 |
self.assertEqual(self.label_default_0.build_default(dict(test=1)), (False, "1"))
|
|
164 |
self.assertEqual(self.label_default_0.build_default(dict(test=0)), (False, "0"))
|
|
165 |
|
|
166 |
def test_str (self) :
|
|
167 |
self.assertEqual(str(self.label), "{test:int}")
|
|
168 |
self.assertEqual(str(self.label_default_0), "{test:int=0}")
|
|
169 |
self.assertEqual(str(self.label_default_1), "{test:int=1}")
|
|
170 |
self.assertEqual(str(self.label_str), "{test}")
|
|
171 |
self.assertEqual(str(self.label_str_default), "{test=def}")
|
|
172 |
|