test_post_model.py 5.0 KB

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