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 ...categories.models import Category
  7. from ...conf import settings
  8. from ...threads import test
  9. from ...users.test import create_test_user
  10. from ..management.commands import clearreadtracker
  11. from ..models import PostRead
  12. class ClearReadTrackerTests(TestCase):
  13. def setUp(self):
  14. self.user = create_test_user("User", "user@example.com")
  15. self.other_user = create_test_user("OtherUser", "otheruser@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 = test.post_thread(self.category)
  27. existing = PostRead.objects.create(
  28. user=self.user,
  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.other_user,
  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)