|
| 1 | +from unittest.mock import Mock, patch |
| 2 | + |
| 3 | +from django.http import QueryDict |
| 4 | +from django.test import RequestFactory |
| 5 | +from rest_framework.request import Request |
| 6 | + |
| 7 | +from sentry.api.helpers.error_upsampling import ( |
| 8 | + _are_all_projects_error_upsampled, |
| 9 | + _is_error_focused_query, |
| 10 | + _should_apply_sample_weight_transform, |
| 11 | + transform_query_columns_for_error_upsampling, |
| 12 | +) |
| 13 | +from sentry.models.organization import Organization |
| 14 | +from sentry.search.events.types import SnubaParams |
| 15 | +from sentry.snuba import discover, errors, transactions |
| 16 | +from sentry.testutils.cases import TestCase |
| 17 | + |
| 18 | + |
| 19 | +class ErrorUpsamplingTest(TestCase): |
| 20 | + def setUp(self) -> None: |
| 21 | + self.organization = Organization.objects.create(name="test-org") |
| 22 | + self.projects = [ |
| 23 | + self.create_project(organization=self.organization, name="Project 1"), |
| 24 | + self.create_project(organization=self.organization, name="Project 2"), |
| 25 | + self.create_project(organization=self.organization, name="Project 3"), |
| 26 | + ] |
| 27 | + self.project_ids = [p.id for p in self.projects] |
| 28 | + self.snuba_params = SnubaParams( |
| 29 | + start=None, |
| 30 | + end=None, |
| 31 | + projects=self.projects, |
| 32 | + ) |
| 33 | + factory = RequestFactory() |
| 34 | + self.request = Request(factory.get("/")) |
| 35 | + self.request.GET = QueryDict("") |
| 36 | + |
| 37 | + @patch("sentry.api.helpers.error_upsampling.options") |
| 38 | + def test_are_all_projects_error_upsampled(self, mock_options: Mock) -> None: |
| 39 | + # Test when all projects are allowlisted |
| 40 | + mock_options.get.return_value = self.project_ids |
| 41 | + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is True |
| 42 | + |
| 43 | + # Test when some projects are not allowlisted |
| 44 | + mock_options.get.return_value = self.project_ids[:-1] |
| 45 | + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is False |
| 46 | + |
| 47 | + # Test when no projects are allowlisted |
| 48 | + mock_options.get.return_value = [] |
| 49 | + assert _are_all_projects_error_upsampled(self.project_ids, self.organization) is False |
| 50 | + |
| 51 | + # Test when no project IDs provided |
| 52 | + assert _are_all_projects_error_upsampled([], self.organization) is False |
| 53 | + |
| 54 | + def test_transform_query_columns_for_error_upsampling(self) -> None: |
| 55 | + # Test count() transformation |
| 56 | + columns = ["count()", "other_column"] |
| 57 | + expected = [ |
| 58 | + "upsampled_count() as count", |
| 59 | + "other_column", |
| 60 | + ] |
| 61 | + assert transform_query_columns_for_error_upsampling(columns) == expected |
| 62 | + |
| 63 | + # Test case insensitivity |
| 64 | + columns = ["COUNT()"] |
| 65 | + expected = [ |
| 66 | + "upsampled_count() as count", |
| 67 | + ] |
| 68 | + assert transform_query_columns_for_error_upsampling(columns) == expected |
| 69 | + |
| 70 | + # Test whitespace handling |
| 71 | + columns = [" count() "] |
| 72 | + expected = [ |
| 73 | + "upsampled_count() as count", |
| 74 | + ] |
| 75 | + assert transform_query_columns_for_error_upsampling(columns) == expected |
| 76 | + |
| 77 | + def test_is_error_focused_query(self) -> None: |
| 78 | + # Test explicit error type |
| 79 | + self.request.GET = QueryDict("query=event.type:error") |
| 80 | + assert _is_error_focused_query(self.request) is True |
| 81 | + |
| 82 | + # Test explicit transaction type |
| 83 | + self.request.GET = QueryDict("query=event.type:transaction") |
| 84 | + assert _is_error_focused_query(self.request) is False |
| 85 | + |
| 86 | + # Test empty query |
| 87 | + self.request.GET = QueryDict("") |
| 88 | + assert _is_error_focused_query(self.request) is False |
| 89 | + |
| 90 | + def test_should_apply_sample_weight_transform(self) -> None: |
| 91 | + # Test errors dataset |
| 92 | + assert _should_apply_sample_weight_transform(errors, self.request) is True |
| 93 | + |
| 94 | + # Test transactions dataset |
| 95 | + assert _should_apply_sample_weight_transform(transactions, self.request) is False |
| 96 | + |
| 97 | + self.request.GET = QueryDict("query=event.type:error") |
| 98 | + assert _should_apply_sample_weight_transform(discover, self.request) is True |
| 99 | + |
| 100 | + self.request.GET = QueryDict("query=event.type:transaction") |
| 101 | + assert _should_apply_sample_weight_transform(discover, self.request) is False |
0 commit comments