Skip to content

Commit

Permalink
Feat: support environment and appName variant overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
mrizk committed Aug 28, 2024
1 parent 10a3338 commit 4a32a30
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 3 deletions.
4 changes: 4 additions & 0 deletions api/variant.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ func (o Override) getIdentifier(ctx *context.Context) string {
value = ctx.SessionId
case "remoteAddress":
value = ctx.RemoteAddress
case "environment":
value = ctx.Environment
case "appName":
value = ctx.AppName
default:
if len(ctx.Properties) > 0 {
for k, v := range ctx.Properties {
Expand Down
74 changes: 71 additions & 3 deletions api/variant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (suite *VariantTestSuite) SetupTest() {
Value: "Test 1",
},
},
Weight: 33,
Weight: 25,
Overrides: []Override{
Override{
ContextName: "userId",
Expand All @@ -47,7 +47,7 @@ func (suite *VariantTestSuite) SetupTest() {
Value: "Test 2",
},
},
Weight: 33,
Weight: 25,
Overrides: []Override{
Override{
ContextName: "remoteAddress",
Expand All @@ -65,7 +65,7 @@ func (suite *VariantTestSuite) SetupTest() {
Value: "Test 3",
},
},
Weight: 34,
Weight: 25,
Overrides: []Override{
Override{
ContextName: "env",
Expand All @@ -75,6 +75,30 @@ func (suite *VariantTestSuite) SetupTest() {
},
},
},
VariantInternal{
Variant: Variant{
Name: "VarG",
Payload: Payload{
Type: "string",
Value: "Test 4",
},
},
Weight: 25,
Overrides: []Override{
Override{
ContextName: "environment",
Values: []string{
"development",
},
},
Override{
ContextName: "appName",
Values: []string{
"test",
},
},
},
},
}

suite.VariantWithoutOverride = []VariantInternal{
Expand Down Expand Up @@ -263,3 +287,47 @@ func TestVariantSuite(t *testing.T) {
ts := VariantTestSuite{}
suite.Run(t, &ts)
}

func (suite *VariantTestSuite) TestGetVariant_OverrideOnAppName() {
mockFeature := Feature{
Name: "test.variants",
Enabled: true,
Variants: suite.VariantWithOverride,
}
mockContext := &context.Context{
AppName: "test",
}
expectedPayload := Payload{
Type: "string",
Value: "Test 4",
}
variantSetup := VariantCollection{
GroupId: mockFeature.Name,
Variants: mockFeature.Variants,
}.GetVariant(mockContext)
suite.Equal("VarG", variantSetup.Name, "Should return VarG")
suite.Equal(true, variantSetup.Enabled, "Should be equal")
suite.Equal(expectedPayload, variantSetup.Payload, "Should be equal")
}

func (suite *VariantTestSuite) TestGetVariant_OverrideOnEnvironment() {
mockFeature := Feature{
Name: "test.variants",
Enabled: true,
Variants: suite.VariantWithOverride,
}
mockContext := &context.Context{
Environment: "development",
}
expectedPayload := Payload{
Type: "string",
Value: "Test 4",
}
variantSetup := VariantCollection{
GroupId: mockFeature.Name,
Variants: mockFeature.Variants,
}.GetVariant(mockContext)
suite.Equal("VarG", variantSetup.Name, "Should return VarG")
suite.Equal(true, variantSetup.Enabled, "Should be equal")
suite.Equal(expectedPayload, variantSetup.Payload, "Should be equal")
}

0 comments on commit 4a32a30

Please sign in to comment.