From f544a66929fd74df3d30f92830442c96cffaf22d Mon Sep 17 00:00:00 2001 From: Al Moataz Rizk Date: Thu, 29 Aug 2024 10:00:04 +0200 Subject: [PATCH] Feat: support environment and appName variant overrides (#179) --- api/variant.go | 4 +++ api/variant_test.go | 74 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 75 insertions(+), 3 deletions(-) diff --git a/api/variant.go b/api/variant.go index 8211dd2..c92af76 100644 --- a/api/variant.go +++ b/api/variant.go @@ -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 { diff --git a/api/variant_test.go b/api/variant_test.go index 770404b..e834f70 100644 --- a/api/variant_test.go +++ b/api/variant_test.go @@ -23,7 +23,7 @@ func (suite *VariantTestSuite) SetupTest() { Value: "Test 1", }, }, - Weight: 33, + Weight: 25, Overrides: []Override{ Override{ ContextName: "userId", @@ -47,7 +47,7 @@ func (suite *VariantTestSuite) SetupTest() { Value: "Test 2", }, }, - Weight: 33, + Weight: 25, Overrides: []Override{ Override{ ContextName: "remoteAddress", @@ -65,7 +65,7 @@ func (suite *VariantTestSuite) SetupTest() { Value: "Test 3", }, }, - Weight: 34, + Weight: 25, Overrides: []Override{ Override{ ContextName: "env", @@ -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{ @@ -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") +}