Skip to content

Commit

Permalink
fix!: consistent split evaluations (#88)
Browse files Browse the repository at this point in the history
* fix!: consistent split evaluations

* fix: reset mocked `GetHashedPercentageForObjectIds` correctly

* fix: better identityHashKey inferring
  • Loading branch information
khvn26 authored Jun 20, 2023
1 parent faf20eb commit 8a0b36b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
6 changes: 5 additions & 1 deletion flagengine/segments/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ func EvaluateIdentityInSegment(
traits = overrideTraits
}

identityHashKey := identity.CompositeKey()
if identity.DjangoID != 0 {
identityHashKey = strconv.Itoa(identity.DjangoID)
}
for _, rule := range segment.Rules {
if !traitsMatchSegmentRule(traits, rule, segment.ID, identity.CompositeKey()) {
if !traitsMatchSegmentRule(traits, rule, segment.ID, identityHashKey) {
return false
}
}
Expand Down
37 changes: 36 additions & 1 deletion flagengine/segments/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,42 @@ func TestIdentityInSegmentPercentageSplit(t *testing.T) {
assert.Equal(t, c.expectedResult, result)
})
}
utils.MockSetHashedPercentageForObjectIds(utils.GetHashedPercentageForObjectIds)
utils.ResetMocks()
}

func TestIdentityInSegmentPercentageSplitUsesDjangoID(t *testing.T) {
cases := []struct {
identity *identities.IdentityModel
expectedResult bool
}{
{&identities.IdentityModel{
DjangoID: 1,
Identifier: "Test",
EnvironmentAPIKey: "key",
}, false},
{&identities.IdentityModel{
Identifier: "Test",
EnvironmentAPIKey: "key",
}, true},
}

for i, c := range cases {
t.Run(strconv.Itoa(i), func(t *testing.T) {
cond := &segments.SegmentConditionModel{
Operator: segments.PercentageSplit,
Value: "50",
}
rule := &segments.SegmentRuleModel{
Type: segments.All,
Conditions: []*segments.SegmentConditionModel{cond},
}
segment := &segments.SegmentModel{ID: 1, Name: "% split", Rules: []*segments.SegmentRuleModel{rule}}

result := segments.EvaluateIdentityInSegment(c.identity, segment)

assert.Equal(t, result, c.expectedResult)
})
}
}

func TestIdentityInSegmentIsSetAndIsNotSet(t *testing.T) {
Expand Down
4 changes: 4 additions & 0 deletions flagengine/utils/hashing.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,7 @@ var hashedPercentageForObjectIdsFunc = getHashedPercentageForObjectIds
func MockSetHashedPercentageForObjectIds(fn func([]string, int) float64) {
hashedPercentageForObjectIdsFunc = fn
}

func ResetMocks() {
hashedPercentageForObjectIdsFunc = getHashedPercentageForObjectIds
}

0 comments on commit 8a0b36b

Please sign in to comment.