tests.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. from __future__ import unicode_literals
  2. from unittest import TestCase
  3. from django.core.urlresolvers import NoReverseMatch
  4. from django.contrib.auth.views import logout
  5. from django.shortcuts import resolve_url
  6. from .models import UnimportantThing
  7. class ResolveUrlTests(TestCase):
  8. """
  9. Tests for the ``resolve_url`` function.
  10. """
  11. def test_url_path(self):
  12. """
  13. Tests that passing a URL path to ``resolve_url`` will result in the
  14. same url.
  15. """
  16. self.assertEqual('/something/', resolve_url('/something/'))
  17. def test_full_url(self):
  18. """
  19. Tests that passing a full URL to ``resolve_url`` will result in the
  20. same url.
  21. """
  22. url = 'http://example.com/'
  23. self.assertEqual(url, resolve_url(url))
  24. def test_model(self):
  25. """
  26. Tests that passing a model to ``resolve_url`` will result in
  27. ``get_absolute_url`` being called on that model instance.
  28. """
  29. m = UnimportantThing(importance=1)
  30. self.assertEqual(m.get_absolute_url(), resolve_url(m))
  31. def test_view_function(self):
  32. """
  33. Tests that passing a view name to ``resolve_url`` will result in the
  34. URL path mapping to that view name.
  35. """
  36. resolved_url = resolve_url(logout)
  37. self.assertEqual('/accounts/logout/', resolved_url)
  38. def test_valid_view_name(self):
  39. """
  40. Tests that passing a view function to ``resolve_url`` will result in
  41. the URL path mapping to that view.
  42. """
  43. resolved_url = resolve_url('django.contrib.auth.views.logout')
  44. self.assertEqual('/accounts/logout/', resolved_url)
  45. def test_domain(self):
  46. """
  47. Tests that passing a domain to ``resolve_url`` returns the same domain.
  48. """
  49. self.assertEqual(resolve_url('example.com'), 'example.com')
  50. def test_non_view_callable_raises_no_reverse_match(self):
  51. """
  52. Tests that passing a non-view callable into ``resolve_url`` raises a
  53. ``NoReverseMatch`` exception.
  54. """
  55. with self.assertRaises(NoReverseMatch):
  56. resolve_url(lambda: 'asdf')