|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2020, Optimizely, Inc. and contributors * |
| 3 | + * * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); * |
| 5 | + * you may not use this file except in compliance with the License. * |
| 6 | + * You may obtain a copy of the License at * |
| 7 | + * * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 * |
| 9 | + * * |
| 10 | + * Unless required by applicable law or agreed to in writing, software * |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, * |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * |
| 13 | + * See the License for the specific language governing permissions and * |
| 14 | + * limitations under the License. * |
| 15 | + ***************************************************************************/ |
| 16 | + |
| 17 | +package client |
| 18 | + |
| 19 | +import ( |
| 20 | + "errors" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/optimizely/go-sdk/pkg/optimizelyjson" |
| 24 | + |
| 25 | + "github.com/stretchr/testify/suite" |
| 26 | +) |
| 27 | + |
| 28 | +type OptimizelyDecisionTestSuite struct { |
| 29 | + suite.Suite |
| 30 | + *OptimizelyClient |
| 31 | +} |
| 32 | + |
| 33 | +func (s *OptimizelyDecisionTestSuite) SetupTest() { |
| 34 | + factory := OptimizelyFactory{SDKKey: "1212"} |
| 35 | + s.OptimizelyClient, _ = factory.Client() |
| 36 | +} |
| 37 | + |
| 38 | +func (s *OptimizelyDecisionTestSuite) TestOptimizelyDecision() { |
| 39 | + variationKey := "var1" |
| 40 | + enabled := true |
| 41 | + variables, _ := optimizelyjson.NewOptimizelyJSONfromString(`{"k1":"v1"}`) |
| 42 | + var ruleKey string |
| 43 | + flagKey := "flag1" |
| 44 | + reasons := []string{} |
| 45 | + userID := "testUser1" |
| 46 | + attributes := map[string]interface{}{"key": 1212} |
| 47 | + |
| 48 | + optimizelyUserContext := s.OptimizelyClient.CreateUserContext(userID, attributes) |
| 49 | + decision := NewOptimizelyDecision(variationKey, ruleKey, flagKey, enabled, variables, optimizelyUserContext, reasons) |
| 50 | + |
| 51 | + s.Equal(variationKey, decision.GetVariationKey()) |
| 52 | + s.Equal(enabled, decision.GetEnabled()) |
| 53 | + s.Equal(variables, decision.GetVariables()) |
| 54 | + s.Equal(ruleKey, decision.GetRuleKey()) |
| 55 | + s.Equal(flagKey, decision.GetFlagKey()) |
| 56 | + s.Equal(reasons, decision.GetReasons()) |
| 57 | + s.Equal(optimizelyUserContext, decision.GetUserContext()) |
| 58 | +} |
| 59 | + |
| 60 | +func (s *OptimizelyDecisionTestSuite) TestNewErrorDecision() { |
| 61 | + flagKey := "flag1" |
| 62 | + errorString := "SDK has an error" |
| 63 | + userID := "testUser1" |
| 64 | + attributes := map[string]interface{}{"key": 1212} |
| 65 | + optimizelyUserContext := s.OptimizelyClient.CreateUserContext(userID, attributes) |
| 66 | + decision := NewErrorDecision(flagKey, optimizelyUserContext, errors.New(errorString)) |
| 67 | + |
| 68 | + s.Equal("", decision.GetVariationKey()) |
| 69 | + s.Equal(false, decision.GetEnabled()) |
| 70 | + s.Equal(&optimizelyjson.OptimizelyJSON{}, decision.GetVariables()) |
| 71 | + s.Equal("", decision.GetRuleKey()) |
| 72 | + s.Equal(flagKey, decision.GetFlagKey()) |
| 73 | + s.Equal(1, len(decision.GetReasons())) |
| 74 | + s.Equal(optimizelyUserContext, decision.GetUserContext()) |
| 75 | + s.Equal(errorString, decision.GetReasons()[0]) |
| 76 | +} |
| 77 | + |
| 78 | +func TestOptimizelyDecisionTestSuite(t *testing.T) { |
| 79 | + suite.Run(t, new(OptimizelyDecisionTestSuite)) |
| 80 | +} |
0 commit comments