Skip to content

Commit 43dd1a2

Browse files
asottile-sentrybillyvg
authored andcommitted
ref: convert a few modules to non-django unit tests (#89381)
this makes almost zero difference in the overall testsuite -- but can improve individual module performance when testing locally. since I did the work I figure I might as well merge it but I'm going to not spend any more time converting other ones 🤷 <!-- Describe your PR here. -->
1 parent 38b3f9a commit 43dd1a2

File tree

51 files changed

+443
-401
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+443
-401
lines changed

src/sentry/testutils/cases.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import annotations
22

33
import hashlib
4-
import importlib.metadata
54
import inspect
65
import os.path
76
import random
@@ -976,23 +975,6 @@ def setUp(self):
976975
plugins.register(self.plugin)
977976
self.addCleanup(plugins.unregister, self.plugin)
978977

979-
def assertAppInstalled(self, name, path):
980-
for ep in importlib.metadata.distribution("sentry").entry_points:
981-
if ep.group == "sentry.apps" and ep.name == name:
982-
assert ep.value == path
983-
return
984-
else:
985-
self.fail(f"Missing app from entry_points: {name!r}")
986-
987-
def assertPluginInstalled(self, name, plugin):
988-
path = type(plugin).__module__ + ":" + type(plugin).__name__
989-
for ep in importlib.metadata.distribution("sentry").entry_points:
990-
if ep.group == "sentry.plugins" and ep.name == name:
991-
assert ep.value == path
992-
return
993-
else:
994-
self.fail(f"Missing plugin from entry_points: {name!r}")
995-
996978

997979
class CliTestCase(TestCase):
998980
@cached_property
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import importlib.metadata
2+
3+
4+
def _get_ep(group: str, name: str) -> importlib.metadata.EntryPoint | None:
5+
for ep in importlib.metadata.distribution("sentry").entry_points:
6+
if ep.group == group and ep.name == name:
7+
return ep
8+
else:
9+
return None
10+
11+
12+
def assert_plugin_installed(name: str, plugin: object) -> None:
13+
path = type(plugin).__module__ + ":" + type(plugin).__name__
14+
ep = _get_ep("sentry.plugins", name)
15+
assert ep is not None and ep.value == path
16+
17+
18+
def assert_app_installed(name: str, path: str) -> None:
19+
ep = _get_ep("sentry.apps", name)
20+
assert ep is not None and ep.value == path

tests/apidocs/test_hooks.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
from unittest import TestCase
2+
13
from sentry.apidocs.hooks import custom_postprocessing_hook
2-
from sentry.testutils.cases import TestCase
34

45

56
class FixIssueRoutesTest(TestCase):

tests/sentry/api/endpoints/release_thresholds/health_checks/test_is_crash_free_rate_healthy.py

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@
1818
from .test_fixtures import mock_sessions_data
1919

2020

21-
class GetIntervalIndexesTest(TestCase):
22-
def setUp(self):
21+
class TestGetIntervalIndexes:
22+
def setup_method(self):
2323
# d.strftime('%Y-%m-%dT%H:%M:%SZ')
2424
# construct timestamps in iso utc format
2525
self.now = datetime.utcnow()
26-
offsets = [r for r in range(-5, 5)]
2726
self.intervals = [
28-
(self.now + timedelta(hours=x)).strftime("%Y-%m-%dT%H:%M:%SZ") for x in offsets
27+
(self.now + timedelta(hours=x)).strftime("%Y-%m-%dT%H:%M:%SZ") for x in range(-5, 5)
2928
]
3029

3130
def test_gets_indexes_range_in_intervals(self):
@@ -59,14 +58,10 @@ def test_returns_bad_idxs_when_not_within_intervals(self):
5958
assert start_idx > end_idx
6059

6160

62-
class GetGroupTotals(TestCase):
63-
def setUp(self):
64-
# Mock data has 10 intervals
65-
self.mock_sessions_data = mock_sessions_data
66-
61+
class TestGetGroupTotals:
6762
def test_filters_groups_and_sums_total_success(self):
6863
total_v1 = get_groups_totals(
69-
sessions_data=self.mock_sessions_data,
64+
sessions_data=mock_sessions_data,
7065
release_version="version1",
7166
project_id=1,
7267
field="sum(session)",
@@ -76,7 +71,7 @@ def test_filters_groups_and_sums_total_success(self):
7671
assert total_v1 == 11
7772
# filters via release version
7873
total_v2 = get_groups_totals(
79-
sessions_data=self.mock_sessions_data,
74+
sessions_data=mock_sessions_data,
8075
release_version="version2",
8176
project_id=1,
8277
field="sum(session)",
@@ -87,7 +82,7 @@ def test_filters_groups_and_sums_total_success(self):
8782

8883
def test_filters_group_by_project(self):
8984
total_p2_v1 = get_groups_totals(
90-
sessions_data=self.mock_sessions_data,
85+
sessions_data=mock_sessions_data,
9186
release_version="version1",
9287
project_id=2,
9388
field="sum(session)",
@@ -97,7 +92,7 @@ def test_filters_group_by_project(self):
9792
assert total_p2_v1 == 0
9893

9994
total_p2_v2 = get_groups_totals(
100-
sessions_data=self.mock_sessions_data,
95+
sessions_data=mock_sessions_data,
10196
release_version="version2",
10297
project_id=2,
10398
field="sum(session)",
@@ -108,7 +103,7 @@ def test_filters_group_by_project(self):
108103

109104
def test_filters_group_by_environment(self):
110105
total_canary = get_groups_totals(
111-
sessions_data=self.mock_sessions_data,
106+
sessions_data=mock_sessions_data,
112107
release_version="version1",
113108
project_id=1,
114109
field="sum(session)",
@@ -118,7 +113,7 @@ def test_filters_group_by_environment(self):
118113
)
119114
assert total_canary == 0
120115
total_production = get_groups_totals(
121-
sessions_data=self.mock_sessions_data,
116+
sessions_data=mock_sessions_data,
122117
release_version="version1",
123118
project_id=1,
124119
field="sum(session)",
@@ -130,7 +125,7 @@ def test_filters_group_by_environment(self):
130125

131126
def test_filters_group_by_status(self):
132127
crashed = get_groups_totals(
133-
sessions_data=self.mock_sessions_data,
128+
sessions_data=mock_sessions_data,
134129
release_version="version1",
135130
project_id=1,
136131
field="sum(session)",
@@ -142,7 +137,7 @@ def test_filters_group_by_status(self):
142137

143138
def test_sums_group_via_indexes(self):
144139
total_5_9 = get_groups_totals(
145-
sessions_data=self.mock_sessions_data,
140+
sessions_data=mock_sessions_data,
146141
release_version="version1",
147142
project_id=1,
148143
field="sum(session)",
@@ -154,7 +149,7 @@ def test_sums_group_via_indexes(self):
154149
def test_raises_errors_with_bad_indexes(self):
155150
with pytest.raises(IndexError):
156151
get_groups_totals(
157-
sessions_data=self.mock_sessions_data,
152+
sessions_data=mock_sessions_data,
158153
release_version="version1",
159154
project_id=1,
160155
field="sum(session)",
@@ -164,7 +159,7 @@ def test_raises_errors_with_bad_indexes(self):
164159

165160
with pytest.raises(IndexError):
166161
get_groups_totals(
167-
sessions_data=self.mock_sessions_data,
162+
sessions_data=mock_sessions_data,
168163
release_version="version1",
169164
project_id=1,
170165
field="sum(session)",

tests/sentry/api/endpoints/test_index.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,19 @@
66
from sentry.testutils.silo import control_silo_test
77

88

9+
@control_silo_test
10+
def test_anonymous(client):
11+
response = client.get(reverse("sentry-api-index"))
12+
assert response.status_code == 200
13+
assert response.data["version"] == "0"
14+
assert not response.data["user"]
15+
assert not response.data["auth"]
16+
17+
918
@control_silo_test
1019
class ApiIndexTest(APITestCase):
1120
endpoint = "sentry-api-index"
1221

13-
def test_anonymous(self):
14-
response = self.get_success_response()
15-
assert response.data["version"] == "0"
16-
assert not response.data["user"]
17-
assert not response.data["auth"]
18-
1922
def test_session_auth(self):
2023
self.login_as(user=self.user)
2124
response = self.get_success_response()

tests/sentry/auth/providers/test_saml2.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
from __future__ import annotations
22

33
from typing import Any
4-
from unittest import mock
4+
from unittest import TestCase, mock
55

66
import pytest
77

88
from sentry.auth.exceptions import IdentityNotValid
99
from sentry.auth.providers.saml2.provider import Attributes, SAML2Provider
10-
from sentry.testutils.cases import TestCase
1110
from sentry.testutils.silo import control_silo_test
1211

1312
dummy_provider_config = {

tests/sentry/backup/test_sanitize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from collections.abc import Sequence
22
from datetime import datetime, timedelta
33
from typing import Any
4+
from unittest import TestCase
45
from unittest.mock import Mock, patch
56

67
import orjson
@@ -24,7 +25,6 @@
2425
from sentry.db.models.fields.jsonfield import JSONField
2526
from sentry.db.models.fields.slug import SentrySlugField
2627
from sentry.db.models.fields.uuid import UUIDField
27-
from sentry.testutils.cases import TestCase
2828

2929
FAKE_EMAIL = "test@fake.com"
3030
FAKE_NAME = "Fake Name"

tests/sentry/cache/test_django.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import time
2+
from unittest import TestCase
23

34
from sentry.cache.django import DjangoCache
4-
from sentry.testutils.cases import TestCase
55

66

77
class DjangoCacheTest(TestCase):

0 commit comments

Comments
 (0)