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 |
|
80
|
173 |
class TestStringType (unittest.TestCase) :
|
|
174 |
def setUp (self) :
|
|
175 |
self.type = urltree.URLStringType()
|
|
176 |
|
|
177 |
def test_test (self) :
|
|
178 |
self.assertTrue(self.type.test(""))
|
|
179 |
self.assertTrue(self.type.test("xxx"))
|
|
180 |
|
|
181 |
def test_parse (self) :
|
|
182 |
self.assertEqual(self.type.parse(""), "")
|
|
183 |
self.assertEqual(self.type.parse("xxx"), "xxx")
|
|
184 |
|
|
185 |
def test_build (self) :
|
|
186 |
self.assertEqual(self.type.build(""), "")
|
|
187 |
self.assertEqual(self.type.build("xxx"), "xxx")
|
|
188 |
self.assertEqual(self.type.build("äää"), "äää")
|
|
189 |
|
|
190 |
class TestIntegerType (unittest.TestCase) :
|
|
191 |
def setUp (self) :
|
|
192 |
self.type = urltree.URLIntegerType()
|
|
193 |
self.type_positive = urltree.URLIntegerType(allow_negative=False)
|
|
194 |
self.type_nonzero = urltree.URLIntegerType(allow_zero=False)
|
|
195 |
self.type_max_5 = urltree.URLIntegerType(max=5)
|
|
196 |
|
|
197 |
def test_test (self) :
|
|
198 |
self.assertTrue(self.type.test("1"))
|
|
199 |
self.assertFalse(self.type.test("xx"))
|
|
200 |
self.assertTrue(self.type_positive.test("1"))
|
|
201 |
self.assertFalse(self.type_positive.test("-1"))
|
|
202 |
self.assertTrue(self.type_nonzero.test("1"))
|
|
203 |
self.assertFalse(self.type_nonzero.test("0"))
|
|
204 |
self.assertTrue(self.type_max_5.test("5"))
|
|
205 |
self.assertFalse(self.type_max_5.test("6"))
|
|
206 |
|
|
207 |
def test_parse_invalid (self) :
|
|
208 |
self.assertRaises(ValueError, self.type.parse, "xx")
|
|
209 |
self.assertRaises(ValueError, self.type_nonzero.parse, "0")
|
|
210 |
|
|
211 |
def test_parse_valid (self) :
|
|
212 |
self.assertEqual(self.type.parse("0"), 0)
|
|
213 |
self.assertEqual(self.type.parse("2"), 2)
|
|
214 |
self.assertEqual(self.type_nonzero.parse("3"), 3)
|
|
215 |
|
|
216 |
def test_append (self) :
|
|
217 |
self.assertRaises(URLError, self.type.append, 0, 1)
|
|
218 |
|
|
219 |
def test_build (self) :
|
|
220 |
self.assertEqual(self.type.build(0), "0")
|
|
221 |
self.assertEqual(self.type.build(5), "5")
|
|
222 |
self.assertEqual(self.type_positive.build(1), "1")
|
|
223 |
self.assertEqual(self.type_nonzero.build(1), "1")
|
|
224 |
self.assertEqual(self.type_max_5.build(5), "5")
|
|
225 |
|
|
226 |
def test_build_invalid (self) :
|
|
227 |
self.assertRaises(ValueError, self.type_positive.build, -1)
|
|
228 |
self.assertRaises(ValueError, self.type_nonzero.build, 0)
|
|
229 |
self.assertRaises(ValueError, self.type_max_5.build, 5)
|
|
230 |
|
|
231 |
def test_build_multi (self) :
|
|
232 |
self.assertEqual(self.type.build(0), ["0"])
|
|
233 |
|
|
234 |
class TestListType (unittest.TestCase) :
|
|
235 |
def setUp (self) :
|
|
236 |
self.type = urltree.URLListType()
|
|
237 |
|
|
238 |
def test_parse (self) :
|
|
239 |
self.assertEqual(self.type.parse("x"), ["x"])
|
|
240 |
self.assertEqual(self.type.parse(""), [""])
|
|
241 |
|
|
242 |
def test_append (self) :
|
|
243 |
self.assertEqual(self.type.append(["x"], ["y"]), ["x", "y"])
|
|
244 |
|
|
245 |
def test_build_multi (self) :
|
|
246 |
self.assertEqual(self.type.build_multi(["x", "y"]), ["x", "y"])
|
|
247 |
|
|
248 |
class TestConfig (unittest.TestCase) :
|
|
249 |
def test_init (self) :
|
|
250 |
urltree.URLConfig(type_dict=dict(foo=None), ignore_extra_args=True)
|
|
251 |
|
|
252 |
def test_get_type (self) :
|
|
253 |
self.assertEquals(urltree.URLConfig(dict(foo='xxx')).get_type('foo'), 'xxx')
|
|
254 |
|
|
255 |
def test_call (self) :
|
|
256 |
config = urltree.URLConfig()
|
|
257 |
url = config("foo", None)
|
|
258 |
|
|
259 |
self.assertTrue(isinstance(url, urltree.URL))
|
|
260 |
self.assertTrue(url in config.urls)
|
|
261 |
|
|
262 |
def test_iter (self) :
|
|
263 |
config = urltree.URLConfig()
|
|
264 |
url1 = config("foo1", None)
|
|
265 |
url2 = config("foo2", None)
|
|
266 |
|
|
267 |
urls = list(config)
|
|
268 |
|
|
269 |
self.assertTrue(urls[0].url_mask == "foo1")
|
|
270 |
self.assertTrue(urls[1].url_mask == "foo2")
|
|
271 |
|