test_api.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import datetime
  2. from django.urls import reverse
  3. from django.utils import timezone
  4. from freezegun import freeze_time
  5. from model_bakery import baker
  6. from glitchtip.test_utils.test_case import GlitchTestCase
  7. class TransactionAPITestCase(GlitchTestCase):
  8. @classmethod
  9. def setUpTestData(cls):
  10. cls.create_user()
  11. cls.list_url = reverse("api:list_transactions", args=[cls.organization.slug])
  12. def setUp(self):
  13. self.client.force_login(self.user)
  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(GlitchTestCase):
  21. @classmethod
  22. def setUpTestData(cls):
  23. cls.create_user()
  24. cls.list_url = reverse(
  25. "api:list_transaction_groups", args=[cls.organization.slug]
  26. )
  27. def setUp(self):
  28. self.client.force_login(self.user)
  29. def test_list(self):
  30. group = baker.make("performance.TransactionGroup", project=self.project)
  31. res = self.client.get(self.list_url)
  32. self.assertContains(res, group.transaction)
  33. def test_list_relative_datetime_filter(self):
  34. group = baker.make("performance.TransactionGroup", project=self.project)
  35. now = timezone.now()
  36. last_minute = now - datetime.timedelta(minutes=1)
  37. baker.make(
  38. "performance.TransactionEvent",
  39. group=group,
  40. start_timestamp=last_minute,
  41. timestamp=last_minute + datetime.timedelta(seconds=5),
  42. duration=5000,
  43. )
  44. two_minutes_ago = now - datetime.timedelta(minutes=2)
  45. baker.make(
  46. "performance.TransactionEvent",
  47. group=group,
  48. start_timestamp=two_minutes_ago,
  49. timestamp=two_minutes_ago + datetime.timedelta(seconds=1),
  50. duration=1000,
  51. )
  52. yesterday = now - datetime.timedelta(days=1)
  53. baker.make(
  54. "performance.TransactionEvent",
  55. group=group,
  56. start_timestamp=yesterday,
  57. timestamp=yesterday + datetime.timedelta(seconds=1),
  58. duration=1000,
  59. )
  60. with freeze_time(now):
  61. res = self.client.get(self.list_url, {"start": last_minute})
  62. self.assertEqual(res.status_code, 200)
  63. self.assertEqual(res.json()[0]["transactionCount"], 1)
  64. with freeze_time(now):
  65. res = self.client.get(self.list_url, {"start": "now-1m", "end": "now"})
  66. self.assertEqual(res.status_code, 200)
  67. self.assertEqual(res.json()[0]["transactionCount"], 1)
  68. with freeze_time(now):
  69. res = self.client.get(self.list_url, {"start": "now-2m"})
  70. self.assertEqual(res.status_code, 200)
  71. self.assertEqual(res.json()[0]["transactionCount"], 2)
  72. with freeze_time(now):
  73. res = self.client.get(self.list_url, {"end": "now-1d"})
  74. self.assertEqual(res.status_code, 200)
  75. self.assertEqual(res.json()[0]["transactionCount"], 1)
  76. with freeze_time(now):
  77. res = self.client.get(self.list_url, {"end": "now-24h"})
  78. self.assertEqual(res.status_code, 200)
  79. self.assertEqual(res.json()[0]["transactionCount"], 1)
  80. with freeze_time(now):
  81. res = self.client.get(self.list_url, {"end": "now"})
  82. self.assertEqual(res.status_code, 200)
  83. self.assertEqual(res.json()[0]["transactionCount"], 3)
  84. def test_list_relative_parsing(self):
  85. res = self.client.get(self.list_url, {"start": "now-1h "})
  86. self.assertEqual(res.status_code, 200)
  87. res = self.client.get(self.list_url, {"start": "now - 1h"})
  88. self.assertEqual(res.status_code, 200)
  89. res = self.client.get(self.list_url, {"start": "now-1"})
  90. self.assertEqual(res.status_code, 422)
  91. res = self.client.get(self.list_url, {"start": "now-1minute"})
  92. self.assertEqual(res.status_code, 422)
  93. res = self.client.get(self.list_url, {"start": "won-1m"})
  94. self.assertEqual(res.status_code, 422)
  95. res = self.client.get(self.list_url, {"start": "now+1m"})
  96. self.assertEqual(res.status_code, 422)
  97. res = self.client.get(self.list_url, {"start": "now 1m"})
  98. self.assertEqual(res.status_code, 422)
  99. def test_list_environment_filter(self):
  100. environment_project = baker.make(
  101. "environments.EnvironmentProject",
  102. environment__organization=self.organization,
  103. )
  104. environment = environment_project.environment
  105. environment.projects.add(self.project)
  106. group1 = baker.make(
  107. "performance.TransactionGroup",
  108. project=self.project,
  109. tags={"environment": [environment.name]},
  110. )
  111. group2 = baker.make("performance.TransactionGroup", project=self.project)
  112. res = self.client.get(self.list_url, {"environment": environment.name})
  113. self.assertContains(res, group1.transaction)
  114. self.assertNotContains(res, group2.transaction)
  115. def test_filter_then_average(self):
  116. group = baker.make("performance.TransactionGroup", project=self.project)
  117. now = timezone.now()
  118. last_minute = now - datetime.timedelta(minutes=1)
  119. with freeze_time(last_minute):
  120. baker.make(
  121. "performance.TransactionEvent",
  122. group=group,
  123. start_timestamp=last_minute,
  124. timestamp=last_minute + datetime.timedelta(seconds=5),
  125. duration=5000,
  126. )
  127. transaction2 = baker.make(
  128. "performance.TransactionEvent",
  129. group=group,
  130. start_timestamp=now,
  131. timestamp=now + datetime.timedelta(seconds=1),
  132. duration=1000,
  133. )
  134. res = self.client.get(self.list_url)
  135. self.assertEqual(res.json()[0]["avgDuration"], 3000)
  136. res = self.client.get(
  137. self.list_url
  138. + "?start="
  139. + transaction2.start_timestamp.replace(microsecond=0)
  140. .replace(tzinfo=None)
  141. .isoformat()
  142. + "Z"
  143. )
  144. self.assertEqual(res.json()[0]["avgDuration"], 1000)