PageRenderTime 56ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/tests/regressiontests/comment_tests/tests/templatetag_tests.py

https://code.google.com/p/mango-py/
Python | 97 lines | 88 code | 9 blank | 0 comment | 14 complexity | c442e38877340d0f8ee78345d9a42e58 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. from django.contrib.comments.forms import CommentForm
  2. from django.contrib.comments.models import Comment
  3. from django.contrib.contenttypes.models import ContentType
  4. from django.template import Template, Context
  5. from regressiontests.comment_tests.models import Article, Author
  6. from regressiontests.comment_tests.tests import CommentTestCase
  7. class CommentTemplateTagTests(CommentTestCase):
  8. def render(self, t, **c):
  9. ctx = Context(c)
  10. out = Template(t).render(ctx)
  11. return ctx, out
  12. def testCommentFormTarget(self):
  13. ctx, out = self.render("{% load comments %}{% comment_form_target %}")
  14. self.assertEqual(out, "/post/")
  15. def testGetCommentForm(self, tag=None):
  16. t = "{% load comments %}" + (tag or "{% get_comment_form for comment_tests.article a.id as form %}")
  17. ctx, out = self.render(t, a=Article.objects.get(pk=1))
  18. self.assertEqual(out, "")
  19. self.assertTrue(isinstance(ctx["form"], CommentForm))
  20. def testGetCommentFormFromLiteral(self):
  21. self.testGetCommentForm("{% get_comment_form for comment_tests.article 1 as form %}")
  22. def testGetCommentFormFromObject(self):
  23. self.testGetCommentForm("{% get_comment_form for a as form %}")
  24. def testRenderCommentForm(self, tag=None):
  25. t = "{% load comments %}" + (tag or "{% render_comment_form for comment_tests.article a.id %}")
  26. ctx, out = self.render(t, a=Article.objects.get(pk=1))
  27. self.assertTrue(out.strip().startswith("<form action="))
  28. self.assertTrue(out.strip().endswith("</form>"))
  29. def testRenderCommentFormFromLiteral(self):
  30. self.testRenderCommentForm("{% render_comment_form for comment_tests.article 1 %}")
  31. def testRenderCommentFormFromObject(self):
  32. self.testRenderCommentForm("{% render_comment_form for a %}")
  33. def testGetCommentCount(self, tag=None):
  34. self.createSomeComments()
  35. t = "{% load comments %}" + (tag or "{% get_comment_count for comment_tests.article a.id as cc %}") + "{{ cc }}"
  36. ctx, out = self.render(t, a=Article.objects.get(pk=1))
  37. self.assertEqual(out, "2")
  38. def testGetCommentCountFromLiteral(self):
  39. self.testGetCommentCount("{% get_comment_count for comment_tests.article 1 as cc %}")
  40. def testGetCommentCountFromObject(self):
  41. self.testGetCommentCount("{% get_comment_count for a as cc %}")
  42. def testGetCommentList(self, tag=None):
  43. c1, c2, c3, c4 = self.createSomeComments()
  44. t = "{% load comments %}" + (tag or "{% get_comment_list for comment_tests.author a.id as cl %}")
  45. ctx, out = self.render(t, a=Author.objects.get(pk=1))
  46. self.assertEqual(out, "")
  47. self.assertEqual(list(ctx["cl"]), [c2])
  48. def testGetCommentListFromLiteral(self):
  49. self.testGetCommentList("{% get_comment_list for comment_tests.author 1 as cl %}")
  50. def testGetCommentListFromObject(self):
  51. self.testGetCommentList("{% get_comment_list for a as cl %}")
  52. def testGetCommentPermalink(self):
  53. c1, c2, c3, c4 = self.createSomeComments()
  54. t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
  55. t += "{% get_comment_permalink cl.0 %}"
  56. ct = ContentType.objects.get_for_model(Author)
  57. author = Author.objects.get(pk=1)
  58. ctx, out = self.render(t, author=author)
  59. self.assertEqual(out, "/cr/%s/%s/#c%s" % (ct.id, author.id, c2.id))
  60. def testGetCommentPermalinkFormatted(self):
  61. c1, c2, c3, c4 = self.createSomeComments()
  62. t = "{% load comments %}{% get_comment_list for comment_tests.author author.id as cl %}"
  63. t += "{% get_comment_permalink cl.0 '#c%(id)s-by-%(user_name)s' %}"
  64. ct = ContentType.objects.get_for_model(Author)
  65. author = Author.objects.get(pk=1)
  66. ctx, out = self.render(t, author=author)
  67. self.assertEqual(out, "/cr/%s/%s/#c%s-by-Joe Somebody" % (ct.id, author.id, c2.id))
  68. def testRenderCommentList(self, tag=None):
  69. t = "{% load comments %}" + (tag or "{% render_comment_list for comment_tests.article a.id %}")
  70. ctx, out = self.render(t, a=Article.objects.get(pk=1))
  71. self.assertTrue(out.strip().startswith("<dl id=\"comments\">"))
  72. self.assertTrue(out.strip().endswith("</dl>"))
  73. def testRenderCommentListFromLiteral(self):
  74. self.testRenderCommentList("{% render_comment_list for comment_tests.article 1 %}")
  75. def testRenderCommentListFromObject(self):
  76. self.testRenderCommentList("{% render_comment_list for a %}")