tests.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from __future__ import unicode_literals
  2. from django.contrib.auth.views import logout
  3. from django.core.urlresolvers import NoReverseMatch, reverse_lazy
  4. from django.shortcuts import resolve_url
  5. from django.test import SimpleTestCase, override_settings
  6. from django.utils import six
  7. from .models import UnimportantThing
  8. @override_settings(ROOT_URLCONF='resolve_url.urls')
  9. class ResolveUrlTests(SimpleTestCase):
  10. """
  11. Tests for the ``resolve_url`` function.
  12. """
  13. def test_url_path(self):
  14. """
  15. Tests that passing a URL path to ``resolve_url`` will result in the
  16. same url.
  17. """
  18. self.assertEqual('/something/', resolve_url('/something/'))
  19. def test_relative_path(self):
  20. """
  21. Tests that passing a relative URL path to ``resolve_url`` will result
  22. in the same url.
  23. """
  24. self.assertEqual('../', resolve_url('../'))
  25. self.assertEqual('../relative/', resolve_url('../relative/'))
  26. self.assertEqual('./', resolve_url('./'))
  27. self.assertEqual('./relative/', resolve_url('./relative/'))
  28. def test_full_url(self):
  29. """
  30. Tests that passing a full URL to ``resolve_url`` will result in the
  31. same url.
  32. """
  33. url = 'http://example.com/'
  34. self.assertEqual(url, resolve_url(url))
  35. def test_model(self):
  36. """
  37. Tests that passing a model to ``resolve_url`` will result in
  38. ``get_absolute_url`` being called on that model instance.
  39. """
  40. m = UnimportantThing(importance=1)
  41. self.assertEqual(m.get_absolute_url(), resolve_url(m))
  42. def test_view_function(self):
  43. """
  44. Tests that passing a view function to ``resolve_url`` will result in
  45. the URL path mapping to that view name.
  46. """
  47. resolved_url = resolve_url(logout)
  48. self.assertEqual('/accounts/logout/', resolved_url)
  49. def test_lazy_reverse(self):
  50. """
  51. Tests that passing the result of reverse_lazy is resolved to a real URL
  52. string.
  53. """
  54. resolved_url = resolve_url(reverse_lazy('logout'))
  55. self.assertIsInstance(resolved_url, six.text_type)
  56. self.assertEqual('/accounts/logout/', resolved_url)
  57. def test_valid_view_name(self):
  58. """
  59. Tests that passing a view name to ``resolve_url`` will result in the
  60. URL path mapping to that view.
  61. """
  62. resolved_url = resolve_url('logout')
  63. self.assertEqual('/accounts/logout/', resolved_url)
  64. def test_domain(self):
  65. """
  66. Tests that passing a domain to ``resolve_url`` returns the same domain.
  67. """
  68. self.assertEqual(resolve_url('example.com'), 'example.com')
  69. def test_non_view_callable_raises_no_reverse_match(self):
  70. """
  71. Tests that passing a non-view callable into ``resolve_url`` raises a
  72. ``NoReverseMatch`` exception.
  73. """
  74. with self.assertRaises(NoReverseMatch):
  75. resolve_url(lambda: 'asdf')