aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_breadcrumbs.py
blob: 780fd5c4df03c138c21c5f49414c240ae2295972 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from __future__ import unicode_literals
from django.conf.urls import patterns, url
from django.test import TestCase
from rest_framework.utils.breadcrumbs import get_breadcrumbs
from rest_framework.views import APIView


class Root(APIView):
    pass


class ResourceRoot(APIView):
    pass


class ResourceInstance(APIView):
    pass


class NestedResourceRoot(APIView):
    pass


class NestedResourceInstance(APIView):
    pass

urlpatterns = patterns(
    '',
    url(r'^$', Root.as_view()),
    url(r'^resource/$', ResourceRoot.as_view()),
    url(r'^resource/(?P<key>[0-9]+)$', ResourceInstance.as_view()),
    url(r'^resource/(?P<key>[0-9]+)/$', NestedResourceRoot.as_view()),
    url(r'^resource/(?P<key>[0-9]+)/(?P<other>[A-Za-z]+)$', NestedResourceInstance.as_view()),
)


class BreadcrumbTests(TestCase):
    """Tests the breadcrumb functionality used by the HTML renderer."""

    urls = 'tests.test_breadcrumbs'

    def test_root_breadcrumbs(self):
        url = '/'
        self.assertEqual(
            get_breadcrumbs(url),
            [('Root', '/')]
        )

    def test_resource_root_breadcrumbs(self):
        url = '/resource/'
        self.assertEqual(
            get_breadcrumbs(url),
            [
                ('Root', '/'),
                ('Resource Root', '/resource/')
            ]
        )

    def test_resource_instance_breadcrumbs(self):
        url = '/resource/123'
        self.assertEqual(
            get_breadcrumbs(url),
            [
                ('Root', '/'),
                ('Resource Root', '/resource/'),
                ('Resource Instance', '/resource/123')
            ]
        )

    def test_nested_resource_breadcrumbs(self):
        url = '/resource/123/'
        self.assertEqual(
            get_breadcrumbs(url),
            [
                ('Root', '/'),
                ('Resource Root', '/resource/'),
                ('Resource Instance', '/resource/123'),
                ('Nested Resource Root', '/resource/123/')
            ]
        )

    def test_nested_resource_instance_breadcrumbs(self):
        url = '/resource/123/abc'
        self.assertEqual(
            get_breadcrumbs(url),
            [
                ('Root', '/'),
                ('Resource Root', '/resource/'),
                ('Resource Instance', '/resource/123'),
                ('Nested Resource Root', '/resource/123/'),
                ('Nested Resource Instance', '/resource/123/abc')
            ]
        )

    def test_broken_url_breadcrumbs_handled_gracefully(self):
        url = '/foobar'
        self.assertEqual(
            get_breadcrumbs(url),
            [('Root', '/')]
        )