summaryrefslogtreecommitdiff
path: root/tests/resolve_url/tests.py
blob: 26fe783bf36a761fca4151350aac4ca48a160c6f (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
from django.shortcuts import resolve_url
from django.test import SimpleTestCase, override_settings
from django.urls import NoReverseMatch, reverse_lazy

from .models import UnimportantThing
from .urls import some_view


@override_settings(ROOT_URLCONF="resolve_url.urls")
class ResolveUrlTests(SimpleTestCase):
    """
    Tests for the resolve_url() function.
    """

    def test_url_path(self):
        """
        Passing a URL path to resolve_url() results in the same url.
        """
        self.assertEqual("/something/", resolve_url("/something/"))

    def test_relative_path(self):
        """
        Passing a relative URL path to resolve_url() results in the same url.
        """
        self.assertEqual("../", resolve_url("../"))
        self.assertEqual("../relative/", resolve_url("../relative/"))
        self.assertEqual("./", resolve_url("./"))
        self.assertEqual("./relative/", resolve_url("./relative/"))

    def test_full_url(self):
        """
        Passing a full URL to resolve_url() results in the same url.
        """
        url = "http://example.com/"
        self.assertEqual(url, resolve_url(url))

    def test_model(self):
        """
        Passing a model to resolve_url() results in get_absolute_url() being
        called on that model instance.
        """
        m = UnimportantThing(importance=1)
        self.assertEqual(m.get_absolute_url(), resolve_url(m))

    def test_view_function(self):
        """
        Passing a view function to resolve_url() results in the URL path
        mapping to that view name.
        """
        resolved_url = resolve_url(some_view)
        self.assertEqual("/some-url/", resolved_url)

    def test_lazy_reverse(self):
        """
        Passing the result of reverse_lazy is resolved to a real URL
        string.
        """
        resolved_url = resolve_url(reverse_lazy("some-view"))
        self.assertIsInstance(resolved_url, str)
        self.assertEqual("/some-url/", resolved_url)

    def test_valid_view_name(self):
        """
        Passing a view name to resolve_url() results in the URL path mapping
        to that view.
        """
        resolved_url = resolve_url("some-view")
        self.assertEqual("/some-url/", resolved_url)

    def test_domain(self):
        """
        Passing a domain to resolve_url() returns the same domain.
        """
        self.assertEqual(resolve_url("example.com"), "example.com")

    def test_non_view_callable_raises_no_reverse_match(self):
        """
        Passing a non-view callable into resolve_url() raises a
        NoReverseMatch exception.
        """
        with self.assertRaises(NoReverseMatch):
            resolve_url(lambda: "asdf")