Exemplo n.º 1
0
    def test_match(self):
        "Test matching known URLs."

        mapper = UrlMapper(rootloc='/demo')
        root = demoapp.create_application(mapper)
        match = mapper.match

        tests = (
            ('@@ImSpecial', '/demo/altit', {}),
            ('@@SimpleThought', '/demo/fold/think', {}),
            ('@@IntegerComponent', '/demo/formatted/00001042', {
                'uid': 1042
            }),
            ('@@LeafPlusOneComponent', '/demo/lcomp/bli', {
                'comp': 'bli'
            }),
            ('@@UserData', '/demo/users/rachel/data/school', {
                'username': '******',
                'userdata': 'school'
            }),
            ('@@LeafPlusOneComponent', 'http://furius.ca/demo/lcomp/bli', {
                'comp': 'bli'
            }),
        )

        for resid, url, expected in tests:
            assertEquals(match(resid, url), expected)
Exemplo n.º 2
0
    def test_optparam(self):
        "Test with optional parameters."

        mapper = UrlMapper(rootloc='/demo')
        mapper, root = demoapp.create_application(mapper)
        #for l in mapper.render():
        #    print l
        mapurl = mapper.mapurl

        # Test number of arguments.
        assertEquals(mapurl('@@OptionalParams'), '/demo/wopts')
        assertRaises(RanvierError, mapurl, '@@OptionalParams', 2)
        assertRaises(RanvierError, mapurl, '@@OptionalParams', 2, 3)
        assertRaises(RanvierError, mapurl, '@@OptionalParams', 2, 3, 4)

        # Try with optional parameter that does not exist.
        assertRaises(RanvierError, mapurl, '@@OptionalParams', symbol='lambda')

        # Test results of optional parameters.
        assertEquals(mapurl('@@OptionalParams', cat='Miaouw'),
                     '/demo/wopts?cat=Miaouw')
        assertEquals(mapurl('@@OptionalParams', dog='Wouf'),
                     '/demo/wopts?dog=Wouf')
        assertEquals(mapurl('@@OptionalParams', cat='Miawou', dog='Wouf'),
                     '/demo/wopts?dog=Wouf&cat=Miawou')

        # Test with formatting.
        assertEquals(mapurl('@@OptionalParams', nbanimals=42),
                     '/demo/wopts?nbanimals=00042')

        # Test escaping.
        assertEquals(mapurl('@@OptionalParams', dog='Wouf Wouf!'),
                     '/demo/wopts?dog=Wouf+Wouf%21')
Exemplo n.º 3
0
    def test_render_reload(self):
        "Testing reloading the mapper from a rendered rendition."

        # Render the mapper to a set of lines
        mapper = UrlMapper()
        root = demoapp.create_application(mapper)
        lines = mapper.render()
        rendered = os.linesep.join(lines)

        # Reload the mapper from these lines.
        loaded_mapper = UrlMapper.load(lines)
        self._test_backmaps(loaded_mapper)

        # Round-trip: render and compare to the original file.
        loaded_lines = loaded_mapper.render()
        self.assertEquals(loaded_lines, lines)

        # Load from a file.
        fromfile_mapper = UrlMapper.urlload('example-resources.txt')
Exemplo n.º 4
0
    def test_template(self):
        "Test conversion to string template."

        mapper = UrlMapper(rootloc='/demo')
        root = demoapp.create_application(mapper)
        mapurl, mapurl_pattern = mapper.mapurl, mapper.mapurl_pattern

        tests = (
            ('@@ExternalExample', (), 'http://paulgraham.com/',
             'http://paulgraham.com/'),
            ('@@Root', (), '/demo/', '/demo/'),
            ('@@ImSpecial', (), '/demo/altit', '/demo/altit'),
            ('@@Atocha', (), '/atocha/index.html', '/atocha/index.html'),
            ('@@AnswerBabbler', (), '/demo/deleg', '/demo/deleg'),
            ('@@DemoFolderWithMenu', (), '/demo/fold/', '/demo/fold/'),
            ('@@SimpleGreed', (), '/demo/fold/greed', '/demo/fold/greed'),
            ('@@SimpleHamming', (), '/demo/fold/ham', '/demo/fold/ham'),
            ('@@SimpleThought', (), '/demo/fold/think', '/demo/fold/think'),
            ('@@IntegerComponent', (1042, ), '/demo/formatted/00001042',
             '/demo/formatted/(uid%08d)'),
            ('@@Home', (), '/demo/home', '/demo/home'),
            ('@@InternalRedirectTest', (), '/demo/internalredir',
             '/demo/internalredir'),
            ('@@LeafPlusOneComponent', ('bli', ), '/demo/lcomp/bli',
             '/demo/lcomp/(comp)'),
            ('@@DemoPrettyEnumResource', (), '/demo/prettyres',
             '/demo/prettyres'),
            ('@@RedirectTest', (), '/demo/redirtest', '/demo/redirtest'),
            ('@@EnumResource', (), '/demo/resources', '/demo/resources'),
            ('@@Stylesheet', (), '/demo/style.css', '/demo/style.css'),
            ('@@UserData', ('rachel',
                            'school'), '/demo/users/rachel/data/school',
             '/demo/users/(username)/data/(userdata)'),
            ('@@PrintName', ('rachel', ), '/demo/users/rachel/name',
             '/demo/users/(username)/name'),
            ('@@PrintUsername', ('rachel', ), '/demo/users/rachel/username',
             '/demo/users/(username)/username'),
        )

        for resid, args, rendered, pattern in tests:
            assertEquals(mapurl(resid, *args), rendered)
            assertEquals(mapurl_pattern(resid), pattern)
Exemplo n.º 5
0
 def test_backmaps(self):
     "Testing backmapping URLs."
     # Create a resource tree.
     mapper = UrlMapper()
     root = demoapp.create_application(mapper)
     return self._test_backmaps(mapper)