Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions posthog/test/fixtures/feature_flag_rules_v2/SOURCE.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"description": "Copied unchanged from the released feature flag rules v2 contract in PostHog/posthog-sdk-test-harness. Do not edit these files; copy a newer release instead.",
"repository": "https://github.com/PostHog/posthog-sdk-test-harness",
"release": "1.30.0",
"revision": "6414114501396d96aadf2412fdd8054a03076782",
"release": "1.38.0",
"revision": "9637117c6977265fdc3ea62bdd7c80cae7c044ca",
"contract_directory": "contracts/feature_flag_rules_v2",
"contract_version": "10.0.0",
"contract_version": "11.0.0",
"files": {
"readers.json": {
"source_path": "fixtures/wire/readers.json",
Expand All @@ -24,7 +24,7 @@
},
"feature_flag_called_context.schema.json": {
"source_path": "schemas/feature_flag_called_context.schema.json",
"sha256": "8a475f5ab56ba4dba76501c4a4e23f26b62c2c2f8037f23d21d0ea7650bd2cf8"
"sha256": "490b2e3226c52ab11b0415804ac00b46496037428321f0a0d47b6ee6088e3b0e"
},
"experiment_exposure_properties.schema.json": {
"source_path": "schemas/experiment_exposure_properties.schema.json",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
"type": "object",
"additionalProperties": false,
"required": [
"$feature_flag",
"$feature_flag_response"
"$feature_flag"
],
"properties": {
"$feature_flag": {
Expand All @@ -21,9 +20,15 @@
{
"type": "string",
"minLength": 1
},
{
"type": "null"
}
]
},
"$feature_flag_error": {
"type": "string"
},
"$feature_flag_config_version": {
"enum": [
1,
Expand Down Expand Up @@ -56,7 +61,7 @@
"const": true
}
},
"description": "Closed projection of the flag-access context properties. Other existing capture fields are outside this projection. Complete v2 producer context follows the terminal outcome. A tolerant reader drops the whole attribution of a record that breaks its reason's rows (a rule ID without its rule type or the reverse, a rule type the reason does not allow, or a malformed split): for every reason that names a rule, a call may keep the config version and reason code and omit all rule, variant and experiment attribution, while a partly populated or mismatched tuple is rejected. A config version 2 call whose reason code is missing or not registered keeps the raw code, if any, and carries no attribution; a registered code only matches its own reason's branches. A config version 2 call never carries $feature_flag_reason, the reason description that only version 1 calls carry.",
"description": "Closed projection of the flag-access context properties. Other existing capture fields are outside this projection. Complete v2 producer context follows the terminal outcome. A tolerant reader drops the whole attribution of a record that breaks its reason's rows (a rule ID without its rule type or the reverse, a rule type the reason does not allow, or a malformed split): for every reason that names a rule, a call may keep the config version and reason code and omit all rule, variant and experiment attribution, while a partly populated or mismatched tuple is rejected. A config version 2 call whose reason code is missing or not registered keeps the raw code, if any, and carries no attribution; a registered code only matches its own reason's branches. A config version 2 call never carries $feature_flag_reason, the reason description that only version 1 calls carry. $feature_flag_response may be absent or null when the read has no value. A call without config version 2 may do so, because a missing flag carries no config version. A config version 2 call may do so only when $feature_flag_error lists evaluation_failed: a failed record with no successful fallback.",
"allOf": [
{
"if": {
Expand Down Expand Up @@ -84,6 +89,30 @@
}
]
},
"anyOf": [
{
"required": [
"$feature_flag_response"
],
"properties": {
"$feature_flag_response": {
"not": {
"type": "null"
}
}
}
},
{
"required": [
"$feature_flag_error"
],
"properties": {
"$feature_flag_error": {
"pattern": "(^|,)evaluation_failed(,|$)"
}
}
}
],
"oneOf": [
{
"properties": {
Expand Down
43 changes: 34 additions & 9 deletions posthog/test/test_experiment_exposure.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,7 @@ def _check_reader_events(case: Dict[str, Any], events: List[_Event]) -> None:
assert called.properties.get("$feature_flag_config_version") == (
2 if config_version == 2 else None
), key
if called.properties["$feature_flag_response"] is not None:
FLAG_CALLED_SCHEMA.validate(
_projection(FLAG_CALLED_SCHEMA, called.properties)
)
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))
for exposure in exposures:
EXPOSURE_SCHEMA.validate(_projection(EXPOSURE_SCHEMA, exposure.properties))
assert not set(EXPOSURE_LEAVES_OUT) & set(exposure.properties), key
Expand Down Expand Up @@ -524,21 +521,20 @@ def test_flag_called_attribution_follows_the_record(source, expected, path):
[called] = _named(events, FLAG_CALLED, "garden-layout")
assert _attribution(called.properties) == expected
if failed:
# The vendored schema rejects the null $feature_flag_response a failed record reports.
assert called.properties["$feature_flag_response"] is None
assert set(called.properties["$feature_flag_error"].split(",")) == {
"evaluation_failed",
"errors_while_computing_flags",
}
else:
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))
exposures = _named(events, EXPOSURE, "garden-layout")
assert len(exposures) == (1 if "$feature_flag_variant_key" in expected else 0)


# A record whose reason names no rule, or that failed, reports no rule even when it
# carries one. Neither sends an exposure. The last element says whether a successful
# record is a valid server outcome, which the contract's event schema then accepts.
# The schema accepts the call of every failed record.
STRAY_RULE_CASES = [
("no_rule_match", {}, True),
("missing_group_key", {}, True),
Expand Down Expand Up @@ -579,10 +575,11 @@ def test_rule_fields_are_sent_only_for_reasons_that_name_a_rule(
"$feature_flag_reason_code": code,
}
assert _named(events, EXPOSURE) == []
if fields.get("failed") is True:
failed = fields.get("failed") is True
if failed:
assert called.properties["$feature_flag_response"] is None
assert called.properties["$feature_flag_error"] == "evaluation_failed"
elif valid_outcome:
if failed or valid_outcome:
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))


Expand All @@ -603,6 +600,33 @@ async def read(client: Any) -> None:
"$feature_flag_reason_code": "experiment_split",
}
assert _named(events, EXPOSURE) == []
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))


@pytest.mark.parametrize("response", ["null", "absent"])
@pytest.mark.parametrize(
"error",
[None, "flag_missing", "errors_while_computing_flags"],
ids=["no_error", "flag_missing", "errors_while_computing_flags"],
)
def test_a_config_version_2_call_without_a_value_must_list_evaluation_failed(
error, response
):
# The failed read is valid only because its error lists evaluation_failed.
record = _record("error", None, failed=True)
events = _read_events("get_feature_flag_result", _response(record))
[called] = _named(events, FLAG_CALLED)
properties = _projection(FLAG_CALLED_SCHEMA, called.properties)
FLAG_CALLED_SCHEMA.validate(properties)

if response == "absent":
del properties["$feature_flag_response"]
if error is None:
del properties["$feature_flag_error"]
else:
properties["$feature_flag_error"] = error
with pytest.raises(jsonschema.ValidationError):
FLAG_CALLED_SCHEMA.validate(properties)


@pytest.mark.parametrize("path", ["get_feature_flag_result", "snapshot.get_flag"])
Expand Down Expand Up @@ -752,6 +776,7 @@ def test_a_config_version_2_call_reports_its_reason_by_code_only(record, path):
[called] = _named(events, FLAG_CALLED)
assert called.properties["$feature_flag_reason_code"] == record["reason"]["code"]
assert "$feature_flag_reason" not in called.properties
FLAG_CALLED_SCHEMA.validate(_projection(FLAG_CALLED_SCHEMA, called.properties))


@pytest.mark.parametrize("path", list(SYNC_READS))
Expand Down
6 changes: 3 additions & 3 deletions posthog/test/test_flags_v4_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ def _evaluate(client: Client, response: Dict[str, Any]) -> FeatureFlagEvaluation

def test_vendored_fixtures_match_the_recorded_release_digests():
source = _load("SOURCE.json")
assert source["release"] == "1.30.0"
assert source["revision"] == "6414114501396d96aadf2412fdd8054a03076782"
assert source["contract_version"] == "10.0.0"
assert source["release"] == "1.38.0"
assert source["revision"] == "9637117c6977265fdc3ea62bdd7c80cae7c044ca"
assert source["contract_version"] == "11.0.0"
for name, entry in source["files"].items():
digest = hashlib.sha256((FIXTURES / name).read_bytes()).hexdigest()
assert digest == entry["sha256"], name
Expand Down