test_clearreadtracker_command.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. from datetime import timedelta
  2. from io import StringIO
  3. from django.core import management
  4. from django.utils import timezone
  5. from ...conf.test import override_dynamic_settings
  6. from ..management.commands import clearreadtracker
  7. from ..models import PostRead
  8. def call_command():
  9. command = clearreadtracker.Command()
  10. out = StringIO()
  11. management.call_command(command, stdout=out)
  12. return out.getvalue().strip().splitlines()[-1].strip()
  13. def test_command_works_if_there_are_no_read_tracker_entries(db):
  14. command_output = call_command()
  15. assert command_output == "No expired entries were found"
  16. @override_dynamic_settings(readtracker_cutoff=5)
  17. def test_recent_read_tracker_entry_is_not_cleared(user, post):
  18. existing = PostRead.objects.create(
  19. user=user,
  20. category=post.category,
  21. thread=post.thread,
  22. post=post,
  23. last_read_on=timezone.now(),
  24. )
  25. command_output = call_command()
  26. assert command_output == "No expired entries were found"
  27. assert PostRead.objects.exists()
  28. @override_dynamic_settings(readtracker_cutoff=5)
  29. def test_old_read_tracker_entry_is_cleared(user, post):
  30. existing = PostRead.objects.create(
  31. user=user,
  32. category=post.category,
  33. thread=post.thread,
  34. post=post,
  35. last_read_on=timezone.now() - timedelta(days=10),
  36. )
  37. command_output = call_command()
  38. assert command_output == "Deleted 1 expired entries"
  39. assert not PostRead.objects.exists()