summaryrefslogtreecommitdiff
path: root/tests/admin_views
diff options
context:
space:
mode:
authorTim Graham <timograham@gmail.com>2013-05-27 12:31:49 -0400
committerTim Graham <timograham@gmail.com>2013-05-27 13:29:53 -0400
commit0268aba96bf91de3d172cd6ce2e44c3ce33ef7a0 (patch)
tree7afc83960b5e96e1ace08653d3a13d6fca07c866 /tests/admin_views
parentd194714c0a707773bd470bffb3d67a60e40bb787 (diff)
Fixed #20182 - admin lookup should treat 0 as False for __isnull
Thanks Benjie Chen.
Diffstat (limited to 'tests/admin_views')
-rw-r--r--tests/admin_views/admin.py2
-rw-r--r--tests/admin_views/models.py3
-rw-r--r--tests/admin_views/tests.py53
3 files changed, 55 insertions, 3 deletions
diff --git a/tests/admin_views/admin.py b/tests/admin_views/admin.py
index 4e68ffb8a6..4ab7844ef7 100644
--- a/tests/admin_views/admin.py
+++ b/tests/admin_views/admin.py
@@ -149,7 +149,7 @@ class InquisitionAdmin(admin.ModelAdmin):
class SketchAdmin(admin.ModelAdmin):
- raw_id_fields = ('inquisition',)
+ raw_id_fields = ('inquisition', 'defendant0', 'defendant1')
class FabricAdmin(admin.ModelAdmin):
diff --git a/tests/admin_views/models.py b/tests/admin_views/models.py
index e78dc40a6c..5cc6f6251a 100644
--- a/tests/admin_views/models.py
+++ b/tests/admin_views/models.py
@@ -137,6 +137,7 @@ class Thing(models.Model):
class Actor(models.Model):
name = models.CharField(max_length=50)
age = models.IntegerField()
+ title = models.CharField(max_length=50, null=True)
def __str__(self):
return self.name
@@ -158,6 +159,8 @@ class Sketch(models.Model):
'leader__age': 27,
'expected': False,
})
+ defendant0 = models.ForeignKey(Actor, limit_choices_to={'title__isnull': False}, related_name='as_defendant0')
+ defendant1 = models.ForeignKey(Actor, limit_choices_to={'title__isnull': True}, related_name='as_defendant1')
def __str__(self):
return self.title
diff --git a/tests/admin_views/tests.py b/tests/admin_views/tests.py
index 3485ea353b..106b0a706a 100644
--- a/tests/admin_views/tests.py
+++ b/tests/admin_views/tests.py
@@ -468,8 +468,12 @@ class AdminViewBasicTest(TestCase):
self.assertContains(response, '4 articles')
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'false'})
self.assertContains(response, '3 articles')
+ response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': '0'})
+ self.assertContains(response, '3 articles')
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': 'true'})
self.assertContains(response, '1 article')
+ response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'section__isnull': '1'})
+ self.assertContains(response, '1 article')
def testLogoutAndPasswordChangeURLs(self):
response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit)
@@ -3508,7 +3512,6 @@ class RawIdFieldsTest(TestCase):
def test_limit_choices_to(self):
"""Regression test for 14880"""
- # This includes tests integers, strings and booleans in the lookup query string
actor = Actor.objects.create(name="Palin", age=27)
inquisition1 = Inquisition.objects.create(expected=True,
leader=actor,
@@ -3524,11 +3527,57 @@ class RawIdFieldsTest(TestCase):
# Handle relative links
popup_url = urljoin(response.request['PATH_INFO'], popup_url)
- # Get the popup
+ # Get the popup and verify the correct objects show up in the resulting
+ # page. This step also tests integers, strings and booleans in the
+ # lookup query string; in model we define inquisition field to have a
+ # limit_choices_to option that includes a filter on a string field
+ # (inquisition__actor__name), a filter on an integer field
+ # (inquisition__actor__age), and a filter on a boolean field
+ # (inquisition__expected).
response2 = self.client.get(popup_url)
self.assertContains(response2, "Spain")
self.assertNotContains(response2, "England")
+ def test_limit_choices_to_isnull_false(self):
+ """Regression test for 20182"""
+ Actor.objects.create(name="Palin", age=27)
+ Actor.objects.create(name="Kilbraken", age=50, title="Judge")
+ response = self.client.get('/test_admin/admin/admin_views/sketch/add/')
+ # Find the link
+ m = re.search(br'<a href="([^"]*)"[^>]* id="lookup_id_defendant0"', response.content)
+ self.assertTrue(m) # Got a match
+ popup_url = m.groups()[0].decode().replace("&amp;", "&")
+
+ # Handle relative links
+ popup_url = urljoin(response.request['PATH_INFO'], popup_url)
+ # Get the popup and verify the correct objects show up in the resulting
+ # page. This step tests field__isnull=0 gets parsed correctly from the
+ # lookup query string; in model we define defendant0 field to have a
+ # limit_choices_to option that includes "actor__title__isnull=False".
+ response2 = self.client.get(popup_url)
+ self.assertContains(response2, "Kilbraken")
+ self.assertNotContains(response2, "Palin")
+
+ def test_limit_choices_to_isnull_true(self):
+ """Regression test for 20182"""
+ Actor.objects.create(name="Palin", age=27)
+ Actor.objects.create(name="Kilbraken", age=50, title="Judge")
+ response = self.client.get('/test_admin/admin/admin_views/sketch/add/')
+ # Find the link
+ m = re.search(br'<a href="([^"]*)"[^>]* id="lookup_id_defendant1"', response.content)
+ self.assertTrue(m) # Got a match
+ popup_url = m.groups()[0].decode().replace("&amp;", "&")
+
+ # Handle relative links
+ popup_url = urljoin(response.request['PATH_INFO'], popup_url)
+ # Get the popup and verify the correct objects show up in the resulting
+ # page. This step tests field__isnull=1 gets parsed correctly from the
+ # lookup query string; in model we define defendant1 field to have a
+ # limit_choices_to option that includes "actor__title__isnull=True".
+ response2 = self.client.get(popup_url)
+ self.assertNotContains(response2, "Kilbraken")
+ self.assertContains(response2, "Palin")
+
@override_settings(PASSWORD_HASHERS=('django.contrib.auth.hashers.SHA1PasswordHasher',))
class UserAdminTest(TestCase):