summaryrefslogtreecommitdiff
path: root/tests/null_fk
diff options
context:
space:
mode:
authorFlorian Apolloner <florian@apolloner.eu>2013-02-26 09:53:47 +0100
committerFlorian Apolloner <florian@apolloner.eu>2013-02-26 14:36:57 +0100
commit89f40e36246100df6a11316c31a76712ebc6c501 (patch)
tree6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/null_fk
parentb3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff)
downloaddjango-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/null_fk')
-rw-r--r--tests/null_fk/__init__.py0
-rw-r--r--tests/null_fk/models.py50
-rw-r--r--tests/null_fk/tests.py69
3 files changed, 119 insertions, 0 deletions
diff --git a/tests/null_fk/__init__.py b/tests/null_fk/__init__.py
new file mode 100644
index 0000000000..e69de29bb2
--- /dev/null
+++ b/tests/null_fk/__init__.py
diff --git a/tests/null_fk/models.py b/tests/null_fk/models.py
new file mode 100644
index 0000000000..c86ee8a5a9
--- /dev/null
+++ b/tests/null_fk/models.py
@@ -0,0 +1,50 @@
+"""
+Regression tests for proper working of ForeignKey(null=True).
+"""
+
+from django.db import models
+from django.utils.encoding import python_2_unicode_compatible
+
+
+class SystemDetails(models.Model):
+ details = models.TextField()
+
+class SystemInfo(models.Model):
+ system_details = models.ForeignKey(SystemDetails)
+ system_name = models.CharField(max_length=32)
+
+class Forum(models.Model):
+ system_info = models.ForeignKey(SystemInfo)
+ forum_name = models.CharField(max_length=32)
+
+@python_2_unicode_compatible
+class Post(models.Model):
+ forum = models.ForeignKey(Forum, null=True)
+ title = models.CharField(max_length=32)
+
+ def __str__(self):
+ return self.title
+
+@python_2_unicode_compatible
+class Comment(models.Model):
+ post = models.ForeignKey(Post, null=True)
+ comment_text = models.CharField(max_length=250)
+
+ class Meta:
+ ordering = ('comment_text',)
+
+ def __str__(self):
+ return self.comment_text
+
+# Ticket 15823
+
+class Item(models.Model):
+ title = models.CharField(max_length=100)
+
+class PropertyValue(models.Model):
+ label = models.CharField(max_length=100)
+
+class Property(models.Model):
+ item = models.ForeignKey(Item, related_name='props')
+ key = models.CharField(max_length=100)
+ value = models.ForeignKey(PropertyValue, null=True)
diff --git a/tests/null_fk/tests.py b/tests/null_fk/tests.py
new file mode 100644
index 0000000000..96a06b6769
--- /dev/null
+++ b/tests/null_fk/tests.py
@@ -0,0 +1,69 @@
+from __future__ import absolute_import, unicode_literals
+
+from django.db.models import Q
+from django.test import TestCase
+
+from .models import (SystemDetails, Item, PropertyValue, SystemInfo, Forum,
+ Post, Comment)
+
+
+class NullFkTests(TestCase):
+
+ def test_null_fk(self):
+ d = SystemDetails.objects.create(details='First details')
+ s = SystemInfo.objects.create(system_name='First forum', system_details=d)
+ f = Forum.objects.create(system_info=s, forum_name='First forum')
+ p = Post.objects.create(forum=f, title='First Post')
+ c1 = Comment.objects.create(post=p, comment_text='My first comment')
+ c2 = Comment.objects.create(comment_text='My second comment')
+
+ # Starting from comment, make sure that a .select_related(...) with a specified
+ # set of fields will properly LEFT JOIN multiple levels of NULLs (and the things
+ # that come after the NULLs, or else data that should exist won't). Regression
+ # test for #7369.
+ c = Comment.objects.select_related().get(id=c1.id)
+ self.assertEqual(c.post, p)
+ self.assertEqual(Comment.objects.select_related().get(id=c2.id).post, None)
+
+ self.assertQuerysetEqual(
+ Comment.objects.select_related('post__forum__system_info').all(),
+ [
+ (c1.id, 'My first comment', '<Post: First Post>'),
+ (c2.id, 'My second comment', 'None')
+ ],
+ transform = lambda c: (c.id, c.comment_text, repr(c.post))
+ )
+
+ # Regression test for #7530, #7716.
+ self.assertTrue(Comment.objects.select_related('post').filter(post__isnull=True)[0].post is None)
+
+ self.assertQuerysetEqual(
+ Comment.objects.select_related('post__forum__system_info__system_details'),
+ [
+ (c1.id, 'My first comment', '<Post: First Post>'),
+ (c2.id, 'My second comment', 'None')
+ ],
+ transform = lambda c: (c.id, c.comment_text, repr(c.post))
+ )
+
+ def test_combine_isnull(self):
+ item = Item.objects.create(title='Some Item')
+ pv = PropertyValue.objects.create(label='Some Value')
+ item.props.create(key='a', value=pv)
+ item.props.create(key='b') # value=NULL
+ q1 = Q(props__key='a', props__value=pv)
+ q2 = Q(props__key='b', props__value__isnull=True)
+
+ # Each of these individually should return the item.
+ self.assertEqual(Item.objects.get(q1), item)
+ self.assertEqual(Item.objects.get(q2), item)
+
+ # Logically, qs1 and qs2, and qs3 and qs4 should be the same.
+ qs1 = Item.objects.filter(q1) & Item.objects.filter(q2)
+ qs2 = Item.objects.filter(q2) & Item.objects.filter(q1)
+ qs3 = Item.objects.filter(q1) | Item.objects.filter(q2)
+ qs4 = Item.objects.filter(q2) | Item.objects.filter(q1)
+
+ # Regression test for #15823.
+ self.assertEqual(list(qs1), list(qs2))
+ self.assertEqual(list(qs3), list(qs4))