Ticket #17838: test_ticket17838.patch

File test_ticket17838.patch, 2.6 KB (added by Taylor Mitchell, 13 years ago)

Test cases against both contrib.comments and the GFK functionality

  • tests/modeltests/prefetch_related/models.py

    diff --git a/tests/modeltests/prefetch_related/models.py b/tests/modeltests/prefetch_related/models.py
    index e6e4f89..1dc034f 100644
    a b class Bookmark(models.Model):  
    125125    tags = generic.GenericRelation(TaggedItem)
    126126
    127127
     128class Comment(models.Model):
     129    comment = models.TextField()
     130
     131    # Content-object field
     132    content_type   = models.ForeignKey(ContentType)
     133    object_pk      = models.TextField()
     134    content_object = generic.GenericForeignKey(ct_field="content_type", fk_field="object_pk")
     135
     136
    128137## Models for lookup ordering tests
    129138
    130139
  • tests/modeltests/prefetch_related/tests.py

    diff --git a/tests/modeltests/prefetch_related/tests.py b/tests/modeltests/prefetch_related/tests.py
    index 0a75727..810791f 100644
    a b from django.test import TestCase  
    55
    66from .models import (Author, Book, Reader, Qualification, Teacher, Department,
    77    TaggedItem, Bookmark, AuthorAddress, FavoriteAuthors, AuthorWithAge,
    8     BookWithYear, Person, House, Room, Employee)
     8    BookWithYear, Person, House, Room, Employee, Comment)
    99
    1010
    1111class PrefetchRelatedTests(TestCase):
    class GenericRelationTests(TestCase):  
    254254            qs = TaggedItem.objects.prefetch_related('content_object')
    255255            list(qs)
    256256
     257    def test_prefetch_GFK_nonint_pk(self):
     258        Comment.objects.create(comment="awesome", content_object=self.book1)
     259
     260        # 1 for Comment table, 1 for Book table
     261        with self.assertNumQueries(2):
     262            qs = Comment.objects.prefetch_related('content_object')
     263            [c.content_object for c in qs]
     264
     265
    257266    def test_traverse_GFK(self):
    258267        """
    259268        Test that we can traverse a 'content_object' with prefetch_related() and
  • tests/regressiontests/comment_tests/tests/model_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/model_tests.py b/tests/regressiontests/comment_tests/tests/model_tests.py
    index c7eaa4c..69c1a81 100644
    a b class CommentManagerTests(CommentTestCase):  
    4949        author_comments = list(Comment.objects.for_model(Author.objects.get(pk=1)))
    5050        self.assertEqual(article_comments, [c1, c3])
    5151        self.assertEqual(author_comments, [c2])
     52
     53    def testPrefetchRelated(self):
     54        c1, c2, c3, c4 = self.createSomeComments()
     55        # one for comments, one for Articles, one for Author
     56        with self.assertNumQueries(3):
     57            qs = Comment.objects.prefetch_related('content_object')
     58            [c.content_object for c in qs]
Back to Top