test_api.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. import datetime
  2. from django.shortcuts 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 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_list_relative_datetime_filter(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. baker.make(
  37. "performance.TransactionEvent",
  38. group=group,
  39. start_timestamp=last_minute,
  40. timestamp=last_minute + datetime.timedelta(seconds=5),
  41. duration=5000,
  42. )
  43. two_minutes_ago = now - datetime.timedelta(minutes=2)
  44. with freeze_time(two_minutes_ago):
  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. with freeze_time(yesterday):
  54. baker.make(
  55. "performance.TransactionEvent",
  56. group=group,
  57. start_timestamp=yesterday,
  58. timestamp=yesterday + datetime.timedelta(seconds=1),
  59. duration=1000,
  60. )
  61. with freeze_time(now):
  62. res = self.client.get(self.list_url, {"start": last_minute})
  63. self.assertEqual(res.status_code, 200)
  64. self.assertEqual(res.data[0]["transactionCount"], 1)
  65. with freeze_time(now):
  66. res = self.client.get(self.list_url, {"start": "now-1m", "end": "now"})
  67. self.assertEqual(res.status_code, 200)
  68. self.assertEqual(res.data[0]["transactionCount"], 1)
  69. with freeze_time(now):
  70. res = self.client.get(self.list_url, {"start": "now-2m"})
  71. self.assertEqual(res.status_code, 200)
  72. self.assertEqual(res.data[0]["transactionCount"], 2)
  73. with freeze_time(now):
  74. res = self.client.get(self.list_url, {"end": "now-1d"})
  75. self.assertEqual(res.status_code, 200)
  76. self.assertEqual(res.data[0]["transactionCount"], 1)
  77. with freeze_time(now):
  78. res = self.client.get(self.list_url, {"end": "now-24h"})
  79. self.assertEqual(res.status_code, 200)
  80. self.assertEqual(res.data[0]["transactionCount"], 1)
  81. with freeze_time(now):
  82. res = self.client.get(self.list_url, {"end": "now"})
  83. self.assertEqual(res.status_code, 200)
  84. self.assertEqual(res.data[0]["transactionCount"], 3)
  85. def test_list_relative_parsing(self):
  86. res = self.client.get(self.list_url, {"start": "now-1h "})
  87. self.assertEqual(res.status_code, 200)
  88. res = self.client.get(self.list_url, {"start": "now - 1h"})
  89. self.assertEqual(res.status_code, 200)
  90. res = self.client.get(self.list_url, {"start": "now-1"})
  91. self.assertEqual(res.status_code, 400)
  92. res = self.client.get(self.list_url, {"start": "now-1minute"})
  93. self.assertEqual(res.status_code, 400)
  94. res = self.client.get(self.list_url, {"start": "won-1m"})
  95. self.assertEqual(res.status_code, 400)
  96. res = self.client.get(self.list_url, {"start": "now+1m"})
  97. self.assertEqual(res.status_code, 400)
  98. res = self.client.get(self.list_url, {"start": "now 1m"})
  99. self.assertEqual(res.status_code, 400)
  100. def test_list_environment_filter(self):
  101. environment_project = baker.make(
  102. "environments.EnvironmentProject",
  103. environment__organization=self.organization,
  104. )
  105. environment = environment_project.environment
  106. environment.projects.add(self.project)
  107. group1 = baker.make(
  108. "performance.TransactionGroup",
  109. project=self.project,
  110. tags={"environment": [environment.name]},
  111. )
  112. group2 = baker.make("performance.TransactionGroup", project=self.project)
  113. res = self.client.get(self.list_url, {"environment": environment.name})
  114. self.assertContains(res, group1.transaction)
  115. self.assertNotContains(res, group2.transaction)
  116. def test_filter_then_average(self):
  117. group = baker.make("performance.TransactionGroup", project=self.project)
  118. now = timezone.now()
  119. last_minute = now - datetime.timedelta(minutes=1)
  120. with freeze_time(last_minute):
  121. baker.make(
  122. "performance.TransactionEvent",
  123. group=group,
  124. start_timestamp=last_minute,
  125. timestamp=last_minute + datetime.timedelta(seconds=5),
  126. duration=5000,
  127. )
  128. transaction2 = baker.make(
  129. "performance.TransactionEvent",
  130. group=group,
  131. start_timestamp=now,
  132. timestamp=now + datetime.timedelta(seconds=1),
  133. duration=1000,
  134. )
  135. res = self.client.get(self.list_url)
  136. self.assertEqual(res.data[0]["avgDuration"], 3000)
  137. res = self.client.get(
  138. self.list_url
  139. + "?start="
  140. + transaction2.created.replace(microsecond=0)
  141. .replace(tzinfo=None)
  142. .isoformat()
  143. + "Z"
  144. )
  145. self.assertEqual(res.data[0]["avgDuration"], 1000)
  146. class SpanAPITestCase(GlitchTipTestCase):
  147. def setUp(self):
  148. self.create_user_and_project()
  149. self.list_url = reverse(
  150. "organization-spans-list",
  151. kwargs={"organization_slug": self.organization.slug},
  152. )
  153. def test_list(self):
  154. span = baker.make("performance.Span", transaction__group__project=self.project)
  155. res = self.client.get(self.list_url)
  156. self.assertContains(res, span.op)