test_goto.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. from misago.acl import add_acl
  2. from misago.forums.models import Forum
  3. from misago.readtracker import threadstracker
  4. from misago.users.testutils import AuthenticatedUserTestCase
  5. from misago.threads import goto
  6. from misago.threads.permissions import exclude_invisible_posts
  7. from misago.threads.testutils import post_thread, reply_thread
  8. class MockThreadsCounter(object):
  9. def set(self, *args, **kwargs):
  10. pass
  11. def decrease(self, *args, **kwargs):
  12. pass
  13. class GotoTests(AuthenticatedUserTestCase):
  14. def setUp(self):
  15. super(GotoTests, self).setUp()
  16. self.forum = Forum.objects.all_forums().filter(role="forum")[:1][0]
  17. self.forum.labels = []
  18. self.thread = post_thread(self.forum)
  19. add_acl(self.user, self.forum)
  20. add_acl(self.user, self.thread)
  21. def test_get_thread_pages(self):
  22. """get_thread_pages returns valid count of pages for given positions"""
  23. self.assertEqual(goto.get_thread_pages(1), 1)
  24. self.assertEqual(goto.get_thread_pages(10), 1)
  25. self.assertEqual(goto.get_thread_pages(13), 1)
  26. self.assertEqual(goto.get_thread_pages(14), 2)
  27. self.assertEqual(goto.get_thread_pages(19), 2)
  28. self.assertEqual(goto.get_thread_pages(20), 2)
  29. self.assertEqual(goto.get_thread_pages(23), 2)
  30. self.assertEqual(goto.get_thread_pages(24), 3)
  31. self.assertEqual(goto.get_thread_pages(27), 3)
  32. self.assertEqual(goto.get_thread_pages(36), 4)
  33. self.assertEqual(goto.get_thread_pages(373), 37)
  34. def test_get_post_page(self):
  35. """get_post_page returns valid page number for given queryset"""
  36. self.assertEqual(goto.get_post_page(1, self.thread.post_set), 1)
  37. # add 12 posts, bumping no of posts on page to to 13
  38. [reply_thread(self.thread) for p in xrange(12)]
  39. self.assertEqual(goto.get_post_page(13, self.thread.post_set), 1)
  40. # add 2 posts
  41. [reply_thread(self.thread) for p in xrange(2)]
  42. self.assertEqual(goto.get_post_page(15, self.thread.post_set), 2)
  43. def test_hashed_reverse(self):
  44. """hashed_reverse returns complete url for given post"""
  45. url = goto.hashed_reverse(self.thread, self.thread.first_post)
  46. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  47. self.assertEqual(url, '%s#post-%s' % url_formats)
  48. url = goto.hashed_reverse(self.thread, self.thread.first_post, 4)
  49. url_formats = self.thread.get_absolute_url(), self.thread.first_post_id
  50. self.assertEqual(url, '%s4/#post-%s' % url_formats)
  51. def test_last(self):
  52. """last returns link to last post in thread"""
  53. url_last = goto.last(self.thread, self.thread.post_set)
  54. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  55. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  56. # add 12 posts to reach page limit
  57. [reply_thread(self.thread) for p in xrange(12)]
  58. url_last = goto.last(self.thread, self.thread.post_set)
  59. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  60. self.assertEqual(url_last, '%s#post-%s' % url_formats)
  61. # add 2 posts to add second page to thread
  62. [reply_thread(self.thread) for p in xrange(2)]
  63. url_last = goto.last(self.thread, self.thread.post_set)
  64. url_formats = self.thread.get_absolute_url(), self.thread.last_post_id
  65. self.assertEqual(url_last, '%s2/#post-%s' % url_formats)
  66. def test_get_post_link(self):
  67. """get_post_link returns link to specified post"""
  68. post_link = goto.get_post_link(
  69. 1, self.thread.post_set, self.thread, self.thread.last_post)
  70. last_link = goto.last(self.thread, self.thread.post_set)
  71. self.assertEqual(post_link, last_link)
  72. # add 16 posts to add extra page to thread
  73. [reply_thread(self.thread) for p in xrange(16)]
  74. post_link = goto.get_post_link(
  75. 17, self.thread.post_set, self.thread, self.thread.last_post)
  76. last_link = goto.last(self.thread, self.thread.post_set)
  77. self.assertEqual(post_link, last_link)
  78. def test_new(self):
  79. """new returns link to first unread post"""
  80. self.user.new_threads = MockThreadsCounter()
  81. self.user.unread_threads = MockThreadsCounter()
  82. post_link = goto.new(self.user, self.thread, self.thread.post_set)
  83. last_link = goto.last(self.thread, self.thread.post_set)
  84. self.assertEqual(post_link, last_link)
  85. # add 18 posts to add extra page to thread, then read them
  86. [reply_thread(self.thread) for p in xrange(18)]
  87. threadstracker.read_thread(
  88. self.user, self.thread, self.thread.last_post)
  89. # add extra unread posts
  90. first_unread = reply_thread(self.thread)
  91. [reply_thread(self.thread) for p in xrange(30)]
  92. new_link = goto.new(self.user, self.thread, self.thread.post_set)
  93. post_link = goto.get_post_link(
  94. 50, self.thread.post_set, self.thread, first_unread)
  95. self.assertEqual(new_link, post_link)
  96. # read thread
  97. threadstracker.read_thread(
  98. self.user, self.thread, self.thread.last_post)
  99. # assert new() points to last reply
  100. post_link = goto.new(self.user, self.thread, self.thread.post_set)
  101. last_link = goto.last(self.thread, self.thread.post_set)
  102. self.assertEqual(post_link, last_link)
  103. def test_post(self):
  104. """post returns link to post given"""
  105. thread = self.thread
  106. post_link = goto.post(thread, thread.post_set, thread.last_post)
  107. last_link = goto.last(self.thread, self.thread.post_set)
  108. self.assertEqual(post_link, last_link)
  109. # add 24 posts
  110. [reply_thread(self.thread) for p in xrange(24)]
  111. post_link = goto.post(thread, thread.post_set, thread.last_post)
  112. last_link = goto.last(self.thread, self.thread.post_set)
  113. self.assertEqual(post_link, last_link)