2012-09-09 06:55:29 +08:00
|
|
|
from django.shortcuts import resolve_url
|
2015-08-18 01:45:07 +08:00
|
|
|
from django.test import SimpleTestCase, override_settings
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import NoReverseMatch, reverse_lazy
|
2012-09-09 06:55:29 +08:00
|
|
|
|
|
|
|
from .models import UnimportantThing
|
2016-05-15 23:28:00 +08:00
|
|
|
from .urls import some_view
|
2012-09-09 06:55:29 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='resolve_url.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ResolveUrlTests(SimpleTestCase):
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Tests for the resolve_url() function.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
|
|
|
|
def test_url_path(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a URL path to resolve_url() results in the same url.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual('/something/', resolve_url('/something/'))
|
|
|
|
|
2014-02-14 11:48:44 +08:00
|
|
|
def test_relative_path(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a relative URL path to resolve_url() results in the same url.
|
2014-02-14 11:48:44 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual('../', resolve_url('../'))
|
|
|
|
self.assertEqual('../relative/', resolve_url('../relative/'))
|
|
|
|
self.assertEqual('./', resolve_url('./'))
|
|
|
|
self.assertEqual('./relative/', resolve_url('./relative/'))
|
|
|
|
|
2012-09-09 06:55:29 +08:00
|
|
|
def test_full_url(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a full URL to resolve_url() results in the same url.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
url = 'http://example.com/'
|
|
|
|
self.assertEqual(url, resolve_url(url))
|
|
|
|
|
|
|
|
def test_model(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a model to resolve_url() results in get_absolute_url() being
|
|
|
|
called on that model instance.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
m = UnimportantThing(importance=1)
|
|
|
|
self.assertEqual(m.get_absolute_url(), resolve_url(m))
|
|
|
|
|
|
|
|
def test_view_function(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a view function to resolve_url() results in the URL path
|
|
|
|
mapping to that view name.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
2016-05-15 23:28:00 +08:00
|
|
|
resolved_url = resolve_url(some_view)
|
|
|
|
self.assertEqual('/some-url/', resolved_url)
|
2012-09-09 06:55:29 +08:00
|
|
|
|
2015-01-10 05:18:34 +08:00
|
|
|
def test_lazy_reverse(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing the result of reverse_lazy is resolved to a real URL
|
2015-01-10 05:18:34 +08:00
|
|
|
string.
|
|
|
|
"""
|
2016-05-15 23:28:00 +08:00
|
|
|
resolved_url = resolve_url(reverse_lazy('some-view'))
|
2016-12-29 23:27:49 +08:00
|
|
|
self.assertIsInstance(resolved_url, str)
|
2016-05-15 23:28:00 +08:00
|
|
|
self.assertEqual('/some-url/', resolved_url)
|
2015-01-10 05:18:34 +08:00
|
|
|
|
2012-09-09 06:55:29 +08:00
|
|
|
def test_valid_view_name(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a view name to resolve_url() results in the URL path mapping
|
|
|
|
to that view.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
2016-05-15 23:28:00 +08:00
|
|
|
resolved_url = resolve_url('some-view')
|
|
|
|
self.assertEqual('/some-url/', resolved_url)
|
2012-09-09 06:55:29 +08:00
|
|
|
|
|
|
|
def test_domain(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a domain to resolve_url() returns the same domain.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
self.assertEqual(resolve_url('example.com'), 'example.com')
|
|
|
|
|
|
|
|
def test_non_view_callable_raises_no_reverse_match(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
Passing a non-view callable into resolve_url() raises a
|
|
|
|
NoReverseMatch exception.
|
2012-09-09 06:55:29 +08:00
|
|
|
"""
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
resolve_url(lambda: 'asdf')
|