test_post_model.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. from datetime import timedelta
  2. from django.contrib.auth import get_user_model
  3. from django.test import TestCase
  4. from django.utils import timezone
  5. from misago.forums.models import Forum
  6. from misago.threads.checksums import update_post_checksum
  7. from misago.threads.models import Thread, Post
  8. class PostModelTests(TestCase):
  9. def setUp(self):
  10. User = get_user_model()
  11. self.user = User.objects.create_user("Bob", "bob@bob.com", "Pass.123")
  12. datetime = timezone.now()
  13. self.forum = Forum.objects.filter(role="forum")[:1][0]
  14. self.thread = Thread(
  15. forum=self.forum,
  16. started_on=datetime,
  17. starter_name='Tester',
  18. starter_slug='tester',
  19. last_post_on=datetime,
  20. last_poster_name='Tester',
  21. last_poster_slug='tester')
  22. self.thread.set_title("Test thread")
  23. self.thread.save()
  24. self.post = Post.objects.create(
  25. forum=self.forum,
  26. thread=self.thread,
  27. poster=self.user,
  28. poster_name=self.user.username,
  29. poster_ip='127.0.0.1',
  30. original="Hello! I am test message!",
  31. parsed="<p>Hello! I am test message!</p>",
  32. checksum="nope",
  33. posted_on=datetime,
  34. updated_on=datetime)
  35. update_post_checksum(self.post)
  36. self.post.save(update_fields=['checksum'])
  37. self.thread.first_post = self.post
  38. self.thread.last_post = self.post
  39. self.thread.save()
  40. def test_merge_invalid(self):
  41. """see if attempts for invalid merges fail"""
  42. with self.assertRaises(ValueError):
  43. self.post.merge(self.post)
  44. User = get_user_model()
  45. other_user = User.objects.create_user("Jeff", "Je@ff.com", "Pass.123")
  46. other_post = Post.objects.create(
  47. forum=self.forum,
  48. thread=self.thread,
  49. poster=other_user,
  50. poster_name=other_user.username,
  51. poster_ip='127.0.0.1',
  52. original="Hello! I am test message!",
  53. parsed="<p>Hello! I am test message!</p>",
  54. checksum="nope",
  55. posted_on=timezone.now() + timedelta(minutes=5),
  56. updated_on=timezone.now() + timedelta(minutes=5))
  57. with self.assertRaises(ValueError):
  58. self.post.merge(other_post)
  59. other_thread = Thread.objects.create(
  60. forum=self.forum,
  61. started_on=timezone.now(),
  62. starter_name='Tester',
  63. starter_slug='tester',
  64. last_post_on=timezone.now(),
  65. last_poster_name='Tester',
  66. last_poster_slug='tester')
  67. other_post = Post.objects.create(
  68. forum=self.forum,
  69. thread=other_thread,
  70. poster=self.user,
  71. poster_name=self.user.username,
  72. poster_ip='127.0.0.1',
  73. original="Hello! I am test message!",
  74. parsed="<p>Hello! I am test message!</p>",
  75. checksum="nope",
  76. posted_on=timezone.now() + timedelta(minutes=5),
  77. updated_on=timezone.now() + timedelta(minutes=5))
  78. with self.assertRaises(ValueError):
  79. self.post.merge(other_post)
  80. other_post = Post.objects.create(
  81. forum=self.forum,
  82. thread=self.thread,
  83. poster_name=other_user.username,
  84. poster_ip='127.0.0.1',
  85. original="Hello! I am test message!",
  86. parsed="<p>Hello! I am test message!</p>",
  87. checksum="nope",
  88. posted_on=timezone.now() + timedelta(minutes=5),
  89. updated_on=timezone.now() + timedelta(minutes=5))
  90. with self.assertRaises(ValueError):
  91. self.post.merge(other_post)
  92. with self.assertRaises(ValueError):
  93. other_post.merge(self.post)
  94. def test_merge(self):
  95. """merge method merges two posts into one"""
  96. other_post = Post.objects.create(
  97. forum=self.forum,
  98. thread=self.thread,
  99. poster=self.user,
  100. poster_name=self.user.username,
  101. poster_ip='127.0.0.1',
  102. original="I am other message!",
  103. parsed="<p>I am other message!</p>",
  104. checksum="nope",
  105. posted_on=timezone.now() + timedelta(minutes=5),
  106. updated_on=timezone.now() + timedelta(minutes=5))
  107. other_post.merge(self.post)
  108. self.assertIn(other_post.original, self.post.original)
  109. self.assertIn(other_post.parsed, self.post.parsed)
  110. self.assertTrue(self.post.is_valid)
  111. def test_move(self):
  112. """move method moves post to other thread"""
  113. new_thread = Thread.objects.create(
  114. forum=self.forum,
  115. started_on=timezone.now(),
  116. starter_name='Tester',
  117. starter_slug='tester',
  118. last_post_on=timezone.now(),
  119. last_poster_name='Tester',
  120. last_poster_slug='tester')
  121. self.post.move(new_thread)
  122. self.assertEqual(self.post.thread, new_thread)