test_api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. baker.make(
  30. "performance.TransactionEvent", group=group,
  31. )
  32. res = self.client.get(self.list_url)
  33. self.assertContains(res, group.transaction)
  34. def test_list_environment_filter(self):
  35. environment_project = baker.make(
  36. "environments.EnvironmentProject",
  37. environment__organization=self.organization,
  38. )
  39. environment = environment_project.environment
  40. environment.projects.add(self.project)
  41. group1 = baker.make(
  42. "performance.TransactionGroup",
  43. project=self.project,
  44. tags={"environment": [environment.name]},
  45. )
  46. baker.make(
  47. "performance.TransactionEvent", group=group1,
  48. )
  49. group2 = baker.make("performance.TransactionGroup", project=self.project)
  50. baker.make(
  51. "performance.TransactionEvent", group=group2,
  52. )
  53. res = self.client.get(self.list_url, {"environment": environment.name})
  54. self.assertContains(res, group1.transaction)
  55. self.assertNotContains(res, group2.transaction)
  56. def test_filter_then_average(self):
  57. group = baker.make("performance.TransactionGroup", project=self.project)
  58. now = timezone.now()
  59. last_minute = now - datetime.timedelta(minutes=1)
  60. with freeze_time(last_minute):
  61. baker.make(
  62. "performance.TransactionEvent",
  63. group=group,
  64. start_timestamp=last_minute,
  65. timestamp=last_minute + datetime.timedelta(seconds=5),
  66. duration=datetime.timedelta(seconds=5),
  67. )
  68. transaction2 = baker.make(
  69. "performance.TransactionEvent",
  70. group=group,
  71. start_timestamp=now,
  72. timestamp=now + datetime.timedelta(seconds=1),
  73. duration=datetime.timedelta(seconds=1),
  74. )
  75. res = self.client.get(self.list_url)
  76. self.assertEqual(res.data[0]["avgDuration"], "00:00:03")
  77. res = self.client.get(
  78. self.list_url
  79. + "?start="
  80. + transaction2.created.replace(microsecond=0)
  81. .replace(tzinfo=None)
  82. .isoformat()
  83. + "Z"
  84. )
  85. self.assertEqual(res.data[0]["avgDuration"], "00:00:01")
  86. class SpanAPITestCase(GlitchTipTestCase):
  87. def setUp(self):
  88. self.create_user_and_project()
  89. self.list_url = reverse(
  90. "organization-spans-list",
  91. kwargs={"organization_slug": self.organization.slug},
  92. )
  93. def test_list(self):
  94. span = baker.make("performance.Span", transaction__group__project=self.project)
  95. res = self.client.get(self.list_url)
  96. self.assertContains(res, span.op)