Skip to content

Commit

Permalink
Add some additional checks on number of queries
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed May 10, 2024
1 parent ac05461 commit 263ede6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from django.utils import timezone
from flag_engine.features.models import FeatureModel, FeatureStateModel
from freezegun import freeze_time
from pytest_django import DjangoAssertNumQueries
from pytest_mock import MockerFixture

from edge_api.identities.models import EdgeIdentity
Expand Down Expand Up @@ -484,6 +485,7 @@ def test_get_all_feature_states_post_v2_versioning_migration(
segment_featurestate: FeatureState,
edge_identity_model: EdgeIdentity,
mocker: MockerFixture,
django_assert_num_queries: DjangoAssertNumQueries,
) -> None:
"""
Specific test to reproduce an issue seen after migrating our staging environment to
Expand All @@ -507,9 +509,10 @@ def test_get_all_feature_states_post_v2_versioning_migration(
edge_identity_dynamo_wrapper_mock.get_segment_ids.return_value = [segment.id]

# When
feature_states, identity_override_feature_names = (
edge_identity_model.get_all_feature_states()
)
with django_assert_num_queries(4):
feature_states, identity_override_feature_names = (
edge_identity_model.get_all_feature_states()
)

# Then
assert len(feature_states) == 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from django.db.models import Q
from django.utils import timezone
from pytest_django import DjangoAssertNumQueries

from environments.identities.models import Identity
from environments.models import Environment
Expand All @@ -18,7 +19,9 @@


def test_get_environment_flags_queryset_returns_only_latest_versions(
feature, environment
feature: Feature,
environment: Environment,
django_assert_num_queries: DjangoAssertNumQueries,
):
# Given
feature_state_v1 = FeatureState.objects.get(
Expand All @@ -31,7 +34,11 @@ def test_get_environment_flags_queryset_returns_only_latest_versions(
feature_state_v1.clone(env=environment, as_draft=True) # draft feature state

# When
feature_states = get_environment_flags_queryset(environment=environment)
with django_assert_num_queries(2):
feature_states = get_environment_flags_queryset(environment=environment)

# trigger the queryset to execute and ensure the number of queries is correct
list(feature_states)

# Then
assert feature_states.count() == 1
Expand Down Expand Up @@ -115,6 +122,7 @@ def test_get_environment_flags_v2_versioning_returns_latest_live_versions_of_fea
environment_v2_versioning: Environment,
feature: Feature,
admin_user: FFAdminUser,
django_assert_num_queries: DjangoAssertNumQueries,
) -> None:
# Given
# a second feature with its corresponding environment feature version
Expand All @@ -139,10 +147,11 @@ def test_get_environment_flags_v2_versioning_returns_latest_live_versions_of_fea
environment_feature_1_version_2.publish(admin_user)

# When
environment_feature_states = get_environment_flags_list(
environment=environment_v2_versioning,
additional_filters=Q(feature_segment=None, identity=None),
)
with django_assert_num_queries(2):
environment_feature_states = get_environment_flags_list(
environment=environment_v2_versioning,
additional_filters=Q(feature_segment=None, identity=None),
)

# Then
assert set(environment_feature_states) == {
Expand Down

0 comments on commit 263ede6

Please sign in to comment.