tests.py 3.1 KB

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