Skip to content

Commit

Permalink
fix: Raise if environment_feature_version is None (#5028)
Browse files Browse the repository at this point in the history
Co-authored-by: Kim Gustyr <kim.gustyr@flagsmith.com>
  • Loading branch information
zachaysan and khvn26 authored Jan 22, 2025
1 parent ebe8448 commit edd7141
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
4 changes: 2 additions & 2 deletions api/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -471,14 +471,14 @@ def _with_project_permissions(


@pytest.fixture()
def environment_v2_versioning(environment):
def environment_v2_versioning(environment: Environment) -> Environment:
enable_v2_versioning(environment.id)
environment.refresh_from_db()
return environment


@pytest.fixture()
def identity(environment):
def identity(environment: Environment) -> Identity:
return Identity.objects.create(identifier="test_identity", environment=environment)


Expand Down
8 changes: 8 additions & 0 deletions api/features/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,14 @@ def __gt__(self, other: "FeatureState") -> bool: # noqa: C901

if self.type == other.type:
if self.environment.use_v2_feature_versioning:
if (
self.environment_feature_version is None
or other.environment_feature_version is None
):
raise ValueError(
"Cannot compare feature states as they are missing environment_feature_version."
)

return (
self.environment_feature_version > other.environment_feature_version
)
Expand Down
30 changes: 30 additions & 0 deletions api/tests/unit/features/test_unit_features_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,36 @@ def test_feature_state_gt_operator_order(
assert segment_2_state > default_env_state


def test_feature_state_gt_operator_order_when_environment_feature_version_is_none(
identity: Identity,
feature: Feature,
feature_state: FeatureState,
environment: Environment,
environment_v2_versioning: Environment,
mocker: MockerFixture,
) -> None:
# Given
feature_state2 = FeatureState.objects.create(
identity=identity, feature=feature, environment=environment
)
mocker.patch.object(
FeatureState,
"type",
new_callable=mocker.PropertyMock,
return_value="ENVIRONMENT",
)

# When
with pytest.raises(ValueError) as exception:
assert feature_state > feature_state2

# Then
assert (
exception.value.args[0]
== "Cannot compare feature states as they are missing environment_feature_version."
)


def test_feature_state_gt_operator_throws_value_error_if_different_environments(
project: Project,
environment: Environment,
Expand Down
4 changes: 2 additions & 2 deletions api/tests/unit/users/test_unit_users_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ def test_list_user_groups(
group_2 = response_json["results"][1]
group_2_users = group_2["users"]
assert len(group_2_users) == 2
assert tuple((user["id"], user["group_admin"]) for user in group_2_users) == (
assert set((user["id"], user["group_admin"]) for user in group_2_users) == {
(user1.pk, False),
(user2.pk, True),
)
}

0 comments on commit edd7141

Please sign in to comment.