|
| 1 | +import json |
| 2 | +import typing |
| 3 | +from pathlib import Path |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from flag_engine.engine import get_identity_feature_states |
| 8 | +from flag_engine.environments.builders import build_environment_model |
| 9 | +from flag_engine.environments.models import EnvironmentModel |
| 10 | +from flag_engine.identities.builders import build_identity_model |
| 11 | +from flag_engine.identities.models import IdentityModel |
| 12 | + |
| 13 | +MODULE_PATH = Path(__file__).parent.resolve() |
| 14 | + |
| 15 | + |
| 16 | +def _extract_test_cases( |
| 17 | + file_path: Path, |
| 18 | +) -> typing.Iterable[typing.Tuple[EnvironmentModel, IdentityModel, dict]]: |
| 19 | + """ |
| 20 | + Extract the test cases from the json data file which should be in the following |
| 21 | + format. |
| 22 | +
|
| 23 | + { |
| 24 | + "environment": {...}, // the environment document as found in DynamoDB |
| 25 | + "identities_and_responses": [ |
| 26 | + { |
| 27 | + "identity": {...}, // the identity as found in DynamoDB, |
| 28 | + "response": {...}, // the response that was obtained from the current API |
| 29 | + } |
| 30 | + ] |
| 31 | + } |
| 32 | +
|
| 33 | + :param file_path: the path to the json data file |
| 34 | + :return: a list of tuples containing the environment, identity and api response |
| 35 | + """ |
| 36 | + with open(file_path, "r") as f: |
| 37 | + test_data = json.loads(f.read()) |
| 38 | + environment_model = build_environment_model(test_data["environment"]) |
| 39 | + return [ |
| 40 | + ( |
| 41 | + environment_model, |
| 42 | + build_identity_model(test_case["identity"]), |
| 43 | + test_case["response"], |
| 44 | + ) |
| 45 | + for test_case in test_data["identities_and_responses"] |
| 46 | + ] |
| 47 | + |
| 48 | + |
| 49 | +@pytest.mark.parametrize( |
| 50 | + "environment_model, identity_model, api_response", |
| 51 | + _extract_test_cases( |
| 52 | + MODULE_PATH / "engine-test-data/data/environment_n9fbf9h3v4fFgH3U3ngWhb.json" |
| 53 | + ), |
| 54 | +) |
| 55 | +def test_engine(environment_model, identity_model, api_response): |
| 56 | + # When |
| 57 | + # we get the feature states from the engine |
| 58 | + engine_response = get_identity_feature_states(environment_model, identity_model) |
| 59 | + |
| 60 | + # and we sort the flags and feature states so we can iterate over them and compare |
| 61 | + sorted_engine_flags = sorted(engine_response, key=lambda fs: fs.feature.name) |
| 62 | + sorted_api_flags = sorted(api_response["flags"], key=lambda f: f["feature"]["name"]) |
| 63 | + |
| 64 | + # Then |
| 65 | + # there are an equal number of flags and feature states |
| 66 | + assert len(sorted_engine_flags) == len(sorted_api_flags) |
| 67 | + |
| 68 | + # and the values and enabled status of each of the feature states returned by the |
| 69 | + # engine are identical to those returned by the Django API (i.e. the test data). |
| 70 | + for i, feature_state in enumerate(sorted_engine_flags): |
| 71 | + assert ( |
| 72 | + feature_state.get_value(identity_model.django_id) |
| 73 | + == sorted_api_flags[i]["feature_state_value"] |
| 74 | + ) |
| 75 | + assert feature_state.enabled == sorted_api_flags[i]["enabled"] |
0 commit comments