test_api.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_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=datetime.timedelta(seconds=5),
  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=datetime.timedelta(seconds=1),
  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=datetime.timedelta(seconds=1),
  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"})
  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. # with freeze_time(now):
  86. # res = self.client.get(self.list_url, {"start": "now-1m", "end":"now"})
  87. # self.assertEqual(res.status_code, 200)
  88. # self.assertEqual(res.data[0]["transactionCount"], 1)
  89. res = self.client.get(self.list_url, {"start": "now-1"})
  90. self.assertEqual(res.status_code, 400)
  91. res = self.client.get(self.list_url, {"start": "now-1minute"})
  92. self.assertEqual(res.status_code, 400)
  93. res = self.client.get(self.list_url, {"start": "won-1m"})
  94. self.assertEqual(res.status_code, 400)
  95. def test_list_environment_filter(self):
  96. environment_project = baker.make(
  97. "environments.EnvironmentProject",
  98. environment__organization=self.organization,
  99. )
  100. environment = environment_project.environment
  101. environment.projects.add(self.project)
  102. group1 = baker.make(
  103. "performance.TransactionGroup",
  104. project=self.project,
  105. tags={"environment": [environment.name]},
  106. )
  107. group2 = baker.make("performance.TransactionGroup", project=self.project)
  108. res = self.client.get(self.list_url, {"environment": environment.name})
  109. self.assertContains(res, group1.transaction)
  110. self.assertNotContains(res, group2.transaction)
  111. def test_filter_then_average(self):
  112. group = baker.make("performance.TransactionGroup", project=self.project)
  113. now = timezone.now()
  114. last_minute = now - datetime.timedelta(minutes=1)
  115. with freeze_time(last_minute):
  116. baker.make(
  117. "performance.TransactionEvent",
  118. group=group,
  119. start_timestamp=last_minute,
  120. timestamp=last_minute + datetime.timedelta(seconds=5),
  121. duration=datetime.timedelta(seconds=5),
  122. )
  123. transaction2 = baker.make(
  124. "performance.TransactionEvent",
  125. group=group,
  126. start_timestamp=now,
  127. timestamp=now + datetime.timedelta(seconds=1),
  128. duration=datetime.timedelta(seconds=1),
  129. )
  130. res = self.client.get(self.list_url)
  131. self.assertEqual(res.data[0]["avgDuration"], "00:00:03")
  132. res = self.client.get(
  133. self.list_url
  134. + "?start="
  135. + transaction2.created.replace(microsecond=0)
  136. .replace(tzinfo=None)
  137. .isoformat()
  138. + "Z"
  139. )
  140. self.assertEqual(res.data[0]["avgDuration"], "00:00:01")
  141. class SpanAPITestCase(GlitchTipTestCase):
  142. def setUp(self):
  143. self.create_user_and_project()
  144. self.list_url = reverse(
  145. "organization-spans-list",
  146. kwargs={"organization_slug": self.organization.slug},
  147. )
  148. def test_list(self):
  149. span = baker.make("performance.Span", transaction__group__project=self.project)
  150. res = self.client.get(self.list_url)
  151. self.assertContains(res, span.op)