tests.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from __future__ import unicode_literals
  2. import warnings
  3. from django.core.urlresolvers import NoReverseMatch
  4. from django.contrib.auth.views import logout
  5. from django.shortcuts import resolve_url
  6. from django.test import TestCase, override_settings
  7. from django.utils.deprecation import RemovedInDjango20Warning
  8. from .models import UnimportantThing
  9. @override_settings(ROOT_URLCONF='resolve_url.urls')
  10. class ResolveUrlTests(TestCase):
  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_valid_view_name(self):
  51. """
  52. Tests that passing a view function to ``resolve_url`` will result in
  53. the URL path mapping to that view.
  54. """
  55. with warnings.catch_warnings():
  56. warnings.filterwarnings("ignore", category=RemovedInDjango20Warning)
  57. resolved_url = resolve_url('django.contrib.auth.views.logout')
  58. self.assertEqual('/accounts/logout/', resolved_url)
  59. def test_domain(self):
  60. """
  61. Tests that passing a domain to ``resolve_url`` returns the same domain.
  62. """
  63. self.assertEqual(resolve_url('example.com'), 'example.com')
  64. def test_non_view_callable_raises_no_reverse_match(self):
  65. """
  66. Tests that passing a non-view callable into ``resolve_url`` raises a
  67. ``NoReverseMatch`` exception.
  68. """
  69. with self.assertRaises(NoReverseMatch):
  70. resolve_url(lambda: 'asdf')