test_organization_events_has_measurements.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. from django.urls import reverse
  2. from rest_framework.exceptions import ErrorDetail
  3. from sentry.testutils.cases import APITestCase, SnubaTestCase
  4. from sentry.testutils.helpers.datetime import before_now, iso_format
  5. from sentry.testutils.silo import region_silo_test
  6. from sentry.utils.samples import load_data
  7. @region_silo_test
  8. class OrganizationEventsHasMeasurementsTest(APITestCase, SnubaTestCase):
  9. def setUp(self):
  10. super().setUp()
  11. self.min_ago = iso_format(before_now(minutes=1))
  12. self.two_min_ago = iso_format(before_now(minutes=2))
  13. self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
  14. self.features = {}
  15. def do_request(self, query, features=None):
  16. if features is None:
  17. features = {
  18. "organizations:discover-basic": True,
  19. "organizations:global-views": True,
  20. }
  21. features.update(self.features)
  22. self.login_as(user=self.user)
  23. url = reverse(
  24. "sentry-api-0-organization-events-has-measurements",
  25. kwargs={"organization_slug": self.organization.slug},
  26. )
  27. with self.feature(features):
  28. return self.client.get(url, query, format="json")
  29. def test_without_feature(self):
  30. response = self.do_request({}, features={"organizations:discover-basic": False})
  31. assert response.status_code == 404, response.content
  32. def test_no_projects(self):
  33. response = self.do_request({})
  34. assert response.status_code == 200, response.content
  35. assert response.data == {"measurements": False}
  36. def test_more_than_one_project(self):
  37. project = self.create_project()
  38. response = self.do_request(
  39. {
  40. "project": [self.project.id, project.id],
  41. "transaction": self.transaction_data["transaction"],
  42. "type": "web",
  43. }
  44. )
  45. assert response.status_code == 400, response.content
  46. assert response.data == {
  47. "non_field_errors": [ErrorDetail("Only 1 project allowed.", code="invalid")],
  48. }
  49. def test_no_transaction(self):
  50. response = self.do_request(
  51. {
  52. "project": [self.project.id],
  53. "type": "web",
  54. }
  55. )
  56. assert response.status_code == 400, response.content
  57. assert response.data == {
  58. "transaction": [ErrorDetail("This field may not be null.", code="null")],
  59. }
  60. def test_no_type(self):
  61. response = self.do_request(
  62. {
  63. "project": [self.project.id],
  64. "transaction": self.transaction_data["transaction"],
  65. }
  66. )
  67. assert response.status_code == 400, response.content
  68. assert response.data == {
  69. "type": [ErrorDetail("This field may not be null.", code="null")],
  70. }
  71. def test_unknown_type(self):
  72. response = self.do_request(
  73. {
  74. "project": [self.project.id],
  75. "transaction": self.transaction_data["transaction"],
  76. "type": "foo",
  77. }
  78. )
  79. assert response.status_code == 400, response.content
  80. assert response.data == {
  81. "type": [ErrorDetail('"foo" is not a valid choice.', code="invalid_choice")],
  82. }
  83. def test_no_events(self):
  84. response = self.do_request(
  85. {
  86. "project": [self.project.id],
  87. "transaction": self.transaction_data["transaction"],
  88. "type": "web",
  89. }
  90. )
  91. assert response.status_code == 200, response.content
  92. assert response.data == {"measurements": False}
  93. def test_has_event_but_no_web_measurements(self):
  94. # make sure the transaction doesnt have measurements
  95. self.transaction_data["measurements"] = {}
  96. self.store_event(self.transaction_data, self.project.id)
  97. response = self.do_request(
  98. {
  99. "project": [self.project.id],
  100. "transaction": self.transaction_data["transaction"],
  101. "type": "web",
  102. }
  103. )
  104. assert response.status_code == 200, response.content
  105. assert response.data == {"measurements": False}
  106. def test_has_event_and_no_recent_web_measurements(self):
  107. # make sure the event is older than 7 days
  108. transaction_data = load_data("transaction", timestamp=before_now(days=8))
  109. # make sure the transaction has some web measurements
  110. transaction_data["measurements"] = {"lcp": {"value": 100}}
  111. self.store_event(transaction_data, self.project.id)
  112. response = self.do_request(
  113. {
  114. "project": [self.project.id],
  115. "transaction": self.transaction_data["transaction"],
  116. "type": "web",
  117. }
  118. )
  119. assert response.status_code == 200, response.content
  120. assert response.data == {"measurements": False}
  121. def test_has_event_and_web_measurements(self):
  122. # make sure the transaction has some web measurements
  123. self.transaction_data["measurements"] = {"lcp": {"value": 100}}
  124. self.store_event(self.transaction_data, self.project.id)
  125. response = self.do_request(
  126. {
  127. "project": [self.project.id],
  128. "transaction": self.transaction_data["transaction"],
  129. "type": "web",
  130. }
  131. )
  132. assert response.status_code == 200, response.content
  133. assert response.data == {"measurements": True}
  134. def test_has_event_and_no_recent_mobile_measurements(self):
  135. # make sure the event is older than 7 days
  136. transaction_data = load_data("transaction", timestamp=before_now(days=8))
  137. # make sure the transaction has some web measurements
  138. transaction_data["measurements"] = {"app_start_cold": {"value": 100}}
  139. self.store_event(transaction_data, self.project.id)
  140. response = self.do_request(
  141. {
  142. "project": [self.project.id],
  143. "transaction": self.transaction_data["transaction"],
  144. "type": "mobile",
  145. }
  146. )
  147. assert response.status_code == 200, response.content
  148. assert response.data == {"measurements": False}
  149. def test_has_event_and_mobile_measurements(self):
  150. # make sure the transaction has some mobile measurements
  151. self.transaction_data["measurements"] = {"app_start_cold": {"value": 100}}
  152. self.store_event(self.transaction_data, self.project.id)
  153. response = self.do_request(
  154. {
  155. "project": [self.project.id],
  156. "transaction": self.transaction_data["transaction"],
  157. "type": "mobile",
  158. }
  159. )
  160. assert response.status_code == 200, response.content
  161. assert response.data == {"measurements": True}