Ticket #16042: 16042.4.diff

File 16042.4.diff, 5.8 KB (added by Julien Phalip, 13 years ago)

Cleaner, more thorough tests

  • django/contrib/comments/templatetags/comments.py

    diff --git a/django/contrib/comments/templatetags/comments.py b/django/contrib/comments/templatetags/comments.py
    index 7962a40..52c731c 100644
    a b class BaseCommentNode(template.Node):  
    4747    def lookup_content_type(token, tagname):
    4848        try:
    4949            app, model = token.split('.')
    50             return ContentType.objects.get(app_label=app, model=model)
     50            return ContentType.objects.get_by_natural_key(app, model)
    5151        except ValueError:
    5252            raise template.TemplateSyntaxError("Third argument in %r must be in the format 'app.model'" % tagname)
    5353        except ContentType.DoesNotExist:
  • tests/regressiontests/comment_tests/tests/templatetag_tests.py

    diff --git a/tests/regressiontests/comment_tests/tests/templatetag_tests.py b/tests/regressiontests/comment_tests/tests/templatetag_tests.py
    index 0ead6c2..99c3ca3 100644
    a b  
     1from __future__ import with_statement
     2
    13from django.contrib.comments.forms import CommentForm
    24from django.contrib.comments.models import Comment
    35from django.contrib.contenttypes.models import ContentType
    class CommentTemplateTagTests(CommentTestCase):  
    4547            self.testRenderCommentFormFromObject()
    4648        self.assertNumQueries(1, test)
    4749
    48     def testGetCommentCount(self, tag=None):
    49         self.createSomeComments()
     50    def verifyGetCommentCount(self, tag=None):
    5051        t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
    5152        ctx, out = self.render(t, a=Article.objects.get(pk=1))
    5253        self.assertEqual(out, "2")
    5354
     55    def testGetCommentCount(self):
     56        self.createSomeComments()
     57        self.verifyGetCommentCount("{% get_comment_count for comment_tests.article a.id as cc %}")
     58
    5459    def testGetCommentCountFromLiteral(self):
    55         self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
     60        self.createSomeComments()
     61        self.verifyGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
    5662
    5763    def testGetCommentCountFromObject(self):
    58         self.testGetCommentCount("{% get_comment_count for a as cc %}")
     64        self.createSomeComments()
     65        self.verifyGetCommentCount("{% get_comment_count for a as cc %}")
    5966
    60     def testGetCommentList(self, tag=None):
    61         c1, c2, c3, c4 = self.createSomeComments()
    62         t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
     67    def verifyGetCommentList(self, tag=None):
     68        c1, c2, c3, c4 = Comment.objects.all()[:4]
     69        t = "{% load comments %}" +  (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
    6370        ctx, out = self.render(t, a=Author.objects.get(pk=1))
    6471        self.assertEqual(out, "")
    6572        self.assertEqual(list(ctx["cl"]), [c2])
    6673
     74    def testGetCommentList(self):
     75        self.createSomeComments()
     76        self.verifyGetCommentList("{% get_comment_list for comment_tests.author a.id as cl %}")
     77
    6778    def testGetCommentListFromLiteral(self):
    68         self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
     79        self.createSomeComments()
     80        self.verifyGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
    6981
    7082    def testGetCommentListFromObject(self):
    71         self.testGetCommentList("{% get_comment_list for a as cl %}")
     83        self.createSomeComments()
     84        self.verifyGetCommentList("{% get_comment_list for a as cl %}")
    7285
    7386    def testGetCommentPermalink(self):
    7487        c1, c2, c3, c4 = self.createSomeComments()
    class CommentTemplateTagTests(CommentTestCase):  
    100113    def testRenderCommentListFromObject(self):
    101114        self.testRenderCommentList("{% render_comment_list for a %}")
    102115
     116    def testNumberQueries(self):
     117        """
     118        Ensure that the template tags use cached content types to reduce the
     119        number of DB queries.
     120        Refs #16042.
     121        """
     122
     123        self.createSomeComments()
     124
     125        # {% render_comment_list %} -----------------
     126
     127        # Clear CT cache
     128        ContentType.objects.clear_cache()
     129        with self.assertNumQueries(4):
     130            self.testRenderCommentListFromObject()
     131
     132        # Force the CT to be cached
     133        ct = ContentType.objects.get_for_model(Article)
     134        with self.assertNumQueries(3):
     135            self.testRenderCommentListFromObject()
     136
     137        # {% get_comment_list %} --------------------
     138
     139        ContentType.objects.clear_cache()
     140        with self.assertNumQueries(4):
     141            self.verifyGetCommentList()
     142
     143        ct = ContentType.objects.get_for_model(Author)
     144        with self.assertNumQueries(3):
     145            self.verifyGetCommentList()
     146
     147        # {% render_comment_form %} -----------------
     148
     149        ContentType.objects.clear_cache()
     150        with self.assertNumQueries(3):
     151            self.testRenderCommentForm()
     152
     153        ct = ContentType.objects.get_for_model(Article)
     154        with self.assertNumQueries(2):
     155            self.testRenderCommentForm()
     156
     157        # {% get_comment_form %} --------------------
     158
     159        ContentType.objects.clear_cache()
     160        with self.assertNumQueries(3):
     161            self.testGetCommentForm()
     162
     163        ct = ContentType.objects.get_for_model(Article)
     164        with self.assertNumQueries(2):
     165            self.testGetCommentForm()
     166
     167        # {% get_comment_count %} -------------------
     168
     169        ContentType.objects.clear_cache()
     170        with self.assertNumQueries(3):
     171            self.verifyGetCommentCount()
     172
     173        ct = ContentType.objects.get_for_model(Article)
     174        with self.assertNumQueries(2):
     175            self.verifyGetCommentCount()
Back to Top