diff options
author | Tim Graham <timograham@gmail.com> | 2014-09-09 08:29:49 -0400 |
---|---|---|
committer | Tim Graham <timograham@gmail.com> | 2014-09-09 11:05:31 -0400 |
commit | c32bc1a7a7bbb3d5bd0a2c11bc77dd5ab1c32fbc (patch) | |
tree | 378c2e4d2ee2390430fadc759e1ecb8852e84aa9 /tests/absolute_url_overrides | |
parent | e5ae03fd5a887a7038fb6878ffeec85b92829d1b (diff) | |
download | django-c32bc1a7a7bbb3d5bd0a2c11bc77dd5ab1c32fbc.tar.gz |
Fixed #11775 -- Made ABSOLUTE_URL_OVERRIDES work with models that don't define get_absolute_url().
Thanks jukvalim for the report and initial patch,
and Preston Timmons for review.
Diffstat (limited to 'tests/absolute_url_overrides')
-rw-r--r-- | tests/absolute_url_overrides/__init__.py | 0 | ||||
-rw-r--r-- | tests/absolute_url_overrides/tests.py | 54 |
2 files changed, 54 insertions, 0 deletions
diff --git a/tests/absolute_url_overrides/__init__.py b/tests/absolute_url_overrides/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/absolute_url_overrides/__init__.py diff --git a/tests/absolute_url_overrides/tests.py b/tests/absolute_url_overrides/tests.py new file mode 100644 index 0000000000..b57374cb6d --- /dev/null +++ b/tests/absolute_url_overrides/tests.py @@ -0,0 +1,54 @@ +from django.db import models +from django.test import TestCase + + +class AbsoluteUrlOverrideTests(TestCase): + + def test_get_absolute_url(self): + """ + get_absolute_url() functions as a normal method. + """ + get_absolute_url = lambda o: '/test-a/%s/' % o.pk + TestA = self._create_model_class('TestA', get_absolute_url) + + self.assertTrue(hasattr(TestA, 'get_absolute_url')) + obj = TestA(pk=1, name='Foo') + self.assertEqual('/test-a/%s/' % obj.pk, obj.get_absolute_url()) + + def test_override_get_absolute_url(self): + """ + ABSOLUTE_URL_OVERRIDES should override get_absolute_url(). + """ + get_absolute_url = lambda o: '/test-b/%s/' % o.pk + with self.settings( + ABSOLUTE_URL_OVERRIDES={ + 'absolute_url_overrides.testb': lambda o: '/overridden-test-b/%s/' % o.pk, + }, + ): + TestB = self._create_model_class('TestB', get_absolute_url) + obj = TestB(pk=1, name='Foo') + self.assertEqual('/overridden-test-b/%s/' % obj.pk, obj.get_absolute_url()) + + def test_insert_get_absolute_url(self): + """ + ABSOLUTE_URL_OVERRIDES should work even if the model doesn't have a + get_absolute_url() method. + """ + with self.settings( + ABSOLUTE_URL_OVERRIDES={ + 'absolute_url_overrides.testc': lambda o: '/test-c/%s/' % o.pk, + }, + ): + TestC = self._create_model_class('TestC') + obj = TestC(pk=1, name='Foo') + self.assertEqual('/test-c/%s/' % obj.pk, obj.get_absolute_url()) + + def _create_model_class(self, class_name, get_absolute_url_method=None): + attrs = { + 'name': models.CharField(max_length=50), + '__module__': 'absolute_url_overrides', + } + if get_absolute_url_method: + attrs['get_absolute_url'] = get_absolute_url_method + + return type(class_name, (models.Model,), attrs) |