Repository navigation
feat: Support $.flags context values in segment conditions
#261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
khvn26
wants to merge
4
commits into
main
Choose a base branch
from
feat/flags-context-values
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c0cbc25
feat: Support `$.flags` context values in segment conditions
khvn26 8cffe72
refactor: Combine context reads with `|`
khvn26 6811818
refactor: Get context reads from rules and from segments
khvn26 cedc5de
refactor: Filter identity-free segments when evaluating environment f…
khvn26 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||||||||||
| FlagsmithFeatureDoesNotExistError, | ||||||||||||||
| ) | ||||||||||||||
| from flagsmith.local_evaluation import LocalEvaluation | ||||||||||||||
| from flagsmith.mappers import map_environment_document_to_context | ||||||||||||||
| from flagsmith.models import DefaultFlag, ExperimentMetadata, Flag, Flags | ||||||||||||||
| from flagsmith.offline_handlers import OfflineHandler | ||||||||||||||
| from flagsmith.types import SDKEvaluationContext | ||||||||||||||
|
|
@@ -101,40 +102,72 @@ def test_get_environment_flags_uses_local_environment_when_available( | |||||||||||||
| assert all_flags[0].value == "some-value" | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_get_environment_flags_omits_segments_from_evaluation_context( | ||||||||||||||
| def _condition(property_: str) -> typing.Any: | ||||||||||||||
| return {"operator": "EQUAL", "property_": property_, "value": "value"} | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| @pytest.mark.parametrize( | ||||||||||||||
| "rules, expected_segment_names", | ||||||||||||||
| [ | ||||||||||||||
| ( | ||||||||||||||
| [{"type": "ALL", "conditions": [_condition("$.environment.name")]}], | ||||||||||||||
| ["Test segment"], | ||||||||||||||
| ), | ||||||||||||||
| ( | ||||||||||||||
| [ | ||||||||||||||
| { | ||||||||||||||
| "type": "ALL", | ||||||||||||||
| "rules": [ | ||||||||||||||
| { | ||||||||||||||
| "type": "ANY", | ||||||||||||||
| "conditions": [ | ||||||||||||||
| _condition("$.environment['name']"), | ||||||||||||||
| _condition("$.flags.some_feature.enabled"), | ||||||||||||||
| ], | ||||||||||||||
| } | ||||||||||||||
| ], | ||||||||||||||
| } | ||||||||||||||
| ], | ||||||||||||||
| ["Test segment"], | ||||||||||||||
| ), | ||||||||||||||
| ([{"type": "ALL", "conditions": [_condition("trait")]}], []), | ||||||||||||||
| ( | ||||||||||||||
| [ | ||||||||||||||
| { | ||||||||||||||
| "type": "ALL", | ||||||||||||||
| "conditions": [_condition("$.environment.name")], | ||||||||||||||
| "rules": [{"type": "ALL", "conditions": [_condition("trait")]}], | ||||||||||||||
| } | ||||||||||||||
| ], | ||||||||||||||
| [], | ||||||||||||||
| ), | ||||||||||||||
| ], | ||||||||||||||
| ) | ||||||||||||||
| def test_get_environment_flags__local_evaluation__includes_identity_free_segments_only( | ||||||||||||||
| mocker: MockerFixture, | ||||||||||||||
| local_eval_flagsmith: Flagsmith, | ||||||||||||||
| evaluation_context: SDKEvaluationContext, | ||||||||||||||
| flagsmith: Flagsmith, | ||||||||||||||
| environment: EnvironmentModel, | ||||||||||||||
| rules: typing.Any, | ||||||||||||||
| expected_segment_names: typing.List[str], | ||||||||||||||
| ) -> None: | ||||||||||||||
| # Given | ||||||||||||||
| mock_get_evaluation_result = mocker.patch( | ||||||||||||||
| "flagsmith.flagsmith.engine.get_evaluation_result", | ||||||||||||||
| autospec=True, | ||||||||||||||
| # the only segment has the given rules, plus identity overrides | ||||||||||||||
| environment["project"]["segments"][0]["rules"] = rules | ||||||||||||||
| flagsmith._local_evaluation = LocalEvaluation( | ||||||||||||||
| map_environment_document_to_context(environment) | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| expected_evaluation_result = { | ||||||||||||||
| "flags": { | ||||||||||||||
| "some_feature": { | ||||||||||||||
| "name": "some_feature", | ||||||||||||||
| "enabled": True, | ||||||||||||||
| "value": "some-feature-state-value", | ||||||||||||||
| "reason": "DEFAULT", | ||||||||||||||
| "metadata": {"id": 1}, | ||||||||||||||
| } | ||||||||||||||
| }, | ||||||||||||||
| "segments": [], | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| mock_get_evaluation_result.return_value = expected_evaluation_result | ||||||||||||||
| flagsmith.enable_local_evaluation = True | ||||||||||||||
| spy = mocker.spy(engine, "get_evaluation_result") | ||||||||||||||
|
|
||||||||||||||
| # When | ||||||||||||||
| local_eval_flagsmith.get_environment_flags() | ||||||||||||||
| flagsmith.get_environment_flags() | ||||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| # Verify segments are not present in the context passed to the engine | ||||||||||||||
| context_without_segments = evaluation_context.copy() | ||||||||||||||
| context_without_segments.pop("segments", None) | ||||||||||||||
| mock_get_evaluation_result.assert_called_once_with(context=context_without_segments) | ||||||||||||||
| # only segments reading `$.environment` or `$.flags` alone are evaluated, | ||||||||||||||
| # as `GET /api/v1/flags/` does | ||||||||||||||
| context = spy.call_args.kwargs["context"] | ||||||||||||||
| segments = (context.get("segments") or {}).values() | ||||||||||||||
| assert [segment["name"] for segment in segments] == expected_segment_names | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_get_environment_flags__local_evaluation__evaluates_once_per_environment( | ||||||||||||||
|
|
@@ -316,6 +349,40 @@ def test_get_identity_flags_includes_segments_in_evaluation_context( | |||||||||||||
| assert "segments" in context | ||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_get_identity_flags__flags_condition__evaluates_full_context( | ||||||||||||||
| mocker: MockerFixture, | ||||||||||||||
| flagsmith: Flagsmith, | ||||||||||||||
| environment: EnvironmentModel, | ||||||||||||||
| ) -> None: | ||||||||||||||
| # Given | ||||||||||||||
| # a segment condition reading another flag | ||||||||||||||
| environment["project"]["segments"][0]["rules"] = [ | ||||||||||||||
| { | ||||||||||||||
| "type": "ALL", | ||||||||||||||
| "rules": [], | ||||||||||||||
| "conditions": [_condition("$.flags.some_feature.enabled")], | ||||||||||||||
| } | ||||||||||||||
| ] | ||||||||||||||
| flagsmith._local_evaluation = LocalEvaluation( | ||||||||||||||
| map_environment_document_to_context(environment) | ||||||||||||||
| ) | ||||||||||||||
| flagsmith.enable_local_evaluation = True | ||||||||||||||
| spy = mocker.spy(engine, "get_evaluation_result") | ||||||||||||||
|
|
||||||||||||||
| # When | ||||||||||||||
| flagsmith.get_identity_flags("identifier").get_flag("some_feature") | ||||||||||||||
|
|
||||||||||||||
| # Then | ||||||||||||||
| # flags are evaluated at once against the full context, segments included, | ||||||||||||||
| # rather than per flag against a trimmed one | ||||||||||||||
| spy.assert_called_once() | ||||||||||||||
| context = spy.call_args.kwargs["context"] | ||||||||||||||
| assert set(context["features"]) == {"some_feature"} | ||||||||||||||
| assert [segment["name"] for segment in (context["segments"] or {}).values()] == [ | ||||||||||||||
| "Test segment" | ||||||||||||||
| ] | ||||||||||||||
|
Comment on lines
+381
to
+383
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
|
||||||||||||||
|
|
||||||||||||||
| def test_get_identity_flags__resolves_one_flag_at_a_time( | ||||||||||||||
| local_eval_flagsmith: Flagsmith, | ||||||||||||||
| mocker: MockerFixture, | ||||||||||||||
|
|
||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we please move this into a private function named after why it the filtering is needed? This is easy to understand while reviewing this PR, but not so much for someone reading this for the first time in a couple years from now.