test_api.py 3.4 KB

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