test_discover_homepage_query.py 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import pytest
  2. from django.urls import reverse
  3. from sentry.api.serializers import serialize
  4. from sentry.discover.models import DiscoverSavedQuery, DiscoverSavedQueryTypes
  5. from sentry.testutils.cases import BaseMetricsTestCase
  6. from sentry.testutils.helpers.datetime import before_now
  7. from tests.snuba.api.endpoints.test_discover_saved_queries import DiscoverSavedQueryBase
  8. FEATURES = ("organizations:discover-query", "organizations:performance-use-metrics")
  9. pytestmark = pytest.mark.sentry_metrics
  10. class DiscoverHomepageQueryTest(DiscoverSavedQueryBase):
  11. def setUp(self):
  12. super().setUp()
  13. self.url = reverse("sentry-api-0-discover-homepage-query", args=[self.org.slug])
  14. self.query = {"fields": ["test"], "conditions": [], "limit": 10}
  15. self.project_ids = [
  16. self.create_project(organization=self.org).id,
  17. self.create_project(organization=self.org).id,
  18. ]
  19. def test_returns_no_response_if_no_homepage_query_for_user(self):
  20. with self.feature(FEATURES):
  21. response = self.client.get(self.url)
  22. assert response.status_code == 204, response.content
  23. assert response.data is None
  24. def test_returns_serialized_saved_query_if_homepage_is_set(self):
  25. saved_query = DiscoverSavedQuery.objects.create(
  26. organization=self.org,
  27. created_by_id=self.user.id,
  28. name="Test query",
  29. query=self.query,
  30. is_homepage=True,
  31. )
  32. with self.feature(FEATURES):
  33. response = self.client.get(self.url)
  34. assert response.status_code == 200, response.content
  35. assert response.data == serialize(saved_query)
  36. def test_put_updates_existing_homepage_query_to_reflect_new_data(self):
  37. saved_query = DiscoverSavedQuery.objects.create(
  38. organization=self.org,
  39. created_by_id=self.user.id,
  40. name="Test query",
  41. query=self.query,
  42. dataset=DiscoverSavedQueryTypes.DISCOVER,
  43. is_homepage=True,
  44. )
  45. with self.feature(FEATURES):
  46. response = self.client.put(
  47. self.url,
  48. {
  49. "name": "A new homepage query update",
  50. "projects": self.project_ids,
  51. "fields": ["field1", "field2"],
  52. "queryDataset": DiscoverSavedQueryTypes.get_type_name(
  53. DiscoverSavedQueryTypes.TRANSACTION_LIKE
  54. ),
  55. },
  56. )
  57. assert response.status_code == 200, response.content
  58. saved_query.refresh_from_db()
  59. assert response.data == serialize(saved_query)
  60. assert saved_query.query["fields"] == ["field1", "field2"]
  61. assert saved_query.dataset == DiscoverSavedQueryTypes.TRANSACTION_LIKE
  62. assert set(saved_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  63. def test_put_creates_new_discover_saved_query_if_none_exists(self):
  64. homepage_query_payload = {
  65. "version": 2,
  66. "name": "New Homepage Query",
  67. "projects": self.project_ids,
  68. "environment": ["alpha"],
  69. "fields": ["environment", "platform.name"],
  70. "orderby": "-timestamp",
  71. "range": None,
  72. }
  73. with self.feature(FEATURES):
  74. response = self.client.put(self.url, data=homepage_query_payload)
  75. assert response.status_code == 201, response.content
  76. new_query = DiscoverSavedQuery.objects.get(
  77. created_by_id=self.user.id, organization=self.org, is_homepage=True
  78. )
  79. assert response.data == serialize(new_query)
  80. assert new_query.query["fields"] == homepage_query_payload["fields"]
  81. assert new_query.query["environment"] == homepage_query_payload["environment"]
  82. assert new_query.dataset == DiscoverSavedQueryTypes.get_id_for_type_name("discover")
  83. assert set(new_query.projects.values_list("id", flat=True)) == set(self.project_ids)
  84. def test_put_responds_with_saved_empty_name_field(self):
  85. homepage_query_payload = {
  86. "version": 2,
  87. "name": "New Homepage Query",
  88. "projects": self.project_ids,
  89. "environment": ["alpha"],
  90. "fields": ["environment", "platform.name"],
  91. "orderby": "-timestamp",
  92. "range": None,
  93. }
  94. with self.feature(FEATURES):
  95. response = self.client.put(self.url, data=homepage_query_payload)
  96. assert response.status_code == 201, response.content
  97. new_query = DiscoverSavedQuery.objects.get(
  98. created_by_id=self.user.id, organization=self.org, is_homepage=True
  99. )
  100. assert new_query.name == ""
  101. assert response.data["name"] == ""
  102. def test_put_with_no_name(self):
  103. homepage_query_payload = {
  104. "version": 2,
  105. "name": "",
  106. "projects": self.project_ids,
  107. "environment": ["alpha"],
  108. "fields": ["environment", "platform.name"],
  109. "orderby": "-timestamp",
  110. "range": None,
  111. }
  112. with self.feature(FEATURES):
  113. response = self.client.put(self.url, data=homepage_query_payload)
  114. assert response.status_code == 201, response.content
  115. new_query = DiscoverSavedQuery.objects.get(
  116. created_by_id=self.user.id, organization=self.org, is_homepage=True
  117. )
  118. assert new_query.name == ""
  119. assert response.data["name"] == ""
  120. def test_post_not_allowed(self):
  121. homepage_query_payload = {
  122. "version": 2,
  123. "name": "New Homepage Query",
  124. "projects": ["-1"],
  125. "environment": ["alpha"],
  126. "fields": ["environment", "platform.name"],
  127. "orderby": "-timestamp",
  128. "range": None,
  129. }
  130. with self.feature(FEATURES):
  131. response = self.client.post(self.url, data=homepage_query_payload)
  132. assert response.status_code == 405, response.content
  133. def test_delete_resets_saved_query(self):
  134. DiscoverSavedQuery.objects.create(
  135. organization=self.org,
  136. created_by_id=self.user.id,
  137. name="Test query",
  138. query=self.query,
  139. is_homepage=True,
  140. )
  141. with self.feature(FEATURES):
  142. response = self.client.delete(self.url)
  143. assert response.status_code == 204
  144. assert not DiscoverSavedQuery.objects.filter(
  145. created_by_id=self.user.id, organization=self.org, is_homepage=True
  146. ).exists()
  147. def test_put_allows_custom_measurements_in_equations_with_query(self):
  148. # Having a custom measurement stored implies that a transaction with this measurement has been stored
  149. BaseMetricsTestCase.store_metric(
  150. self.org.id,
  151. self.project_ids[0],
  152. "d:transactions/measurements.custom_duration@millisecond",
  153. {},
  154. int(before_now(days=1).timestamp()),
  155. 1,
  156. )
  157. homepage_query_payload = {
  158. "version": 2,
  159. "name": "New Homepage Query",
  160. "projects": [self.project_ids[0]],
  161. "environment": ["alpha"],
  162. "fields": [
  163. "transaction.duration",
  164. "measurements.custom_duration",
  165. "equation|measurements.custom_duration / transaction.duration",
  166. ],
  167. "orderby": "-transaction.duration",
  168. "query": "test",
  169. "range": None,
  170. }
  171. with self.feature(FEATURES):
  172. response = self.client.put(self.url, data=homepage_query_payload)
  173. assert response.status_code == 201, response.content
  174. new_query = DiscoverSavedQuery.objects.get(
  175. created_by_id=self.user.id, organization=self.org, is_homepage=True
  176. )
  177. assert response.data == serialize(new_query)
  178. assert list(new_query.projects.values_list("id", flat=True)) == [self.project_ids[0]]