test_tasks.py 1.2 KB

1234567891011121314151617181920212223242526272829303132
  1. from datetime import timedelta
  2. from django.conf import settings
  3. from django.utils.timezone import now
  4. from freezegun import freeze_time
  5. from model_bakery import baker
  6. from glitchtip.test_utils.test_case import GlitchTipTestCase
  7. from ..models import Span, TransactionEvent, TransactionGroup
  8. from ..tasks import cleanup_old_transaction_events
  9. class TasksTestCase(GlitchTipTestCase):
  10. def test_cleanup_old_events(self):
  11. group = baker.make("performance.TransactionGroup")
  12. transactions = baker.make(
  13. "performance.TransactionEvent", group=group, _quantity=5
  14. )
  15. baker.make("performance.Span", transaction=transactions[0])
  16. cleanup_old_transaction_events()
  17. self.assertEqual(TransactionGroup.objects.count(), 1)
  18. self.assertEqual(TransactionEvent.objects.count(), 5)
  19. self.assertEqual(Span.objects.count(), 1)
  20. with freeze_time(
  21. now() + timedelta(days=settings.GLITCHTIP_MAX_TRANSACTION_EVENT_LIFE_DAYS)
  22. ):
  23. cleanup_old_transaction_events()
  24. self.assertEqual(TransactionGroup.objects.count(), 0)
  25. self.assertEqual(TransactionEvent.objects.count(), 0)
  26. self.assertEqual(Span.objects.count(), 0)