terom@81: # :set encoding=utf8 terom@79: """ terom@79: Unit tests for qmsk.web.urltree terom@79: """ terom@79: terom@79: import unittest terom@79: import urltree terom@79: terom@79: class TestLabelValue (unittest.TestCase) : terom@79: class dummylabel : terom@79: key = 'foo' terom@79: terom@79: def test_str_default (self) : terom@79: self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', True)), "foo") terom@79: terom@79: def test_str_value (self) : terom@79: self.assertEqual(str(urltree.LabelValue(self.dummylabel, 'bar', False)), "foo='bar'") terom@79: terom@79: class TestLabel (unittest.TestCase) : terom@79: def setUp (self) : terom@79: self.config = urltree.URLConfig() terom@79: terom@79: def _test_parse (self, mask, _type, defaults={}, **attrs) : terom@79: label = urltree.Label.parse(mask, defaults, self.config) terom@79: terom@79: self.assertTrue(isinstance(label, _type)) terom@79: terom@79: for k, v in attrs.iteritems() : terom@79: self.assertEqual(getattr(label, k), v) terom@79: terom@79: def test_parse_empty (self) : terom@79: self._test_parse("", urltree.EmptyLabel) terom@79: terom@79: def test_parse_static (self) : terom@79: self._test_parse("foo", urltree.StaticLabel, name="foo") terom@79: terom@79: def test_parse_static_valid (self) : terom@79: self._test_parse("foo_bar", urltree.StaticLabel, name="foo_bar") terom@79: self._test_parse("foo2", urltree.StaticLabel, name="foo2") terom@79: self._test_parse("2foo.q", urltree.StaticLabel, name="2foo.q") terom@79: terom@79: def test_parse_static_invalid (self) : terom@79: self.assertRaises(urltree.URLError, urltree.Label.parse, "foo/bar", {}, self.config) terom@79: terom@79: def test_parse_value_plain (self) : terom@79: self._test_parse("{foo}", urltree.SimpleValueLabel, key="foo", default=None, type_name=None, type=urltree.URLConfig.BUILTIN_TYPES[None]) terom@79: terom@79: def test_parse_value_default1 (self) : terom@79: self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, key="foo", default="bar1", type_name=None) terom@79: terom@79: def test_parse_value_default2 (self) : terom@79: self._test_parse("{foo}", urltree.SimpleValueLabel, dict(foo="bar2"), key="foo", default="bar2", type_name=None) terom@79: terom@79: def test_parse_value_default3 (self) : terom@79: self._test_parse("{foo=bar1}", urltree.SimpleValueLabel, dict(foo="bar3"), key="foo", default="bar3", type_name=None) terom@79: terom@79: def test_parse_value_type_str (self) : terom@79: self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str']) terom@79: terom@79: def test_parse_value_type_int (self) : terom@79: self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int']) terom@79: terom@82: def test_parse_invalid_type (self) : terom@82: self.assertRaises(KeyError, self._test_parse, "{foo:bar}", None) terom@82: terom@79: class TestEmptyLabel (unittest.TestCase) : terom@79: def setUp (self) : terom@79: self.label = urltree.EmptyLabel() terom@79: terom@79: def test_eq (self) : terom@79: self.assertTrue(self.label == urltree.EmptyLabel()) terom@79: self.assertFalse(self.label == urltree.StaticLabel("foo")) terom@79: terom@79: def test_match (self) : terom@79: self.assertFalse(self.label.match()) terom@79: self.assertFalse(self.label.match("foo")) terom@79: self.assertTrue(self.label.match("")) terom@79: terom@79: def test_build (self) : terom@79: self.assertEqual(self.label.build({}), "") terom@79: terom@79: def test_build_default (self) : terom@79: self.assertEqual(self.label.build_default({}), (False, "")) terom@79: terom@79: def test_str (self) : terom@79: self.assertEqual(str(self.label), "") terom@79: terom@79: class TestStaticLabel (unittest.TestCase) : terom@79: def setUp (self) : terom@79: self.label = urltree.StaticLabel("test") terom@79: terom@79: def test_eq (self) : terom@79: self.assertTrue(self.label == urltree.StaticLabel("test")) terom@79: self.assertFalse(self.label == urltree.StaticLabel("Test")) terom@79: self.assertFalse(self.label == urltree.EmptyLabel()) terom@79: terom@79: def test_match (self) : terom@79: self.assertFalse(self.label.match()) terom@79: self.assertFalse(self.label.match("foo")) terom@79: self.assertTrue(self.label.match("test")) terom@79: terom@79: def test_build (self) : terom@79: self.assertEqual(self.label.build({}), "test") terom@79: terom@79: def test_build_default (self) : terom@79: self.assertEqual(self.label.build_default({}), (False, "test")) terom@79: terom@79: def test_str (self) : terom@79: self.assertEqual(str(self.label), "test") terom@79: terom@79: class TestSimpleValueLabel (unittest.TestCase) : terom@79: def setUp (self) : terom@79: self.label = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], None) terom@79: self.label_default_0 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 0) terom@79: self.label_default_1 = urltree.SimpleValueLabel("test", 'int', urltree.URLConfig.BUILTIN_TYPES['int'], 1) terom@79: self.label_str = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], None) terom@79: self.label_str_default = urltree.SimpleValueLabel("test", None, urltree.URLConfig.BUILTIN_TYPES[None], 'def') terom@79: terom@79: def test_eq (self) : terom@79: self.assertTrue(self.label == urltree.SimpleValueLabel("test", 'str', None, 1)) terom@79: self.assertFalse(self.label == urltree.StaticLabel("Test")) terom@79: self.assertFalse(self.label == urltree.EmptyLabel()) terom@79: terom@79: def _check_value (self, label, label_value, value, is_default) : terom@79: self.assertTrue(isinstance(label_value, urltree.LabelValue)) terom@79: self.assertEqual(label_value.label, label) terom@79: self.assertEqual(label_value.value, value) terom@79: self.assertEqual(label_value.is_default, is_default) terom@79: terom@79: def test_match_default_none (self) : terom@79: self.assertEquals(self.label.match(), None) terom@79: terom@79: def test_match_default_value (self) : terom@79: self._check_value(self.label_default_0, self.label_default_0.match(), 0, True) terom@79: self._check_value(self.label_default_1, self.label_default_1.match(), 1, True) terom@79: terom@79: def test_match_invalid (self) : terom@79: self.assertEquals(self.label.match("foo"), False) terom@79: self.assertEquals(self.label_default_0.match("foo"), False) terom@79: self.assertEquals(self.label_default_1.match("foo"), False) terom@79: terom@79: def test_match_valid (self) : terom@79: self._check_value(self.label, self.label.match("0"), 0, False) terom@79: self._check_value(self.label, self.label.match("1"), 1, False) terom@79: self._check_value(self.label_default_0, self.label_default_0.match("1"), 1, False) terom@79: self._check_value(self.label_default_1, self.label_default_1.match("0"), 0, False) terom@79: terom@79: def test_build (self) : terom@79: self.assertEqual(self.label.build(dict(test=1)), "1") terom@79: terom@79: def test_build_nodefault (self) : terom@79: self.assertRaises(urltree.URLError, self.label.build, {}) terom@79: terom@79: def test_build_none (self) : terom@79: self.assertRaises(urltree.URLError, self.label.build, dict(test=None)) terom@79: terom@79: def test_build_default (self) : terom@79: self.assertEqual(self.label_default_0.build_default({}), (True, "0")) terom@79: self.assertEqual(self.label_default_1.build_default({}), (True, "1")) terom@83: self.assertEqual(self.label_default_0.build_default(dict(test=0)), (True, "0")) terom@79: terom@79: def test_build_nonedefault (self) : terom@79: self.assertEqual(self.label_default_1.build_default(dict(test=None)), (True, "1")) terom@79: terom@79: def test_build_value (self) : terom@79: self.assertEqual(self.label.build_default(dict(test=0)), (False, "0")) terom@79: self.assertEqual(self.label.build_default(dict(test=1)), (False, "1")) terom@79: self.assertEqual(self.label_default_0.build_default(dict(test=1)), (False, "1")) terom@79: terom@79: def test_str (self) : terom@79: self.assertEqual(str(self.label), "{test:int}") terom@79: self.assertEqual(str(self.label_default_0), "{test:int=0}") terom@79: self.assertEqual(str(self.label_default_1), "{test:int=1}") terom@79: self.assertEqual(str(self.label_str), "{test}") terom@79: self.assertEqual(str(self.label_str_default), "{test=def}") terom@79: terom@80: class TestStringType (unittest.TestCase) : terom@80: def setUp (self) : terom@80: self.type = urltree.URLStringType() terom@80: terom@80: def test_test (self) : terom@80: self.assertTrue(self.type.test("")) terom@80: self.assertTrue(self.type.test("xxx")) terom@80: terom@80: def test_parse (self) : terom@80: self.assertEqual(self.type.parse(""), "") terom@80: self.assertEqual(self.type.parse("xxx"), "xxx") terom@80: terom@80: def test_build (self) : terom@80: self.assertEqual(self.type.build(""), "") terom@80: self.assertEqual(self.type.build("xxx"), "xxx") terom@80: self.assertEqual(self.type.build("äää"), "äää") terom@80: terom@80: class TestIntegerType (unittest.TestCase) : terom@80: def setUp (self) : terom@80: self.type = urltree.URLIntegerType() terom@80: self.type_positive = urltree.URLIntegerType(allow_negative=False) terom@80: self.type_nonzero = urltree.URLIntegerType(allow_zero=False) terom@80: self.type_max_5 = urltree.URLIntegerType(max=5) terom@80: terom@80: def test_test (self) : terom@80: self.assertTrue(self.type.test("1")) terom@80: self.assertFalse(self.type.test("xx")) terom@80: self.assertTrue(self.type_positive.test("1")) terom@80: self.assertFalse(self.type_positive.test("-1")) terom@80: self.assertTrue(self.type_nonzero.test("1")) terom@80: self.assertFalse(self.type_nonzero.test("0")) terom@80: self.assertTrue(self.type_max_5.test("5")) terom@80: self.assertFalse(self.type_max_5.test("6")) terom@80: terom@80: def test_parse_invalid (self) : terom@80: self.assertRaises(ValueError, self.type.parse, "xx") terom@80: self.assertRaises(ValueError, self.type_nonzero.parse, "0") terom@80: terom@80: def test_parse_valid (self) : terom@80: self.assertEqual(self.type.parse("0"), 0) terom@80: self.assertEqual(self.type.parse("2"), 2) terom@80: self.assertEqual(self.type_nonzero.parse("3"), 3) terom@80: terom@80: def test_append (self) : terom@81: self.assertRaises(urltree.URLError, self.type.append, 0, 1) terom@80: terom@80: def test_build (self) : terom@80: self.assertEqual(self.type.build(0), "0") terom@80: self.assertEqual(self.type.build(5), "5") terom@80: self.assertEqual(self.type_positive.build(1), "1") terom@80: self.assertEqual(self.type_nonzero.build(1), "1") terom@80: self.assertEqual(self.type_max_5.build(5), "5") terom@80: terom@80: def test_build_invalid (self) : terom@80: self.assertRaises(ValueError, self.type_positive.build, -1) terom@80: self.assertRaises(ValueError, self.type_nonzero.build, 0) terom@81: self.assertRaises(ValueError, self.type_max_5.build, 6) terom@80: terom@80: def test_build_multi (self) : terom@81: self.assertEqual(self.type.build_multi(0), ["0"]) terom@80: terom@80: class TestListType (unittest.TestCase) : terom@80: def setUp (self) : terom@80: self.type = urltree.URLListType() terom@80: terom@80: def test_parse (self) : terom@80: self.assertEqual(self.type.parse("x"), ["x"]) terom@80: self.assertEqual(self.type.parse(""), [""]) terom@80: terom@80: def test_append (self) : terom@80: self.assertEqual(self.type.append(["x"], ["y"]), ["x", "y"]) terom@80: terom@80: def test_build_multi (self) : terom@80: self.assertEqual(self.type.build_multi(["x", "y"]), ["x", "y"]) terom@80: terom@80: class TestConfig (unittest.TestCase) : terom@80: def test_init (self) : terom@80: urltree.URLConfig(type_dict=dict(foo=None), ignore_extra_args=True) terom@80: terom@80: def test_get_type (self) : terom@80: self.assertEquals(urltree.URLConfig(dict(foo='xxx')).get_type('foo'), 'xxx') terom@82: terom@82: def test_get_type_invalid (self) : terom@82: self.assertRaises(KeyError, urltree.URLConfig(dict(foo='xxx')).get_type, 'xxx') terom@80: terom@80: def test_call (self) : terom@80: config = urltree.URLConfig() terom@80: url = config("foo", None) terom@80: terom@80: self.assertTrue(isinstance(url, urltree.URL)) terom@80: self.assertTrue(url in config.urls) terom@80: terom@80: def test_iter (self) : terom@80: config = urltree.URLConfig() terom@80: url1 = config("foo1", None) terom@80: url2 = config("foo2", None) terom@80: terom@80: urls = list(config) terom@80: terom@80: self.assertTrue(urls[0].url_mask == "foo1") terom@80: self.assertTrue(urls[1].url_mask == "foo2") terom@80: terom@82: class TestURL (unittest.TestCase) : terom@82: def setUp (self) : terom@82: self.config = urltree.URLConfig(ignore_extra_args=True) terom@82: self.config_strict = urltree.URLConfig(ignore_extra_args=False) terom@82: terom@82: def _test_label_path (self, mask, *path, **qargs) : terom@82: url = self.config(mask, None) terom@82: terom@82: # right label path terom@82: self.assertEquals(url.label_path, list(path)) terom@82: terom@82: # right qargs keys terom@82: self.assertEquals(set(url.query_args), set(qargs)) terom@82: terom@82: # right qargs values terom@82: for key, value in qargs.iteritems() : terom@82: self.assertEquals(url.query_args[key], value) terom@83: terom@83: # __init__ terom@82: def test_label_path_empty (self) : terom@82: self._test_label_path("", urltree.EmptyLabel()) terom@82: terom@82: def test_label_path_root (self) : terom@82: self._test_label_path("/", urltree.EmptyLabel()) terom@82: terom@82: def test_label_path_static (self) : terom@82: self._test_label_path("/foo", urltree.StaticLabel("foo")) terom@82: terom@82: def test_label_path_static2 (self) : terom@82: self._test_label_path("/foo/bar/", urltree.StaticLabel("foo"), urltree.StaticLabel("bar"), urltree.EmptyLabel()) terom@82: terom@82: def test_label_path_mix (self) : terom@82: self._test_label_path("/foo/{bar}", urltree.StaticLabel("foo"), urltree.SimpleValueLabel("bar", None, None, None)) terom@82: terom@82: # def test_query_args_root_empty (self) : terom@82: # self._test_label_path("/?", urltree.EmptyLabel()) terom@82: terom@82: def test_query_args_simple (self) : terom@82: self._test_label_path("/x/?foo", urltree.StaticLabel("x"), foo=(self.config.get_type(None), None)) terom@82: terom@82: def test_query_args_multi (self) : terom@82: self._test_label_path("/x/?foo=0&bar&tee:int=&eee:int", urltree.StaticLabel("x"), terom@82: foo = (self.config.get_type(None), "0"), terom@82: bar = (self.config.get_type(None), None), terom@82: tee = (self.config.get_type('int'), ''), terom@82: eee = (self.config.get_type('int'), None), terom@82: ) terom@82: terom@82: def test_label_path_mutate (self) : terom@82: l = self.config("xxx", None) terom@82: terom@82: lp = l.get_label_path() terom@82: terom@82: lp.pop(0) terom@82: terom@82: self.assertTrue(len(l.label_path) > len(lp)) terom@82: terom@82: def _setup_handler (self) : terom@82: def _handler (req, **kwargs) : terom@82: return kwargs terom@82: terom@82: return _handler terom@82: terom@82: def _setup_execute (self, mask, config) : terom@82: _handler = self._setup_handler() terom@82: terom@82: url = config(mask, _handler) terom@82: terom@82: return url terom@82: terom@82: class dummyrequest : terom@82: def __init__ (self, qargs) : self.qargs = qargs terom@82: def get_args (self) : return self.qargs terom@82: terom@82: class dummy_label : terom@82: def __init__ (self, key, type) : terom@82: self.key = key terom@82: self.type = type terom@82: terom@82: class dummy_labelvalue : terom@82: def __init__ (self, key, value, is_default, type) : terom@82: self.label = TestURL.dummy_label(key, type) terom@82: self.value = value terom@82: self.is_default = is_default terom@82: terom@82: def _test_execute (self, mask, values={}, qargs={}, qlist=[], config=None) : terom@82: if not config : terom@82: config = self.config terom@82: terom@82: # setup terom@82: url = self._setup_execute(mask, config) terom@82: req = self.dummyrequest(qargs.items() + qlist) terom@82: values = [self.dummy_labelvalue(k, v, d, config.get_type()) for k, (v, d) in values.iteritems()] terom@82: terom@82: # exec terom@82: out_args = url.execute(req, values) terom@82: terom@82: return out_args terom@83: terom@83: # execute terom@82: def test_execute_empty (self) : terom@82: self.assertEquals(set(self._test_execute("/")), set()) terom@82: terom@82: def test_execute_plain (self) : terom@82: self.assertEquals(set(self._test_execute("/foo")), set()) terom@82: terom@82: def test_execute_simple (self) : terom@82: self.assertEquals(self._test_execute("/foo/{bar}", dict(bar=(0, False))), dict(bar=0)) terom@82: terom@82: def test_execute_multi (self) : terom@82: self.assertEquals(self._test_execute("/foo/{bar}/{quux}", dict(bar=(1, False), quux=(2, False))), dict(bar=1, quux=2)) terom@82: terom@82: def test_execute_default (self) : terom@82: self.assertEquals(self._test_execute("/foo/{bar=0}", dict(bar=("0", True))), dict(bar="0")) terom@82: terom@82: def test_execute_qargs_default (self) : terom@82: self.assertEquals(self._test_execute("/{foo}/?bar=0", dict(foo=("x", False))), dict(foo="x", bar="0")) terom@82: terom@82: def test_execute_qargs_default_type (self) : terom@82: self.assertEquals(self._test_execute("/{foo}/?bar:int=0", dict(foo=("x", False))), dict(foo="x", bar=0)) terom@82: terom@82: def test_execute_qargs_default_none (self) : terom@82: self.assertEquals(self._test_execute("/{foo}/?bar=", dict(foo=("x", False))), dict(foo="x")) terom@82: terom@82: def test_execute_qargs_missing (self) : terom@82: self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False))) terom@82: terom@82: def test_execute_qargs_invalid (self) : terom@82: self.assertRaises(ValueError, self._test_execute, "/{foo}/?bar:int", dict(foo=("x", False)), dict(bar="x")) terom@82: terom@82: def test_execute_qargs_simple (self) : terom@82: self.assertEquals(self._test_execute("/{foo}/?bar", dict(foo=("x", False)), dict(bar="y")), dict(foo="x", bar="y")) terom@82: terom@82: def test_execute_qargs_novalue (self) : terom@82: self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), dict(bar='')) terom@82: terom@82: def test_execute_qargs_multi_invalid (self) : terom@82: self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')]) terom@82: terom@82: def test_execute_qargs_multi_list (self) : terom@82: self.assertEqual(self._test_execute("/{foo}/?bar:list", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')]), dict(foo='x', bar=['a', 'b'])) terom@82: terom@82: def test_execute_qarg_override_strict (self) : terom@82: self.assertRaises(urltree.URLError, self._test_execute, "/{foo}", dict(foo=("x1", False)), dict(foo="x2"), config=self.config_strict) terom@82: terom@82: def test_execute_qarg_override_ignore (self) : terom@82: self.assertEqual(self._test_execute("/{foo}", dict(foo=("x1", False)), dict(foo="x2")), dict(foo="x1")) terom@82: terom@82: def test_execute_qarg_override_ok (self) : terom@82: self.assertEqual(self._test_execute("/{foo=x1}", dict(foo=("x1", True)), dict(foo="x2")), dict(foo="x2")) terom@83: terom@83: # build terom@83: class dummyrequest_page : terom@83: def __init__ (self, page_prefix) : terom@83: self.page_prefix = page_prefix terom@83: terom@83: def _test_build (self, mask, url, **args) : terom@83: self.assertEquals(self.config(mask, None).build(self.dummyrequest_page("/index.cgi"), **args), "/index.cgi" + url) terom@83: terom@83: def _test_build_fails (self, err, mask, **args) : terom@83: self.assertRaises(err, self.config(mask, None).build, self.dummyrequest_page("/index.cgi"), **args) terom@82: terom@83: def test_build_empty (self) : terom@83: self._test_build("/", "/") terom@83: terom@83: def test_build_static (self) : terom@83: self._test_build("/foo", "/foo") terom@83: terom@83: def test_build_simple (self) : terom@83: self._test_build("/foo/{bar}", "/foo/x", bar="x") terom@83: terom@83: def test_build_multi (self) : terom@83: self._test_build("/foo/{bar}/{quux}", "/foo/x/y", bar="x", quux="y") terom@83: terom@83: def test_build_missing (self) : terom@83: self._test_build_fails(urltree.URLError, "/foo/{bar}/{quux}", bar="x") terom@83: terom@83: def test_build_unknown (self) : terom@83: self._test_build_fails(urltree.URLError, "/foo/{bar}/{quux}", bar="x", quux="y", frob="???") terom@83: terom@83: def test_build_long (self) : terom@83: self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x/y", bar="x", quux="y") terom@83: terom@83: def test_build_short (self) : terom@83: self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x", quux="b") terom@83: terom@83: def test_build_with_none (self) : terom@83: self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x", quux=None) terom@83: terom@83: def test_build_default (self) : terom@83: self._test_build("/foo/{bar=a}/{quux=b}", "/foo/x", bar="x") terom@83: terom@83: def test_build_qargs (self) : terom@83: self._test_build("/foo/{bar}/?quux", "/foo/x?quux=a", bar="x", quux="a") terom@83: terom@83: def test_build_qargs_default (self) : terom@83: self._test_build("/foo/{bar}/?quux", "/foo/x?quux=a", bar="x", quux="a") terom@83: terom@83: # XXX: this just becomes ...?quux=['a', 'b'] like from str(list) terom@83: # def test_build_qargs_multi_invalid (self) : terom@84: # self._test_build_fails(urltree.URLError, "/foo/{bar}/?quux", bar="x", quux=["a", "b"]) terom@83: terom@83: def test_build_qargs_multi_list (self) : terom@83: self._test_build("/foo/{bar}/?quux:list", "/foo/x?quux=a&quux=b", bar="x", quux=["a", "b"]) terom@83: terom@83: def test_build_qargs_none (self) : terom@83: self._test_build("/foo/{bar}/?quux", "/foo/x", bar="x", quux=None) terom@83: terom@84: class TestTreeBuild (unittest.TestCase) : terom@84: def setUp (self) : terom@84: self.config = urltree.URLConfig(ignore_extra_args=True) terom@83: terom@84: def test_simple_root (self) : terom@84: self.config("/", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[]") terom@83: terom@84: def test_simple_static (self) : terom@84: self.config("/foo/bar", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[bar/[]]]") terom@84: terom@84: def test_multi_static (self) : terom@84: self.config("/foo/bar", None) terom@84: self.config("/foo/quux", None) terom@84: self.config("/asdf", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[bar/[],quux/[]],asdf/[]]") terom@84: terom@84: def test_simple_value (self) : terom@84: self.config("/foo/{bar}", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{bar}/[]]]") terom@84: terom@84: def test_deep (self) : terom@84: self.config("/foo/{cc}/a", None) terom@84: self.config("/foo/{cc}/b", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{cc}/[a/[],b/[]]]]") terom@84: terom@84: def test_deep2 (self) : terom@84: self.config("/foo/{cc}/a/x", None) terom@84: self.config("/foo/{cc}/b", None) terom@84: self.assertEqual(str(urltree.URLTree(self.config).root), "/[foo/[{cc}/[a/[x/[]],b/[]]]]") terom@84: terom@84: def test_ambig_simple (self) : terom@84: self.config("/foo", None) terom@84: self.config("/foo", None) terom@84: terom@84: self.assertRaises(urltree.URLError, urltree.URLTree, self.config) terom@84: terom@84: class TestTreeMatch (unittest.TestCase) : terom@84: def setUp (self) : terom@84: self.config = urltree.URLConfig(ignore_extra_args=True) terom@84: terom@84: self.root =self.config("/", None) terom@84: self.bar = self.config("/bar", None) terom@84: self.quux = self.config("/quux/{xyz}", None) terom@84: self.quux_boo = self.config("/quux/{xyz}/boo/{opt=no}", None) terom@84: self.quux_yes = self.config("/quux/{xyz}/yes", None) terom@84: terom@84: self.tree = urltree.URLTree(self.config) terom@84: terom@84: def _test_match (self, path, url, **values) : terom@84: t_url, t_values = self.tree.match(path) terom@84: terom@84: self.assertEqual(t_url, url) terom@84: terom@84: self.assertEqual(set(v.label.key for v in t_values), set(values)) terom@84: terom@84: for v in t_values : terom@84: self.assertEqual(v.value, values[v.label.key]) terom@84: terom@84: def test_root (self) : terom@84: self._test_match("", self.root) terom@84: terom@84: def test_bar (self) : terom@84: self._test_match("bar", self.bar) terom@84: terom@84: def test_bar_slash (self) : terom@84: self._test_match("bar/", self.bar) terom@84: terom@84: def test_quux (self) : terom@84: self._test_match("quux/a", self.quux, xyz="a") terom@84: terom@84: def test_quux_missing (self) : terom@84: self.assertRaises(urltree.URLError, self._test_match, "quux/", None) terom@84: terom@84: def test_quux_boo (self) : terom@84: self._test_match("quux/a/boo/x", self.quux_boo, xyz="a", opt="x") terom@84: terom@84: def test_quux_default (self) : terom@84: self._test_match("quux/a/boo", self.quux_boo, xyz="a", opt="no") terom@84: terom@84: def test_yes (self) : terom@84: self._test_match("quux/a/yes", self.quux_yes, xyz="a") terom@84: terom@85: class TestTreeHandler (unittest.TestCase) : terom@85: def _build_handler (self, name) : terom@85: def _handler (req, **args) : terom@85: return name, args terom@85: terom@85: return _handler terom@84: terom@85: def setUp (self) : terom@85: self.config = urltree.URLConfig(ignore_extra_args=True) terom@85: terom@85: self.root =self.config("/", self._build_handler('root')) terom@85: self.bar = self.config("/bar", self._build_handler('bar')) terom@85: self.quux = self.config("/quux/{xyz}", self._build_handler('quux')) terom@85: self.quux_boo = self.config("/quux/{xyz}/boo/{opt=no}", self._build_handler('quux_boo')) terom@85: terom@85: self.tree = urltree.URLTree(self.config) terom@85: terom@85: class dummyrequest_page : terom@85: def __init__ (self, page_name, qargs) : terom@85: self.page_name = page_name terom@85: self.qargs = qargs terom@85: terom@85: def get_page_name (self) : return self.page_name terom@85: def get_args (self) : return self.qargs terom@85: terom@85: def _test_handle (self, path, name, qargs={}, **args) : terom@85: req = self.dummyrequest_page(path, qargs.iteritems()) terom@85: terom@85: h_name, h_args = self.tree.handle_request(req) terom@85: terom@85: self.assertEqual(h_name, name) terom@85: self.assertEqual(h_args, args) terom@85: terom@85: def test_root (self) : terom@85: self._test_handle("", 'root') terom@85: terom@85: def test_bar (self) : terom@85: self._test_handle("bar", 'bar') terom@85: terom@85: def test_quux (self) : terom@85: self._test_handle("quux/a", 'quux', xyz='a') terom@85: terom@85: def test_quux_boo (self) : terom@85: self._test_handle("quux/a/boo/b", 'quux_boo', xyz='a', opt='b') terom@85: terom@85: def test_quux_boo_default (self) : terom@85: self._test_handle("quux/a/boo", 'quux_boo', xyz='a', opt='no') terom@85: terom@85: def test_quux_boo_qarg (self) : terom@85: self._test_handle("quux/a/boo", 'quux_boo', dict(opt='yes'), xyz='a', opt='yes') terom@85: