|
| 1 | +/**************************************************************************** |
| 2 | + * Copyright 2019, 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 decision // |
| 18 | +package decision |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/optimizely/go-sdk/pkg/entities" |
| 24 | + "github.com/stretchr/testify/mock" |
| 25 | + "github.com/stretchr/testify/suite" |
| 26 | +) |
| 27 | + |
| 28 | +type MockUserProfileService struct { |
| 29 | + UserProfileService |
| 30 | + mock.Mock |
| 31 | +} |
| 32 | + |
| 33 | +func (m *MockUserProfileService) Lookup(userID string) UserProfile { |
| 34 | + args := m.Called(userID) |
| 35 | + return args.Get(0).(UserProfile) |
| 36 | +} |
| 37 | + |
| 38 | +func (m *MockUserProfileService) Save(userProfile UserProfile) { |
| 39 | + m.Called(userProfile) |
| 40 | +} |
| 41 | + |
| 42 | +var testUserContext entities.UserContext = entities.UserContext{ |
| 43 | + ID: "test_user_1", |
| 44 | +} |
| 45 | + |
| 46 | +type PersistingExperimentServiceTestSuite struct { |
| 47 | + suite.Suite |
| 48 | + mockProjectConfig *mockProjectConfig |
| 49 | + mockExperimentService *MockExperimentDecisionService |
| 50 | + mockUserProfileService *MockUserProfileService |
| 51 | + testComputedDecision ExperimentDecision |
| 52 | + testDecisionContext ExperimentDecisionContext |
| 53 | +} |
| 54 | + |
| 55 | +func (s *PersistingExperimentServiceTestSuite) SetupTest() { |
| 56 | + s.mockProjectConfig = new(mockProjectConfig) |
| 57 | + s.mockExperimentService = new(MockExperimentDecisionService) |
| 58 | + s.mockUserProfileService = new(MockUserProfileService) |
| 59 | + s.testDecisionContext = ExperimentDecisionContext{ |
| 60 | + Experiment: &testExp1113, |
| 61 | + ProjectConfig: s.mockProjectConfig, |
| 62 | + } |
| 63 | + |
| 64 | + computedVariation := testExp1113.Variations["2223"] |
| 65 | + s.testComputedDecision = ExperimentDecision{ |
| 66 | + Variation: &computedVariation, |
| 67 | + } |
| 68 | + s.mockExperimentService.On("GetDecision", s.testDecisionContext, testUserContext).Return(s.testComputedDecision, nil) |
| 69 | +} |
| 70 | + |
| 71 | +func (s *PersistingExperimentServiceTestSuite) TestNilUserProfileService() { |
| 72 | + persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, nil) |
| 73 | + decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext) |
| 74 | + s.Equal(s.testComputedDecision, decision) |
| 75 | + s.NoError(err) |
| 76 | + s.mockExperimentService.AssertExpectations(s.T()) |
| 77 | +} |
| 78 | + |
| 79 | +func (s *PersistingExperimentServiceTestSuite) TestSavedVariationFound() { |
| 80 | + decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID) |
| 81 | + savedUserProfile := UserProfile{ |
| 82 | + ID: testUserContext.ID, |
| 83 | + ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: testExp1113Var2224.ID}, |
| 84 | + } |
| 85 | + s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(savedUserProfile) |
| 86 | + s.mockUserProfileService.On("Save", mock.Anything) |
| 87 | + |
| 88 | + persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService) |
| 89 | + decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext) |
| 90 | + savedDecision := ExperimentDecision{ |
| 91 | + Variation: &testExp1113Var2224, |
| 92 | + } |
| 93 | + s.Equal(savedDecision, decision) |
| 94 | + s.NoError(err) |
| 95 | + s.mockExperimentService.AssertNotCalled(s.T(), "GetDecision", s.testDecisionContext, testUserContext) |
| 96 | + s.mockUserProfileService.AssertNotCalled(s.T(), "Save", mock.Anything) |
| 97 | +} |
| 98 | + |
| 99 | +func (s *PersistingExperimentServiceTestSuite) TestNoSavedVariation() { |
| 100 | + s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(UserProfile{ID: testUserContext.ID}) // empty user profile |
| 101 | + decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID) |
| 102 | + updatedUserProfile := UserProfile{ |
| 103 | + ID: testUserContext.ID, |
| 104 | + ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: s.testComputedDecision.Variation.ID}, |
| 105 | + } |
| 106 | + |
| 107 | + s.mockUserProfileService.On("Save", updatedUserProfile) |
| 108 | + persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService) |
| 109 | + decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext) |
| 110 | + s.Equal(s.testComputedDecision, decision) |
| 111 | + s.NoError(err) |
| 112 | + s.mockExperimentService.AssertExpectations(s.T()) |
| 113 | + s.mockUserProfileService.AssertExpectations(s.T()) |
| 114 | +} |
| 115 | + |
| 116 | +func (s *PersistingExperimentServiceTestSuite) TestSavedVariationNoLongerValid() { |
| 117 | + decisionKey := NewUserDecisionKey(s.testDecisionContext.Experiment.ID) |
| 118 | + savedUserProfile := UserProfile{ |
| 119 | + ID: testUserContext.ID, |
| 120 | + ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: "forgotten_variation"}, |
| 121 | + } |
| 122 | + s.mockUserProfileService.On("Lookup", testUserContext.ID).Return(savedUserProfile) // empty user profile |
| 123 | + |
| 124 | + updatedUserProfile := UserProfile{ |
| 125 | + ID: testUserContext.ID, |
| 126 | + ExperimentBucketMap: map[UserDecisionKey]string{decisionKey: s.testComputedDecision.Variation.ID}, |
| 127 | + } |
| 128 | + s.mockUserProfileService.On("Save", updatedUserProfile) |
| 129 | + persistingExperimentService := NewPersistingExperimentService(s.mockExperimentService, s.mockUserProfileService) |
| 130 | + decision, err := persistingExperimentService.GetDecision(s.testDecisionContext, testUserContext) |
| 131 | + s.Equal(s.testComputedDecision, decision) |
| 132 | + s.NoError(err) |
| 133 | + s.mockExperimentService.AssertExpectations(s.T()) |
| 134 | + s.mockUserProfileService.AssertExpectations(s.T()) |
| 135 | +} |
| 136 | + |
| 137 | +func TestPersistingExperimentServiceTestSuite(t *testing.T) { |
| 138 | + suite.Run(t, new(PersistingExperimentServiceTestSuite)) |
| 139 | +} |
0 commit comments