123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- from django.urls import reverse
- from rest_framework.exceptions import ErrorDetail
- from sentry.testutils.cases import APITestCase, SnubaTestCase
- from sentry.testutils.helpers.datetime import before_now
- from sentry.utils.samples import load_data
- class OrganizationEventsHasMeasurementsTest(APITestCase, SnubaTestCase):
- def setUp(self):
- super().setUp()
- self.min_ago = before_now(minutes=1)
- self.two_min_ago = before_now(minutes=2)
- self.transaction_data = load_data("transaction", timestamp=before_now(minutes=1))
- self.features = {}
- def do_request(self, query, features=None):
- if features is None:
- features = {
- "organizations:discover-basic": True,
- "organizations:global-views": True,
- }
- features.update(self.features)
- self.login_as(user=self.user)
- url = reverse(
- "sentry-api-0-organization-events-has-measurements",
- kwargs={"organization_id_or_slug": self.organization.slug},
- )
- with self.feature(features):
- return self.client.get(url, query, format="json")
- def test_without_feature(self):
- response = self.do_request({}, features={"organizations:discover-basic": False})
- assert response.status_code == 404, response.content
- def test_no_projects(self):
- response = self.do_request({})
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": False}
- def test_more_than_one_project(self):
- project = self.create_project()
- response = self.do_request(
- {
- "project": [self.project.id, project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "web",
- }
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "non_field_errors": [ErrorDetail("Only 1 project allowed.", code="invalid")],
- }
- def test_no_transaction(self):
- response = self.do_request(
- {
- "project": [self.project.id],
- "type": "web",
- }
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "transaction": [ErrorDetail("This field may not be null.", code="null")],
- }
- def test_no_type(self):
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- }
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "type": [ErrorDetail("This field may not be null.", code="null")],
- }
- def test_unknown_type(self):
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "foo",
- }
- )
- assert response.status_code == 400, response.content
- assert response.data == {
- "type": [ErrorDetail('"foo" is not a valid choice.', code="invalid_choice")],
- }
- def test_no_events(self):
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "web",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": False}
- def test_has_event_but_no_web_measurements(self):
- # make sure the transaction doesn't have measurements
- self.transaction_data["measurements"] = {}
- self.store_event(self.transaction_data, self.project.id)
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "web",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": False}
- def test_has_event_and_no_recent_web_measurements(self):
- # make sure the event is older than 7 days
- transaction_data = load_data("transaction", timestamp=before_now(days=8))
- # make sure the transaction has some web measurements
- transaction_data["measurements"] = {"lcp": {"value": 100}}
- self.store_event(transaction_data, self.project.id)
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "web",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": False}
- def test_has_event_and_web_measurements(self):
- # make sure the transaction has some web measurements
- self.transaction_data["measurements"] = {"lcp": {"value": 100}}
- self.store_event(self.transaction_data, self.project.id)
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "web",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": True}
- def test_has_event_and_no_recent_mobile_measurements(self):
- # make sure the event is older than 7 days
- transaction_data = load_data("transaction", timestamp=before_now(days=8))
- # make sure the transaction has some web measurements
- transaction_data["measurements"] = {"app_start_cold": {"value": 100}}
- self.store_event(transaction_data, self.project.id)
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "mobile",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": False}
- def test_has_event_and_mobile_measurements(self):
- # make sure the transaction has some mobile measurements
- self.transaction_data["measurements"] = {"app_start_cold": {"value": 100}}
- self.store_event(self.transaction_data, self.project.id)
- response = self.do_request(
- {
- "project": [self.project.id],
- "transaction": self.transaction_data["transaction"],
- "type": "mobile",
- }
- )
- assert response.status_code == 200, response.content
- assert response.data == {"measurements": True}
|