Browse Source

fix #677: tests for category read cutoff handling

Rafał Pitoń 8 years ago
parent
commit
8f15bef51c

+ 1 - 0
misago/readtracker/categoriestracker.py

@@ -105,6 +105,7 @@ def read_category(user, category):
         categories += queryset.values_list('id', flat=True)
 
     user.categoryread_set.filter(category_id__in=categories).delete()
+    user.threadread_set.filter(category_id__in=categories).delete()
 
     now = timezone.now()
     new_reads = []

+ 48 - 1
misago/readtracker/tests/test_readtracker.py

@@ -166,6 +166,20 @@ class CategoriesTrackerTests(ReadTrackerTests):
         child_read = self.user.categoryread_set.get(category=self.category)
         self.assertTrue(child_read.last_read_on > timezone.now() - timedelta(seconds=3))
 
+    def test_read_category_prunes_threadreads(self):
+        """read_category prunes threadreads in this category"""
+        thread = self.post_thread(timezone.now())
+
+        threadstracker.make_read_aware(self.user, thread)
+        threadstracker.read_thread(self.user, thread, thread.last_post)
+
+        self.assertTrue(self.user.threadread_set.exists())
+
+        categoriestracker.read_category(self.user, self.category)
+
+        self.assertTrue(self.user.categoryread_set.get(category=self.category))
+        self.assertFalse(self.user.threadread_set.exists())
+
 
 class ThreadsTrackerTests(ReadTrackerTests):
     def setUp(self):
@@ -203,7 +217,7 @@ class ThreadsTrackerTests(ReadTrackerTests):
         threadstracker.make_read_aware(self.user, self.thread)
         self.assertFalse(self.thread.is_read)
 
-    def _test_thread_read(self):
+    def test_thread_read(self):
         """thread read flag is set for user, then its set as unread by reply"""
         self.reply_thread()
 
@@ -234,3 +248,36 @@ class ThreadsTrackerTests(ReadTrackerTests):
         for post in posts[:-1]:
             self.assertTrue(post.is_read)
         self.assertFalse(posts[-1].is_read)
+
+    def test_thread_read_category_cutoff(self):
+        """thread read is handled when category cutoff is present"""
+        self.reply_thread()
+
+        add_acl(self.user, self.categories)
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertFalse(self.thread.is_read)
+
+        categoriestracker.read_category(self.user, self.category)
+        threadstracker.make_read_aware(self.user, self.thread)
+        self.assertTrue(self.thread.is_read)
+
+        categoriestracker.make_read_aware(self.user, self.categories)
+        self.assertTrue(self.category.is_read)
+
+        posts = list(self.thread.post_set.order_by('id'))
+        threadstracker.make_posts_read_aware(self.user, self.thread, posts)
+
+        for post in posts:
+            self.assertTrue(post.is_read)
+
+        # post reply
+        self.reply_thread()
+
+        # test if only last post is unread
+        posts = list(self.thread.post_set.order_by('id'))
+        threadstracker.make_read_aware(self.user, self.thread)
+        threadstracker.make_posts_read_aware(self.user, self.thread, posts)
+
+        for post in posts[:-1]:
+            self.assertTrue(post.is_read)
+        self.assertTrue(posts[-1].is_new)

+ 2 - 0
misago/readtracker/threadstracker.py

@@ -93,6 +93,8 @@ def make_thread_read_aware(user, thread):
         try:
             category_record = user.categoryread_set.get(
                 category_id=thread.category_id)
+            thread.last_read_on = category_record.last_read_on
+
             if thread.last_post_on > category_record.last_read_on:
                 try:
                     thread_record = user.threadread_set.get(thread=thread)