|
14 | 14 | * limitations under the License. *
|
15 | 15 | ***************************************************************************/
|
16 | 16 |
|
17 |
| - package decision |
18 |
| - |
19 |
| - import ( |
20 |
| - "fmt" |
21 |
| - "testing" |
22 |
| - |
23 |
| - "github.com/optimizely/go-sdk/v2/pkg/decide" |
24 |
| - "github.com/optimizely/go-sdk/v2/pkg/decision/reasons" |
25 |
| - "github.com/optimizely/go-sdk/v2/pkg/logging" |
26 |
| - |
27 |
| - "github.com/optimizely/go-sdk/v2/pkg/entities" |
28 |
| - "github.com/stretchr/testify/mock" |
29 |
| - "github.com/stretchr/testify/suite" |
30 |
| - ) |
31 |
| - |
32 |
| - type MockBucketer struct { |
33 |
| - mock.Mock |
34 |
| - } |
35 |
| - |
36 |
| - func (m *MockBucketer) Bucket(bucketingID string, experiment entities.Experiment, group entities.Group) (*entities.Variation, reasons.Reason, error) { |
37 |
| - args := m.Called(bucketingID, experiment, group) |
38 |
| - return args.Get(0).(*entities.Variation), args.Get(1).(reasons.Reason), args.Error(2) |
39 |
| - } |
40 |
| - |
41 |
| - // Add the new method to satisfy the ExperimentBucketer interface |
42 |
| - func (m *MockBucketer) BucketToEntityID(bucketingID string, experiment entities.Experiment, group entities.Group) (string, reasons.Reason, error) { |
43 |
| - args := m.Called(bucketingID, experiment, group) |
44 |
| - return args.String(0), args.Get(1).(reasons.Reason), args.Error(2) |
45 |
| - } |
46 |
| - |
47 |
| - type MockLogger struct { |
48 |
| - mock.Mock |
49 |
| - } |
50 |
| - |
51 |
| - func (m *MockLogger) Debug(message string) { |
52 |
| - m.Called(message) |
53 |
| - } |
54 |
| - |
55 |
| - func (m *MockLogger) Info(message string) { |
56 |
| - m.Called(message) |
57 |
| - } |
58 |
| - |
59 |
| - func (m *MockLogger) Warning(message string) { |
60 |
| - m.Called(message) |
61 |
| - } |
62 |
| - |
63 |
| - func (m *MockLogger) Error(message string, err interface{}) { |
64 |
| - m.Called(message, err) |
65 |
| - } |
66 |
| - |
67 |
| - type ExperimentBucketerTestSuite struct { |
68 |
| - suite.Suite |
69 |
| - mockBucketer *MockBucketer |
70 |
| - mockLogger *MockLogger |
71 |
| - mockConfig *mockProjectConfig |
72 |
| - options *decide.Options |
73 |
| - reasons decide.DecisionReasons |
74 |
| - } |
75 |
| - |
76 |
| - func (s *ExperimentBucketerTestSuite) SetupTest() { |
77 |
| - s.mockBucketer = new(MockBucketer) |
78 |
| - s.mockLogger = new(MockLogger) |
79 |
| - s.mockConfig = new(mockProjectConfig) |
80 |
| - s.options = &decide.Options{} |
81 |
| - s.reasons = decide.NewDecisionReasons(s.options) |
82 |
| - } |
83 |
| - |
84 |
| - func (s *ExperimentBucketerTestSuite) TestGetDecisionNoTargeting() { |
85 |
| - testUserContext := entities.UserContext{ |
86 |
| - ID: "test_user_1", |
87 |
| - } |
88 |
| - |
89 |
| - expectedDecision := ExperimentDecision{ |
90 |
| - Variation: &testExp1111Var2222, |
91 |
| - Decision: Decision{ |
92 |
| - Reason: reasons.BucketedIntoVariation, |
93 |
| - }, |
94 |
| - } |
95 |
| - |
96 |
| - testDecisionContext := ExperimentDecisionContext{ |
97 |
| - Experiment: &testExp1111, |
98 |
| - ProjectConfig: s.mockConfig, |
99 |
| - } |
100 |
| - s.mockBucketer.On("Bucket", testUserContext.ID, testExp1111, entities.Group{}).Return(&testExp1111Var2222, reasons.BucketedIntoVariation, nil) |
101 |
| - s.mockLogger.On("Debug", fmt.Sprintf(logging.ExperimentAudiencesEvaluatedTo.String(), "test_experiment_1111", true)) |
102 |
| - experimentBucketerService := ExperimentBucketerService{ |
103 |
| - bucketer: s.mockBucketer, |
104 |
| - logger: s.mockLogger, |
105 |
| - } |
106 |
| - s.options.IncludeReasons = true |
107 |
| - decision, rsons, err := experimentBucketerService.GetDecision(testDecisionContext, testUserContext, s.options) |
108 |
| - messages := rsons.ToReport() |
109 |
| - s.Len(messages, 1) |
110 |
| - s.Equal(`Audiences for experiment test_experiment_1111 collectively evaluated to true.`, messages[0]) |
111 |
| - s.Equal(expectedDecision, decision) |
112 |
| - s.NoError(err) |
113 |
| - s.mockLogger.AssertExpectations(s.T()) |
114 |
| - } |
115 |
| - |
| 17 | +package decision |
| 18 | + |
| 19 | +import ( |
| 20 | + "fmt" |
| 21 | + "testing" |
| 22 | + |
| 23 | + "github.com/optimizely/go-sdk/v2/pkg/decide" |
| 24 | + "github.com/optimizely/go-sdk/v2/pkg/decision/reasons" |
| 25 | + "github.com/optimizely/go-sdk/v2/pkg/logging" |
| 26 | + |
| 27 | + "github.com/optimizely/go-sdk/v2/pkg/entities" |
| 28 | + "github.com/stretchr/testify/mock" |
| 29 | + "github.com/stretchr/testify/suite" |
| 30 | +) |
| 31 | + |
| 32 | +type MockBucketer struct { |
| 33 | + mock.Mock |
| 34 | +} |
| 35 | + |
| 36 | +func (m *MockBucketer) Bucket(bucketingID string, experiment entities.Experiment, group entities.Group) (*entities.Variation, reasons.Reason, error) { |
| 37 | + args := m.Called(bucketingID, experiment, group) |
| 38 | + return args.Get(0).(*entities.Variation), args.Get(1).(reasons.Reason), args.Error(2) |
| 39 | +} |
| 40 | + |
| 41 | +// Add the new method to satisfy the ExperimentBucketer interface |
| 42 | +func (m *MockBucketer) BucketToEntityID(bucketingID string, experiment entities.Experiment, group entities.Group) (string, reasons.Reason, error) { |
| 43 | + args := m.Called(bucketingID, experiment, group) |
| 44 | + return args.String(0), args.Get(1).(reasons.Reason), args.Error(2) |
| 45 | +} |
| 46 | + |
| 47 | +type MockLogger struct { |
| 48 | + mock.Mock |
| 49 | +} |
| 50 | + |
| 51 | +func (m *MockLogger) Debug(message string) { |
| 52 | + m.Called(message) |
| 53 | +} |
| 54 | + |
| 55 | +func (m *MockLogger) Info(message string) { |
| 56 | + m.Called(message) |
| 57 | +} |
| 58 | + |
| 59 | +func (m *MockLogger) Warning(message string) { |
| 60 | + m.Called(message) |
| 61 | +} |
| 62 | + |
| 63 | +func (m *MockLogger) Error(message string, err interface{}) { |
| 64 | + m.Called(message, err) |
| 65 | +} |
| 66 | + |
| 67 | +type ExperimentBucketerTestSuite struct { |
| 68 | + suite.Suite |
| 69 | + mockBucketer *MockBucketer |
| 70 | + mockLogger *MockLogger |
| 71 | + mockConfig *mockProjectConfig |
| 72 | + options *decide.Options |
| 73 | + reasons decide.DecisionReasons |
| 74 | +} |
| 75 | + |
| 76 | +func (s *ExperimentBucketerTestSuite) SetupTest() { |
| 77 | + s.mockBucketer = new(MockBucketer) |
| 78 | + s.mockLogger = new(MockLogger) |
| 79 | + s.mockConfig = new(mockProjectConfig) |
| 80 | + s.options = &decide.Options{} |
| 81 | + s.reasons = decide.NewDecisionReasons(s.options) |
| 82 | +} |
| 83 | + |
| 84 | +func (s *ExperimentBucketerTestSuite) TestGetDecisionNoTargeting() { |
| 85 | + testUserContext := entities.UserContext{ |
| 86 | + ID: "test_user_1", |
| 87 | + } |
| 88 | + |
| 89 | + expectedDecision := ExperimentDecision{ |
| 90 | + Variation: &testExp1111Var2222, |
| 91 | + Decision: Decision{ |
| 92 | + Reason: reasons.BucketedIntoVariation, |
| 93 | + }, |
| 94 | + } |
| 95 | + |
| 96 | + testDecisionContext := ExperimentDecisionContext{ |
| 97 | + Experiment: &testExp1111, |
| 98 | + ProjectConfig: s.mockConfig, |
| 99 | + } |
| 100 | + s.mockBucketer.On("Bucket", testUserContext.ID, testExp1111, entities.Group{}).Return(&testExp1111Var2222, reasons.BucketedIntoVariation, nil) |
| 101 | + s.mockLogger.On("Debug", fmt.Sprintf(logging.ExperimentAudiencesEvaluatedTo.String(), "test_experiment_1111", true)) |
| 102 | + experimentBucketerService := ExperimentBucketerService{ |
| 103 | + bucketer: s.mockBucketer, |
| 104 | + logger: s.mockLogger, |
| 105 | + } |
| 106 | + s.options.IncludeReasons = true |
| 107 | + decision, rsons, err := experimentBucketerService.GetDecision(testDecisionContext, testUserContext, s.options) |
| 108 | + messages := rsons.ToReport() |
| 109 | + s.Len(messages, 1) |
| 110 | + s.Equal(`Audiences for experiment test_experiment_1111 collectively evaluated to true.`, messages[0]) |
| 111 | + s.Equal(expectedDecision, decision) |
| 112 | + s.NoError(err) |
| 113 | + s.mockLogger.AssertExpectations(s.T()) |
| 114 | +} |
116 | 115 |
|
117 | 116 | func (s *ExperimentBucketerTestSuite) TestGetDecisionWithTargetingPasses() {
|
118 | 117 | testUserContext := entities.UserContext{
|
|
0 commit comments