test_tasks.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.test import TestCase
  4. from django.utils.timezone import now
  5. from freezegun import freeze_time
  6. from model_bakery import baker
  7. from events.models import Event
  8. from issues.models import Issue
  9. from ..tasks import cleanup_old_events
  10. class TasksTestCase(TestCase):
  11. def test_cleanup_old_events(self):
  12. events = baker.make("events.Event", _quantity=5, _fill_optional=["issue"])
  13. baker.make("events.Event", issue=events[0].issue, _quantity=5)
  14. cleanup_old_events()
  15. self.assertEqual(Event.objects.count(), 10)
  16. self.assertEqual(Issue.objects.count(), 5)
  17. with freeze_time(
  18. now() + timedelta(days=settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS)
  19. ):
  20. cleanup_old_events()
  21. self.assertEqual(Event.objects.count(), 0)
  22. self.assertEqual(Issue.objects.count(), 0)
  23. def test_cleanup_old_events_foreign_keys(self):
  24. baker.make("events.Event", tags={"foo": "bar"})
  25. with freeze_time(
  26. now() + timedelta(days=settings.GLITCHTIP_MAX_EVENT_LIFE_DAYS)
  27. ):
  28. cleanup_old_events()
  29. self.assertEqual(Event.objects.count(), 0)