Esempio n. 1
0
class MultiviewTests(unittest.TestCase):
    def setUp(self):
        # Patterns with a "catch all" view (thing) at the end.
        self.patterns_catchall = RegexURLResolver(
            '^/',
            patterns(
                '',
                multiurl(
                    url(r'^(\w+)/$', person, name='person'),
                    url(r'^(\w+)/$', place, name='place'),
                    url(r'^(\w+)/$', thing, name='thing'),
                )))

        # Patterns with no "catch all" - last view could still raise ContinueResolving.
        self.patterns_no_fallthrough = RegexURLResolver(
            '^/',
            patterns(
                '',
                multiurl(
                    url(r'^(\w+)/$', person, name='person'),
                    url(r'^(\w+)/$', place, name='place'),
                )))

    def test_resolve_match_first(self):
        m = self.patterns_catchall.resolve('/jane/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Person: Jane Doe")

    def test_resolve_match_middle(self):
        m = self.patterns_catchall.resolve('/sf/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Place: San Francisco")

    def test_resolve_match_last(self):
        m = self.patterns_catchall.resolve('/bacon/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Thing: Bacon")

    def test_resolve_match_faillthrough(self):
        m = self.patterns_no_fallthrough.resolve('/bacon/')
        with self.assertRaises(Resolver404):
            m.func(request=None, *m.args, **m.kwargs)

    def test_no_match(self):
        with self.assertRaises(Resolver404):
            self.patterns_catchall.resolve('/eggs/and/bacon/')

    def test_reverse(self):
        self.assertEqual('joe/',
                         self.patterns_catchall.reverse('person', 'joe'))
        self.assertEqual('joe/',
                         self.patterns_catchall.reverse('place', 'joe'))
        self.assertEqual('joe/',
                         self.patterns_catchall.reverse('thing', 'joe'))
        with self.assertRaises(NoReverseMatch):
            self.patterns_catchall.reverse('person')
        with self.assertRaises(NoReverseMatch):
            self.patterns_catchall.reverse('argh', 'xyz')
Esempio n. 2
0
class MultiviewTests(unittest.TestCase):
    def setUp(self):
        # Patterns with a "catch all" view (thing) at the end.
        self.patterns_catchall = RegexURLResolver('^/', patterns('',
            multiurl(
                url(r'^(\w+)/$', person, name='person'),
                url(r'^(\w+)/$', place, name='place'),
                url(r'^(\w+)/$', thing, name='thing'),
            )
        ))

        # Patterns with no "catch all" - last view could still raise ContinueResolving.
        self.patterns_no_fallthrough = RegexURLResolver('^/', patterns('',
            multiurl(
                url(r'^(\w+)/$', person, name='person'),
                url(r'^(\w+)/$', place, name='place'),
            )
        ))

    def test_resolve_match_first(self):
        m = self.patterns_catchall.resolve('/jane/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Person: Jane Doe")

    def test_resolve_match_middle(self):
        m = self.patterns_catchall.resolve('/sf/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Place: San Francisco")

    def test_resolve_match_last(self):
        m = self.patterns_catchall.resolve('/bacon/')
        response = m.func(request=None, *m.args, **m.kwargs)
        self.assertEqual(response.content, b"Thing: Bacon")

    def test_resolve_match_faillthrough(self):
        m = self.patterns_no_fallthrough.resolve('/bacon/')
        with self.assertRaises(Resolver404):
            m.func(request=None, *m.args, **m.kwargs)

    def test_no_match(self):
        with self.assertRaises(Resolver404):
            self.patterns_catchall.resolve('/eggs/and/bacon/')

    def test_reverse(self):
        self.assertEqual('joe/', self.patterns_catchall.reverse('person', 'joe'))
        self.assertEqual('joe/', self.patterns_catchall.reverse('place', 'joe'))
        self.assertEqual('joe/', self.patterns_catchall.reverse('thing', 'joe'))
        with self.assertRaises(NoReverseMatch):
            self.patterns_catchall.reverse('person')
        with self.assertRaises(NoReverseMatch):
            self.patterns_catchall.reverse('argh', 'xyz')
Esempio n. 3
0
 def reverse_subpage(self, name, *args, **kwargs):
     """
     This method does the same job as Djangos' built in "urlresolvers.reverse()" function for subpage urlconfs.
     """
     resolver = RegexURLResolver(r'^', self.get_subpage_urls())
     return self.url + resolver.reverse(name, *args, **kwargs)
Esempio n. 4
0
 def reverse_subpage(self, name, *args, **kwargs):
     """
     This method does the same job as Djangos' built in "urlresolvers.reverse()" function for subpage urlconfs.
     """
     resolver = RegexURLResolver(r'^', self.get_subpage_urls())
     return self.url + resolver.reverse(name, *args, **kwargs)