tests/test_urltree.py
changeset 79 747554808944
child 80 94c493b7c046
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_urltree.py	Mon Feb 16 19:46:19 2009 +0200
@@ -0,0 +1,172 @@
+"""
+    Unit tests for qmsk.web.urltree
+"""
+
+import unittest
+import urltree
+
+class TestLabelValue (unittest.TestCase) :
+    class dummylabel :
+        key = 'foo'
+
+    def test_str_default (self) :
+        self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', True)), "foo")
+
+    def test_str_value (self) :
+        self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', False)), "foo='bar'")
+
+class TestLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.config = urltree.URLConfig()
+
+    def _test_parse (self, mask, _type, defaults={}, **attrs) :
+        label = urltree.Label.parse(mask, defaults, self.config)
+
+        self.assertTrue(isinstance(label, _type))
+        
+        for k, v in attrs.iteritems() :
+            self.assertEqual(getattr(label, k), v)
+
+    def test_parse_empty (self) :
+        self._test_parse("", urltree.EmptyLabel)
+
+    def test_parse_static (self) :
+        self._test_parse("foo", urltree.StaticLabel, name="foo")
+
+    def test_parse_static_valid (self) :
+        self._test_parse("foo_bar", urltree.StaticLabel, name="foo_bar")
+        self._test_parse("foo2", urltree.StaticLabel, name="foo2")
+        self._test_parse("2foo.q", urltree.StaticLabel, name="2foo.q")
+    
+    def test_parse_static_invalid (self) :
+        self.assertRaises(urltree.URLError, urltree.Label.parse, "foo/bar", {}, self.config)
+
+    def test_parse_value_plain (self) :
+        self._test_parse("{foo}", urltree.SimpleValueLabel, key="foo", default=None, type_name=None, type=urltree.URLConfig.BUILTIN_TYPES[None])
+
+    def test_parse_value_default1 (self) :
+        self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, key="foo", default="bar1", type_name=None)
+
+    def test_parse_value_default2 (self) :
+        self._test_parse("{foo}", urltree.SimpleValueLabel, dict(foo="bar2"), key="foo", default="bar2", type_name=None)
+
+    def test_parse_value_default3 (self) :
+        self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, dict(foo="bar3"), key="foo", default="bar3", type_name=None)
+
+    def test_parse_value_type_str (self) :
+        self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str'])
+
+    def test_parse_value_type_int (self) :
+        self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int'])
+    
+class TestEmptyLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.EmptyLabel()
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.EmptyLabel())
+        self.assertFalse(self.label == urltree.StaticLabel("foo"))
+
+    def test_match (self) :
+        self.assertFalse(self.label.match())
+        self.assertFalse(self.label.match("foo"))
+        self.assertTrue(self.label.match(""))
+
+    def test_build (self) :
+        self.assertEqual(self.label.build({}), "")
+
+    def test_build_default (self) :
+        self.assertEqual(self.label.build_default({}), (False, ""))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "")
+
+class TestStaticLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.StaticLabel("test")
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.StaticLabel("test"))
+        self.assertFalse(self.label == urltree.StaticLabel("Test"))
+        self.assertFalse(self.label == urltree.EmptyLabel())
+
+    def test_match (self) :
+        self.assertFalse(self.label.match())
+        self.assertFalse(self.label.match("foo"))
+        self.assertTrue(self.label.match("test"))
+
+    def test_build (self) :
+        self.assertEqual(self.label.build({}), "test")
+
+    def test_build_default (self) :
+        self.assertEqual(self.label.build_default({}), (False, "test"))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "test")
+
+class TestSimpleValueLabel (unittest.TestCase) :
+    def setUp (self) :
+        self.label = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], None)
+        self.label_default_0 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 0)
+        self.label_default_1 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 1)
+        self.label_str = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], None)
+        self.label_str_default = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], 'def')
+
+    def test_eq (self) :
+        self.assertTrue(self.label == urltree.SimpleValueLabel("test", 'str', None, 1))
+        self.assertFalse(self.label == urltree.StaticLabel("Test"))
+        self.assertFalse(self.label == urltree.EmptyLabel())
+    
+    def _check_value (self, label, label_value, value, is_default) :
+        self.assertTrue(isinstance(label_value, urltree.LabelValue))
+        self.assertEqual(label_value.label, label)
+        self.assertEqual(label_value.value, value)
+        self.assertEqual(label_value.is_default, is_default)
+
+    def test_match_default_none (self) :
+        self.assertEquals(self.label.match(), None)
+
+    def test_match_default_value (self) :
+        self._check_value(self.label_default_0, self.label_default_0.match(), 0, True)
+        self._check_value(self.label_default_1, self.label_default_1.match(), 1, True)
+    
+    def test_match_invalid (self) :
+        self.assertEquals(self.label.match("foo"), False)
+        self.assertEquals(self.label_default_0.match("foo"), False)
+        self.assertEquals(self.label_default_1.match("foo"), False)
+
+    def test_match_valid (self) :
+        self._check_value(self.label, self.label.match("0"), 0, False)
+        self._check_value(self.label, self.label.match("1"), 1, False)
+        self._check_value(self.label_default_0, self.label_default_0.match("1"), 1, False)
+        self._check_value(self.label_default_1, self.label_default_1.match("0"), 0, False)
+
+    def test_build (self) :
+        self.assertEqual(self.label.build(dict(test=1)), "1")
+    
+    def test_build_nodefault (self) :
+        self.assertRaises(urltree.URLError, self.label.build, {})
+
+    def test_build_none (self) :
+        self.assertRaises(urltree.URLError, self.label.build, dict(test=None))
+    
+    def test_build_default (self) :
+        self.assertEqual(self.label_default_0.build_default({}), (True, "0"))
+        self.assertEqual(self.label_default_1.build_default({}), (True, "1"))
+    
+    def test_build_nonedefault (self) :
+        self.assertEqual(self.label_default_1.build_default(dict(test=None)), (True, "1"))
+
+    def test_build_value (self) :
+        self.assertEqual(self.label.build_default(dict(test=0)), (False, "0"))
+        self.assertEqual(self.label.build_default(dict(test=1)), (False, "1"))
+        self.assertEqual(self.label_default_0.build_default(dict(test=1)), (False, "1"))
+        self.assertEqual(self.label_default_0.build_default(dict(test=0)), (False, "0"))
+
+    def test_str (self) :
+        self.assertEqual(str(self.label), "{test:int}")
+        self.assertEqual(str(self.label_default_0), "{test:int=0}")
+        self.assertEqual(str(self.label_default_1), "{test:int=1}")
+        self.assertEqual(str(self.label_str), "{test}")
+        self.assertEqual(str(self.label_str_default), "{test=def}")
+