test_clearreadtracker.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. from datetime import timedelta
  2. from io import StringIO
  3. from django.core.management import call_command
  4. from django.test import TestCase
  5. from django.utils import timezone
  6. from misago.categories.models import Category
  7. from misago.conf import settings
  8. from misago.readtracker.management.commands import clearreadtracker
  9. from misago.readtracker.models import PostRead
  10. from misago.threads import testutils
  11. from misago.users.testutils import create_test_user
  12. class ClearReadTrackerTests(TestCase):
  13. def setUp(self):
  14. self.user_1 = create_test_user("User1", "user1@example.com")
  15. self.user_2 = create_test_user("User2", "user2@example.com")
  16. self.category = Category.objects.get(slug="first-category")
  17. def test_no_deleted(self):
  18. """command works when there are no attachments"""
  19. command = clearreadtracker.Command()
  20. out = StringIO()
  21. call_command(command, stdout=out)
  22. command_output = out.getvalue().strip()
  23. self.assertEqual(command_output, "No expired entries were found")
  24. def test_delete_expired_entries(self):
  25. """test deletes one expired tracker entry, but spares the other"""
  26. thread = testutils.post_thread(self.category)
  27. existing = PostRead.objects.create(
  28. user=self.user_1,
  29. category=self.category,
  30. thread=thread,
  31. post=thread.first_post,
  32. last_read_on=timezone.now()
  33. - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF / 4),
  34. )
  35. deleted = PostRead.objects.create(
  36. user=self.user_2,
  37. category=self.category,
  38. thread=thread,
  39. post=thread.first_post,
  40. last_read_on=timezone.now()
  41. - timedelta(days=settings.MISAGO_READTRACKER_CUTOFF * 2),
  42. )
  43. command = clearreadtracker.Command()
  44. out = StringIO()
  45. call_command(command, stdout=out)
  46. command_output = out.getvalue().strip()
  47. self.assertEqual(command_output, "Deleted 1 expired entries")
  48. PostRead.objects.get(pk=existing.pk)
  49. with self.assertRaises(PostRead.DoesNotExist):
  50. PostRead.objects.get(pk=deleted.pk)