test_api.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import datetime
  2. from django.shortcuts import reverse
  3. from django.utils import timezone
  4. from model_bakery import baker
  5. from freezegun import freeze_time
  6. from glitchtip.test_utils.test_case import GlitchTipTestCase
  7. class TransactionAPITestCase(GlitchTipTestCase):
  8. def setUp(self):
  9. self.create_user_and_project()
  10. self.list_url = reverse(
  11. "organization-transactions-list",
  12. kwargs={"organization_slug": self.organization.slug},
  13. )
  14. def test_list(self):
  15. transaction = baker.make(
  16. "performance.TransactionEvent", group__project=self.project
  17. )
  18. res = self.client.get(self.list_url)
  19. self.assertContains(res, transaction.event_id)
  20. class TransactionGroupAPITestCase(GlitchTipTestCase):
  21. def setUp(self):
  22. self.create_user_and_project()
  23. self.list_url = reverse(
  24. "organization-transaction-groups-list",
  25. kwargs={"organization_slug": self.organization.slug},
  26. )
  27. def test_list(self):
  28. group = baker.make("performance.TransactionGroup", project=self.project)
  29. res = self.client.get(self.list_url)
  30. self.assertContains(res, group.transaction)
  31. def test_filter_then_average(self):
  32. group = baker.make("performance.TransactionGroup", project=self.project)
  33. now = timezone.now()
  34. last_minute = now - datetime.timedelta(minutes=1)
  35. with freeze_time(last_minute):
  36. transaction1 = baker.make(
  37. "performance.TransactionEvent",
  38. group=group,
  39. start_timestamp=last_minute,
  40. timestamp=last_minute + datetime.timedelta(seconds=5),
  41. )
  42. transaction2 = baker.make(
  43. "performance.TransactionEvent",
  44. group=group,
  45. start_timestamp=now,
  46. timestamp=now + datetime.timedelta(seconds=1),
  47. )
  48. res = self.client.get(self.list_url)
  49. self.assertEqual(res.data[0]["avgDuration"], "00:00:03")
  50. res = self.client.get(
  51. self.list_url
  52. + "?start="
  53. + transaction2.created.replace(microsecond=0)
  54. .replace(tzinfo=None)
  55. .isoformat()
  56. + "Z"
  57. )
  58. self.assertEqual(res.data[0]["avgDuration"], "00:00:01")
  59. class SpanAPITestCase(GlitchTipTestCase):
  60. def setUp(self):
  61. self.create_user_and_project()
  62. self.list_url = reverse(
  63. "organization-spans-list",
  64. kwargs={"organization_slug": self.organization.slug},
  65. )
  66. def test_list(self):
  67. span = baker.make("performance.Span", transaction__group__project=self.project)
  68. res = self.client.get(self.list_url)
  69. self.assertContains(res, span.op)