tests/test_urltree.py
changeset 82 d636dfcbd519
parent 81 847da3c265b5
child 83 69312afad653
equal deleted inserted replaced
81:847da3c265b5 82:d636dfcbd519
    58         self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str'])
    58         self._test_parse("{foo:str}", urltree.SimpleValueLabel, key="foo", type_name="str", type=urltree.URLConfig.BUILTIN_TYPES['str'])
    59 
    59 
    60     def test_parse_value_type_int (self) :
    60     def test_parse_value_type_int (self) :
    61         self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int'])
    61         self._test_parse("{foo:int}", urltree.SimpleValueLabel, key="foo", type_name="int", type=urltree.URLConfig.BUILTIN_TYPES['int'])
    62     
    62     
       
    63     def test_parse_invalid_type (self) :
       
    64         self.assertRaises(KeyError, self._test_parse, "{foo:bar}", None)
       
    65 
    63 class TestEmptyLabel (unittest.TestCase) :
    66 class TestEmptyLabel (unittest.TestCase) :
    64     def setUp (self) :
    67     def setUp (self) :
    65         self.label = urltree.EmptyLabel()
    68         self.label = urltree.EmptyLabel()
    66 
    69 
    67     def test_eq (self) :
    70     def test_eq (self) :
   250     def test_init (self) :
   253     def test_init (self) :
   251         urltree.URLConfig(type_dict=dict(foo=None), ignore_extra_args=True)
   254         urltree.URLConfig(type_dict=dict(foo=None), ignore_extra_args=True)
   252 
   255 
   253     def test_get_type (self) :
   256     def test_get_type (self) :
   254         self.assertEquals(urltree.URLConfig(dict(foo='xxx')).get_type('foo'), 'xxx')
   257         self.assertEquals(urltree.URLConfig(dict(foo='xxx')).get_type('foo'), 'xxx')
       
   258     
       
   259     def test_get_type_invalid (self) :
       
   260         self.assertRaises(KeyError, urltree.URLConfig(dict(foo='xxx')).get_type, 'xxx')
   255 
   261 
   256     def test_call (self) :
   262     def test_call (self) :
   257         config = urltree.URLConfig()
   263         config = urltree.URLConfig()
   258         url = config("foo", None)
   264         url = config("foo", None)
   259 
   265 
   268         urls = list(config)
   274         urls = list(config)
   269 
   275 
   270         self.assertTrue(urls[0].url_mask == "foo1")
   276         self.assertTrue(urls[0].url_mask == "foo1")
   271         self.assertTrue(urls[1].url_mask == "foo2")
   277         self.assertTrue(urls[1].url_mask == "foo2")
   272 
   278 
       
   279 class TestURL (unittest.TestCase) :
       
   280     def setUp (self) :
       
   281         self.config = urltree.URLConfig(ignore_extra_args=True)
       
   282         self.config_strict = urltree.URLConfig(ignore_extra_args=False)
       
   283     
       
   284     def _test_label_path (self, mask, *path, **qargs) :
       
   285         url = self.config(mask, None)
       
   286         
       
   287         # right label path
       
   288         self.assertEquals(url.label_path, list(path))
       
   289         
       
   290         # right qargs keys
       
   291         self.assertEquals(set(url.query_args), set(qargs))
       
   292         
       
   293         # right qargs values
       
   294         for key, value in qargs.iteritems() :
       
   295             self.assertEquals(url.query_args[key], value)
       
   296 
       
   297     def test_label_path_empty (self) :
       
   298         self._test_label_path("", urltree.EmptyLabel())
       
   299     
       
   300     def test_label_path_root (self) :
       
   301         self._test_label_path("/", urltree.EmptyLabel())
       
   302 
       
   303     def test_label_path_static (self) :
       
   304         self._test_label_path("/foo", urltree.StaticLabel("foo"))
       
   305 
       
   306     def test_label_path_static2 (self) :
       
   307         self._test_label_path("/foo/bar/", urltree.StaticLabel("foo"), urltree.StaticLabel("bar"), urltree.EmptyLabel())
       
   308     
       
   309     def test_label_path_mix (self) :
       
   310         self._test_label_path("/foo/{bar}", urltree.StaticLabel("foo"), urltree.SimpleValueLabel("bar", None, None, None))
       
   311     
       
   312 #    def test_query_args_root_empty (self) :
       
   313 #        self._test_label_path("/?", urltree.EmptyLabel())
       
   314 
       
   315     def test_query_args_simple (self) :
       
   316         self._test_label_path("/x/?foo", urltree.StaticLabel("x"), foo=(self.config.get_type(None), None))
       
   317 
       
   318     def test_query_args_multi (self) :
       
   319         self._test_label_path("/x/?foo=0&bar&tee:int=&eee:int", urltree.StaticLabel("x"), 
       
   320             foo = (self.config.get_type(None), "0"),
       
   321             bar = (self.config.get_type(None), None),
       
   322             tee = (self.config.get_type('int'), ''),
       
   323             eee = (self.config.get_type('int'), None),
       
   324         )
       
   325     
       
   326     def test_label_path_mutate (self) :
       
   327         l = self.config("xxx", None)
       
   328 
       
   329         lp = l.get_label_path()
       
   330 
       
   331         lp.pop(0)
       
   332 
       
   333         self.assertTrue(len(l.label_path) > len(lp))
       
   334     
       
   335     def _setup_handler (self) :
       
   336         def _handler (req, **kwargs) :
       
   337             return kwargs
       
   338 
       
   339         return _handler
       
   340     
       
   341     def _setup_execute (self, mask, config) :
       
   342         _handler = self._setup_handler()
       
   343 
       
   344         url = config(mask, _handler)
       
   345 
       
   346         return url
       
   347     
       
   348     class dummyrequest :
       
   349         def __init__ (self, qargs) : self.qargs = qargs
       
   350         def get_args (self) : return self.qargs
       
   351     
       
   352     class dummy_label :
       
   353         def __init__ (self, key, type) :
       
   354             self.key = key
       
   355             self.type = type
       
   356 
       
   357     class dummy_labelvalue :
       
   358         def __init__ (self, key, value, is_default, type) : 
       
   359             self.label = TestURL.dummy_label(key, type)
       
   360             self.value = value
       
   361             self.is_default = is_default
       
   362 
       
   363     def _test_execute (self, mask, values={}, qargs={}, qlist=[], config=None) :
       
   364         if not config :
       
   365             config = self.config
       
   366 
       
   367         # setup
       
   368         url = self._setup_execute(mask, config)
       
   369         req = self.dummyrequest(qargs.items() + qlist)
       
   370         values = [self.dummy_labelvalue(k, v, d, config.get_type()) for k, (v, d) in values.iteritems()]
       
   371 
       
   372         # exec
       
   373         out_args = url.execute(req, values)
       
   374         
       
   375         return out_args
       
   376         
       
   377     def test_execute_empty (self) :
       
   378         self.assertEquals(set(self._test_execute("/")), set())
       
   379 
       
   380     def test_execute_plain (self) :
       
   381         self.assertEquals(set(self._test_execute("/foo")), set())
       
   382 
       
   383     def test_execute_simple (self) :
       
   384         self.assertEquals(self._test_execute("/foo/{bar}", dict(bar=(0, False))), dict(bar=0))
       
   385 
       
   386     def test_execute_multi (self) :
       
   387         self.assertEquals(self._test_execute("/foo/{bar}/{quux}", dict(bar=(1, False), quux=(2, False))), dict(bar=1, quux=2))
       
   388 
       
   389     def test_execute_default (self) :
       
   390         self.assertEquals(self._test_execute("/foo/{bar=0}", dict(bar=("0", True))), dict(bar="0"))
       
   391     
       
   392     def test_execute_qargs_default (self) :
       
   393         self.assertEquals(self._test_execute("/{foo}/?bar=0", dict(foo=("x", False))), dict(foo="x", bar="0"))
       
   394     
       
   395     def test_execute_qargs_default_type (self) :
       
   396         self.assertEquals(self._test_execute("/{foo}/?bar:int=0", dict(foo=("x", False))), dict(foo="x", bar=0))
       
   397 
       
   398     def test_execute_qargs_default_none (self) :
       
   399         self.assertEquals(self._test_execute("/{foo}/?bar=", dict(foo=("x", False))), dict(foo="x"))
       
   400 
       
   401     def test_execute_qargs_missing (self) :
       
   402         self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)))
       
   403 
       
   404     def test_execute_qargs_invalid (self) :
       
   405         self.assertRaises(ValueError, self._test_execute, "/{foo}/?bar:int", dict(foo=("x", False)), dict(bar="x"))
       
   406 
       
   407     def test_execute_qargs_simple (self) :
       
   408         self.assertEquals(self._test_execute("/{foo}/?bar", dict(foo=("x", False)), dict(bar="y")), dict(foo="x", bar="y"))
       
   409 
       
   410     def test_execute_qargs_novalue (self) :
       
   411         self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), dict(bar=''))
       
   412 
       
   413     def test_execute_qargs_multi_invalid (self) :
       
   414         self.assertRaises(urltree.URLError, self._test_execute, "/{foo}/?bar", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')])
       
   415 
       
   416     def test_execute_qargs_multi_list (self) :
       
   417         self.assertEqual(self._test_execute("/{foo}/?bar:list", dict(foo=("x", False)), qlist=[('bar', 'a'), ('bar', 'b')]), dict(foo='x', bar=['a', 'b']))
       
   418 
       
   419     def test_execute_qarg_override_strict (self) :
       
   420         self.assertRaises(urltree.URLError, self._test_execute, "/{foo}", dict(foo=("x1", False)), dict(foo="x2"), config=self.config_strict)
       
   421 
       
   422     def test_execute_qarg_override_ignore (self) :
       
   423         self.assertEqual(self._test_execute("/{foo}", dict(foo=("x1", False)), dict(foo="x2")), dict(foo="x1"))
       
   424         
       
   425     def test_execute_qarg_override_ok (self) :
       
   426         self.assertEqual(self._test_execute("/{foo=x1}", dict(foo=("x1", True)), dict(foo="x2")), dict(foo="x2"))
       
   427