|
6 | 6 | from boto3.dynamodb.types import Binary |
7 | 7 | from botocore.exceptions import ClientError |
8 | 8 | from django.core.exceptions import ObjectDoesNotExist |
9 | | -from flag_engine.segments.constants import IN |
| 9 | +from flag_engine.segments.constants import IN, IS_SET |
10 | 10 | from mypy_boto3_dynamodb.service_resource import Table |
11 | 11 | from pytest_django.fixtures import SettingsWrapper |
12 | 12 | from pytest_mock import MockerFixture |
|
24 | 24 | SystemTraitWriteRaceError, |
25 | 25 | ) |
26 | 26 | from environments.identities.models import Identity |
| 27 | +from environments.identities.traits.constants import ( |
| 28 | + TRAIT_STRING_VALUE_MAX_LENGTH, |
| 29 | +) |
27 | 30 | from environments.identities.traits.models import Trait |
28 | 31 | from features.models import Feature, FeatureSegment, FeatureState |
29 | 32 | from features.multivariate.models import ( |
@@ -400,6 +403,55 @@ def test_get_segment_ids__segment_with_feature_overrides__returns_correct_ids( |
400 | 403 | assert segment_ids == [identity_matching_segment.id] |
401 | 404 |
|
402 | 405 |
|
| 406 | +def test_get_segment_ids__system_trait_backed_segment__returns_correct_ids( |
| 407 | + project: "Project", |
| 408 | + environment: "Environment", |
| 409 | + identity: "Identity", |
| 410 | + mocker: "MockerFixture", |
| 411 | +) -> None: |
| 412 | + # Given - two IS_SET segments: one keyed to a system trait the identity |
| 413 | + # carries, one keyed to a system trait it does not |
| 414 | + member_segment = Segment.objects.create(name="Cohort members", project=project) |
| 415 | + rule = SegmentRule.objects.create(segment=member_segment, type=SegmentRule.ALL_RULE) |
| 416 | + Condition.objects.create(rule=rule, operator=IS_SET, property="flagsmith_cohort_a") |
| 417 | + other_segment = Segment.objects.create(name="Other cohort", project=project) |
| 418 | + other_rule = SegmentRule.objects.create( |
| 419 | + segment=other_segment, type=SegmentRule.ALL_RULE |
| 420 | + ) |
| 421 | + Condition.objects.create( |
| 422 | + rule=other_rule, operator=IS_SET, property="flagsmith_cohort_b" |
| 423 | + ) |
| 424 | + |
| 425 | + identity_document = map_identity_to_identity_document(identity) |
| 426 | + identity_document["system_traits"] = {"flagsmith_cohort_a": True} |
| 427 | + identity_uuid = identity_document["identity_uuid"] |
| 428 | + |
| 429 | + dynamo_identity_wrapper = DynamoIdentityWrapper() |
| 430 | + mocker.patch.object( |
| 431 | + dynamo_identity_wrapper, "get_item_from_uuid", return_value=identity_document |
| 432 | + ) |
| 433 | + |
| 434 | + # When |
| 435 | + segment_ids = dynamo_identity_wrapper.get_segment_ids(identity_uuid) # type: ignore[arg-type] |
| 436 | + |
| 437 | + # Then |
| 438 | + assert segment_ids == [member_segment.id] |
| 439 | + |
| 440 | + |
| 441 | +def test_set_system_trait__oversized_string_value__raises() -> None: |
| 442 | + # Given |
| 443 | + wrapper = DynamoIdentityWrapper() |
| 444 | + |
| 445 | + # When / Then |
| 446 | + with pytest.raises(ValueError): |
| 447 | + wrapper.set_system_trait( |
| 448 | + environment_api_key="key", |
| 449 | + identifier="user", |
| 450 | + trait_key="flagsmith_cohort_a", |
| 451 | + trait_value="x" * (TRAIT_STRING_VALUE_MAX_LENGTH + 1), |
| 452 | + ) |
| 453 | + |
| 454 | + |
403 | 455 | def test_get_segment_ids__in_operator_with_integer_traits__returns_matching_segment( |
404 | 456 | project: "Project", environment: "Environment", mocker: "MockerFixture" |
405 | 457 | ) -> None: |
|
0 commit comments