tests.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. from django.test import TestCase
  2. from .models import Article, Author, Comment, Forum, Post, SystemInfo
  3. class NullFkOrderingTests(TestCase):
  4. def test_ordering_across_null_fk(self):
  5. """
  6. Regression test for #7512
  7. ordering across nullable Foreign Keys shouldn't exclude results
  8. """
  9. author_1 = Author.objects.create(name='Tom Jones')
  10. author_2 = Author.objects.create(name='Bob Smith')
  11. Article.objects.create(title='No author on this article')
  12. Article.objects.create(author=author_1, title='This article written by Tom Jones')
  13. Article.objects.create(author=author_2, title='This article written by Bob Smith')
  14. # We can't compare results directly (since different databases sort NULLs to
  15. # different ends of the ordering), but we can check that all results are
  16. # returned.
  17. self.assertEqual(len(list(Article.objects.all())), 3)
  18. s = SystemInfo.objects.create(system_name='System Info')
  19. f = Forum.objects.create(system_info=s, forum_name='First forum')
  20. p = Post.objects.create(forum=f, title='First Post')
  21. Comment.objects.create(post=p, comment_text='My first comment')
  22. Comment.objects.create(comment_text='My second comment')
  23. s2 = SystemInfo.objects.create(system_name='More System Info')
  24. f2 = Forum.objects.create(system_info=s2, forum_name='Second forum')
  25. p2 = Post.objects.create(forum=f2, title='Second Post')
  26. Comment.objects.create(comment_text='Another first comment')
  27. Comment.objects.create(post=p2, comment_text='Another second comment')
  28. # We have to test this carefully. Some databases sort NULL values before
  29. # everything else, some sort them afterwards. So we extract the ordered list
  30. # and check the length. Before the fix, this list was too short (some values
  31. # were omitted).
  32. self.assertEqual(len(list(Comment.objects.all())), 4)