Browse Source

#410: Tests suite for reads tracker.

Rafał Pitoń 11 years ago
parent
commit
ba8c45004a
2 changed files with 80 additions and 6 deletions
  1. 74 0
      misago/readtracker/tests/test_readtracker.py
  2. 6 6
      misago/readtracker/threadstracker.py

+ 74 - 0
misago/readtracker/tests/test_readtracker.py

@@ -160,3 +160,77 @@ class ForumsTrackerTests(ReadTrackerTests):
         forumstracker.sync_record(self.user, self.forum)
         forumstracker.make_read_aware(self.user, self.forums)
         self.assertFalse(self.forum.is_read)
+
+
+class ThreadsTrackerTests(ReadTrackerTests):
+    def setUp(self):
+        super(ThreadsTrackerTests, self).setUp()
+
+        self.thread = self.post_thread(timezone.now() - timedelta(days=10))
+        self.reply_thread()
+
+    def reply_thread(self, is_hidden=False, is_moderated=False):
+        post = Post.objects.create(
+            forum=self.forum,
+            thread=self.thread,
+            poster=self.user,
+            poster_name=self.user.username,
+            poster_ip='127.0.0.1',
+            posted_on=self.thread.last_post_on + timedelta(minutes=5),
+            updated_on=self.thread.last_post_on + timedelta(minutes=5),
+            original='test',
+            parsed='test',
+            checksum='nope',
+            is_hidden=is_hidden,
+            is_moderated=is_moderated)
+        self.thread.synchronize()
+        self.thread.save()
+        self.forum.synchronize()
+        self.forum.save()
+
+        if not is_moderated:
+            self.post = post
+        return post
+
+    def test_thread_read_for_guest(self):
+        """threads are always read for guests"""
+        threadstracker.make_read_aware(self.anon, self.thread)
+        self.assertTrue(self.thread.is_read)
+
+        threadstracker.make_read_aware(self.anon, [self.thread])
+        self.assertTrue(self.thread.is_read)
+
+    def test_thread_unread_for_user(self):
+        """thread is unread for user"""
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertFalse(self.thread.is_read)
+
+    def test_thread_read(self):
+        """thread read flag is set for user, then its set as unread by reply"""
+        add_acl(self.user, self.forums)
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertFalse(self.thread.is_read)
+
+        threadstracker.read_thread(self.user, self.thread, self.post)
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertTrue(self.thread.is_read)
+        forumstracker.make_read_aware(self.user, self.forums)
+        self.assertTrue(self.forum.is_read)
+
+        self.thread.last_post_on = timezone.now()
+        self.thread.save()
+        self.forum.synchronize()
+        self.forum.save()
+
+        self.reply_thread()
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertFalse(self.thread.is_read)
+        forumstracker.make_read_aware(self.user, self.forums)
+        self.assertFalse(self.forum.is_read)
+
+        posts = [post for post in self.thread.post_set.order_by('id')]
+        threadstracker.make_posts_read_aware(self.thread, posts)
+
+        for post in posts[:-1]:
+            self.assertTrue(post.is_read)
+        self.assertFalse(posts[-1].is_read)

+ 6 - 6
misago/readtracker/threadstracker.py

@@ -74,6 +74,12 @@ def make_posts_read_aware(thread, posts):
                 post.is_read = True
 
 
+def read_thread(user, thread, last_read_reply):
+    if not thread.is_read:
+        if thread.last_read_on < last_read_reply.updated_on:
+            sync_record(user, thread, last_read_reply)
+
+
 def count_read_replies(user, thread, last_read_reply):
     if last_read_reply.updated_on >= thread.last_read_on:
         return 0
@@ -84,12 +90,6 @@ def count_read_replies(user, thread, last_read_reply):
         return queryset.count()
 
 
-def read_thread(user, thread, last_read_reply):
-    if not thread.is_read:
-        if thread.last_read_on < last_read_reply.updated_on:
-            sync_record(user, thread, last_read_reply)
-
-
 def sync_record(user, thread, last_read_reply):
     read_replies = count_read_replies(user, thread, last_read_reply)
     if thread.read_record: