tests for the resolveurl function passing a url path to resolveurl results in the same url passing a relative url path to resolveurl results in the same url passing a full url to resolveurl results in the same url passing a model to resolveurl results in getabsoluteurl being called on that model instance passing a view function to resolveurl results in the url path mapping to that view name passing the result of reverselazy is resolved to a real url string passing a view name to resolveurl results in the url path mapping to that view passing a domain to resolveurl returns the same domain passing a nonview callable into resolveurl raises a noreversematch exception tests for the resolve_url function passing a url path to resolve_url results in the same url passing a relative url path to resolve_url results in the same url passing a full url to resolve_url results in the same url passing a model to resolve_url results in get_absolute_url being called on that model instance passing a view function to resolve_url results in the url path mapping to that view name passing the result of reverse_lazy is resolved to a real url string passing a view name to resolve_url results in the url path mapping to that view passing a domain to resolve_url returns the same domain passing a non view callable into resolve_url raises a noreversematch exception
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):
    

    def test_url_path(self):
        
        self.assertEqual("/something/", resolve_url("/something/"))

    def test_relative_path(self):
        
        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):
        
        url = "http://example.com/"
        self.assertEqual(url, resolve_url(url))

    def test_model(self):
        
        m = UnimportantThing(importance=1)
        self.assertEqual(m.get_absolute_url(), resolve_url(m))

    def test_view_function(self):
        
        resolved_url = resolve_url(some_view)
        self.assertEqual("/some-url/", resolved_url)

    def test_lazy_reverse(self):
        
        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):
        
        resolved_url = resolve_url("some-view")
        self.assertEqual("/some-url/", resolved_url)

    def test_domain(self):
        
        self.assertEqual(resolve_url("example.com"), "example.com")

    def test_non_view_callable_raises_no_reverse_match(self):
        
        with self.assertRaises(NoReverseMatch):
            resolve_url(lambda: "asdf")