Skip to content

Commit de61b8b

Browse files
authored
fix(tests): Improper skip_if_no_analytics_db marker behaviour (#5876)
1 parent 22fedbf commit de61b8b

6 files changed

Lines changed: 34 additions & 42 deletions

File tree

api/tests/unit/app_analytics/conftest.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,24 @@
33

44

55
@pytest.fixture
6-
def skip_if_no_analytics_db() -> None:
6+
def use_analytics_db(request: pytest.FixtureRequest) -> None:
77
"""
8-
Skip tests if no analytics database is configured.
8+
Skip tests if no analytics database is configured,
9+
and make sure the django_db fixture uses both default and analytics databases.
910
This is useful to avoid running tests that require a specific database setup.
1011
"""
1112
if "analytics" not in settings.DATABASES: # pragma: no cover
1213
pytest.skip("No analytics database configured, skipping")
14+
return
15+
request.applymarker(pytest.mark.django_db(databases=["default", "analytics"]))
16+
request.getfixturevalue("db")
1317

1418

1519
@pytest.fixture(autouse=True)
16-
def skip_if_no_analytics_db_marked(request: pytest.FixtureRequest) -> None:
20+
def use_analytics_db_marked(request: pytest.FixtureRequest) -> None:
1721
"""
18-
Automatically skip tests that are marked with 'skip_if_no_analytics_db'.
22+
Automatically skip tests that are marked with 'use_analytics_db'.
1923
This allows for selective skipping of tests based on the database configuration.
2024
"""
21-
if request.node.get_closest_marker("skip_if_no_analytics_db"):
22-
request.getfixturevalue("skip_if_no_analytics_db")
25+
if request.node.get_closest_marker("use_analytics_db"):
26+
request.getfixturevalue("use_analytics_db")

api/tests/unit/app_analytics/test_analytics_db_service.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ def cache(organisation: Organisation) -> OrganisationSubscriptionInformationCach
4444
)
4545

4646

47-
@pytest.mark.skip_if_no_analytics_db
48-
@pytest.mark.django_db(databases=["analytics", "default"])
47+
@pytest.mark.use_analytics_db
4948
def test_get_usage_data_from_local_db(organisation, environment, settings): # type: ignore[no-untyped-def]
5049
environment_id = environment.id
5150
now = timezone.now()
@@ -102,8 +101,7 @@ def test_get_usage_data_from_local_db(organisation, environment, settings): # t
102101
assert data.day == today - timedelta(days=29 - count)
103102

104103

105-
@pytest.mark.skip_if_no_analytics_db
106-
@pytest.mark.django_db(databases=["analytics", "default"])
104+
@pytest.mark.use_analytics_db
107105
def test_get_usage_data_from_local_db_project_id_filter( # type: ignore[no-untyped-def]
108106
organisation: Organisation,
109107
project: Project,
@@ -150,8 +148,7 @@ def test_get_usage_data_from_local_db_project_id_filter( # type: ignore[no-unty
150148
assert list(usage_data_for_project_two)[0].flags == total_count # 1 environment
151149

152150

153-
@pytest.mark.skip_if_no_analytics_db
154-
@pytest.mark.django_db(databases=["analytics", "default"])
151+
@pytest.mark.use_analytics_db
155152
def test_get_usage_data_from_local_db__environment_filter__returns_expected(
156153
organisation: Organisation,
157154
environment: Environment,
@@ -197,8 +194,7 @@ def test_get_usage_data_from_local_db__environment_filter__returns_expected(
197194
]
198195

199196

200-
@pytest.mark.skip_if_no_analytics_db
201-
@pytest.mark.django_db(databases=["analytics", "default"])
197+
@pytest.mark.use_analytics_db
202198
def test_get_usage_data_from_local_db__labels_filter__returns_expected(
203199
organisation: Organisation,
204200
environment: Environment,
@@ -270,8 +266,7 @@ def test_get_usage_data_from_local_db__labels_filter__returns_expected(
270266
]
271267

272268

273-
@pytest.mark.skip_if_no_analytics_db
274-
@pytest.mark.django_db(databases=["analytics", "default"])
269+
@pytest.mark.use_analytics_db
275270
def test_get_total_events_count(organisation, environment, settings): # type: ignore[no-untyped-def]
276271
settings.USE_POSTGRES_FOR_ANALYTICS = True
277272
environment_id = environment.id
@@ -322,8 +317,7 @@ def test_get_total_events_count(organisation, environment, settings): # type: i
322317
assert total_events_count == 20 * len(Resource) * 30
323318

324319

325-
@pytest.mark.skip_if_no_analytics_db
326-
@pytest.mark.django_db(databases=["analytics", "default"])
320+
@pytest.mark.use_analytics_db
327321
def test_get_feature_evaluation_data_from_local_db(
328322
feature: Feature,
329323
environment: Environment,
@@ -391,8 +385,7 @@ def test_get_feature_evaluation_data_from_local_db(
391385
assert data.day == today - timedelta(days=29 - i)
392386

393387

394-
@pytest.mark.skip_if_no_analytics_db
395-
@pytest.mark.django_db(databases=["analytics", "default"])
388+
@pytest.mark.use_analytics_db
396389
def test_get_feature_evaluation_data_from_local_db__labels_filter__returns_expected(
397390
feature: Feature,
398391
environment: Environment,

api/tests/unit/app_analytics/test_migrate_to_pg.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
from app_analytics.models import FeatureEvaluationBucket
77

88

9-
@pytest.mark.skip_if_no_analytics_db
10-
@pytest.mark.django_db(databases=["analytics", "default"])
9+
@pytest.mark.use_analytics_db
1110
def test_migrate_feature_evaluations(mocker: MockerFixture) -> None:
1211
# Given
1312
feature_name = "test_feature_one"

api/tests/unit/app_analytics/test_models.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@
88
Resource,
99
)
1010

11-
pytestmark = pytest.mark.skip_if_no_analytics_db
11+
pytestmark = pytest.mark.use_analytics_db
1212

1313

14-
@pytest.mark.django_db(databases=["analytics"])
1514
def test_creating_overlapping_api_usage_bucket_raises_error(db): # type: ignore[no-untyped-def]
1615
# Given
1716
created_at = timezone.now()
@@ -40,7 +39,6 @@ def test_creating_overlapping_api_usage_bucket_raises_error(db): # type: ignore
4039
)
4140

4241

43-
@pytest.mark.django_db(databases=["analytics"])
4442
def test_creating_overlapping_feature_evaluation_bucket_raises_error(db): # type: ignore[no-untyped-def]
4543
# Given
4644
created_at = timezone.now()

api/tests/unit/app_analytics/test_tasks.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
from app_analytics.types import TrackFeatureEvaluationsByEnvironmentData
2424
from environments.models import Environment
2525

26-
pytestmark = pytest.mark.skip_if_no_analytics_db
26+
pytestmark = pytest.mark.use_analytics_db
2727

2828

2929
def _create_api_usage_event(environment_id: int, when: datetime) -> APIUsageRaw:
@@ -40,7 +40,7 @@ def _create_api_usage_event(environment_id: int, when: datetime) -> APIUsageRaw:
4040

4141

4242
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
43-
@pytest.mark.django_db(databases=["analytics"])
43+
@pytest.mark.use_analytics_db
4444
def test_populate_api_usage_bucket_multiple_runs(
4545
freezer: FrozenDateTimeFactory,
4646
) -> None:
@@ -113,7 +113,7 @@ def test_populate_api_usage_bucket_multiple_runs(
113113
[(15, 60), (10, 60), (10, 30), (30, 30), (60, 60), (10, 10), (60, 60 * 4)],
114114
)
115115
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
116-
@pytest.mark.django_db(databases=["analytics"])
116+
@pytest.mark.use_analytics_db
117117
def test_populate_api_usage_bucket(
118118
freezer: FrozenDateTimeFactory,
119119
bucket_size: int,
@@ -153,7 +153,7 @@ def test_populate_api_usage_bucket(
153153
assert bucket.total_count == bucket_size
154154

155155

156-
@pytest.mark.django_db(databases=["analytics", "default"])
156+
@pytest.mark.use_analytics_db
157157
def test_track_request__postgres__inserts_expected(
158158
settings: SettingsWrapper,
159159
environment: Environment,
@@ -205,7 +205,7 @@ def test_track_request__influx__calls_expected(
205205
)
206206

207207

208-
@pytest.mark.django_db(databases=["analytics"])
208+
@pytest.mark.use_analytics_db
209209
def test_track_feature_evaluation(settings: SettingsWrapper) -> None:
210210
# Given
211211
settings.USE_POSTGRES_FOR_ANALYTICS = True
@@ -244,7 +244,7 @@ def test_track_feature_evaluation(settings: SettingsWrapper) -> None:
244244
)
245245

246246

247-
@pytest.mark.django_db(databases=["analytics"])
247+
@pytest.mark.use_analytics_db
248248
def test_track_feature_evaluation__influx__calls_expected(
249249
settings: SettingsWrapper,
250250
mocker: MockerFixture,
@@ -287,7 +287,7 @@ def test_track_feature_evaluation__influx__calls_expected(
287287

288288

289289
@pytest.mark.freeze_time("2023-01-19T09:09:47.325132+00:00")
290-
@pytest.mark.django_db(databases=["analytics"])
290+
@pytest.mark.use_analytics_db
291291
def test_populate_feature_evaluation_bucket_15m(freezer: FrozenDateTimeFactory) -> None:
292292
# Given
293293
environment_id = 1
@@ -379,7 +379,7 @@ def test_populate_feature_evaluation_bucket_15m(freezer: FrozenDateTimeFactory)
379379

380380

381381
@pytest.mark.freeze_time("2023-01-19T09:00:00+00:00")
382-
@pytest.mark.django_db(databases=["analytics"])
382+
@pytest.mark.use_analytics_db
383383
def test_populate_feature_evaluation_bucket__upserts_buckets(
384384
freezer: FrozenDateTimeFactory,
385385
) -> None:
@@ -415,7 +415,7 @@ def test_populate_feature_evaluation_bucket__upserts_buckets(
415415

416416

417417
@pytest.mark.freeze_time("2023-01-19T09:00:00+00:00")
418-
@pytest.mark.django_db(databases=["analytics"])
418+
@pytest.mark.use_analytics_db
419419
def test_populate_feature_evaluation_bucket__source_bucket_size__returns_expected(
420420
freezer: FrozenDateTimeFactory,
421421
) -> None:
@@ -465,7 +465,7 @@ def test_populate_feature_evaluation_bucket__source_bucket_size__returns_expecte
465465

466466

467467
@pytest.mark.freeze_time("2023-01-19T09:00:00+00:00")
468-
@pytest.mark.django_db(databases=["analytics"])
468+
@pytest.mark.use_analytics_db
469469
def test_populate_api_usage_bucket__upserts_buckets(
470470
freezer: FrozenDateTimeFactory,
471471
) -> None:
@@ -501,7 +501,7 @@ def test_populate_api_usage_bucket__upserts_buckets(
501501

502502

503503
@pytest.mark.freeze_time("2023-01-19T09:00:00+00:00")
504-
@pytest.mark.django_db(databases=["analytics"])
504+
@pytest.mark.use_analytics_db
505505
def test_populate_api_usage_bucket_using_a_bucket(
506506
freezer: FrozenDateTimeFactory,
507507
) -> None:
@@ -548,7 +548,7 @@ def _create_feature_evaluation_event(
548548
return event
549549

550550

551-
@pytest.mark.django_db(databases=["analytics"])
551+
@pytest.mark.use_analytics_db
552552
def test_clean_up_old_analytics_data_does_nothing_if_no_data() -> None:
553553
# When
554554
clean_up_old_analytics_data()
@@ -557,7 +557,7 @@ def test_clean_up_old_analytics_data_does_nothing_if_no_data() -> None:
557557
# no exception was raised
558558

559559

560-
@pytest.mark.django_db(databases=["analytics"])
560+
@pytest.mark.use_analytics_db
561561
def test_clean_up_old_analytics_data_removes_old_data(
562562
settings: SettingsWrapper,
563563
) -> None:

api/tests/unit/app_analytics/test_unit_app_analytics_views.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -408,8 +408,7 @@ def test_get_total_usage_count_for_non_admin_user_returns_403( # type: ignore[n
408408
assert response.status_code == status.HTTP_403_FORBIDDEN
409409

410410

411-
@pytest.mark.skip_if_no_analytics_db
412-
@pytest.mark.django_db(databases=["default", "analytics"])
411+
@pytest.mark.use_analytics_db
413412
def test_set_sdk_analytics_flags_with_identifier(
414413
api_client: APIClient,
415414
environment: Environment,
@@ -450,8 +449,7 @@ def test_set_sdk_analytics_flags_with_identifier(
450449
assert feature_evaluation_raw.evaluation_count is feature_request_count # type: ignore[union-attr]
451450

452451

453-
@pytest.mark.skip_if_no_analytics_db
454-
@pytest.mark.django_db(databases=["default", "analytics"])
452+
@pytest.mark.use_analytics_db
455453
def test_set_sdk_analytics_flags_without_identifier(
456454
api_client: APIClient,
457455
environment: Environment,

0 commit comments

Comments
 (0)