@@ -19,18 +19,33 @@ package decision
19
19
import (
20
20
"testing"
21
21
22
- "github.com/stretchr/testify/assert "
22
+ "github.com/stretchr/testify/suite "
23
23
24
24
"github.com/optimizely/go-sdk/optimizely/entities"
25
25
)
26
26
27
- func TestCompositeExperimentServiceGetDecision (t * testing.T ) {
28
- mockProjectConfig := new (mockProjectConfig )
29
- testDecisionContext := ExperimentDecisionContext {
27
+ type CompositeExperimentTestSuite struct {
28
+ suite.Suite
29
+ mockConfig * mockProjectConfig
30
+ mockExperimentService * MockExperimentDecisionService
31
+ mockExperimentService2 * MockExperimentDecisionService
32
+ testDecisionContext ExperimentDecisionContext
33
+ }
34
+
35
+ func (s * CompositeExperimentTestSuite ) SetupTest () {
36
+ s .mockConfig = new (mockProjectConfig )
37
+ s .mockExperimentService = new (MockExperimentDecisionService )
38
+ s .mockExperimentService2 = new (MockExperimentDecisionService )
39
+
40
+ // Setup test data
41
+ s .testDecisionContext = ExperimentDecisionContext {
30
42
Experiment : & testExp1111 ,
31
- ProjectConfig : mockProjectConfig ,
43
+ ProjectConfig : s . mockConfig ,
32
44
}
45
+ }
33
46
47
+ func (s * CompositeExperimentTestSuite ) TestGetDecision () {
48
+ // test that we return out of the decision making and the next one does not get called
34
49
testUserContext := entities.UserContext {
35
50
ID : "test_user_1" ,
36
51
}
@@ -39,102 +54,67 @@ func TestCompositeExperimentServiceGetDecision(t *testing.T) {
39
54
expectedExperimentDecision := ExperimentDecision {
40
55
Variation : & expectedVariation ,
41
56
}
42
- // test that we return out of the decision making and the next one doesn't get called
43
- mockExperimentDecisionService := new (MockExperimentDecisionService )
44
- mockExperimentDecisionService .On ("GetDecision" , testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
57
+ s .mockExperimentService .On ("GetDecision" , s .testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
45
58
46
- mockExperimentDecisionService2 := new (MockExperimentDecisionService )
47
59
compositeExperimentService := & CompositeExperimentService {
48
- experimentServices : []ExperimentService {mockExperimentDecisionService , mockExperimentDecisionService2 },
60
+ experimentServices : []ExperimentService {s .mockExperimentService , s .mockExperimentService2 },
61
+ }
62
+ decision , err := compositeExperimentService .GetDecision (s .testDecisionContext , testUserContext )
63
+ s .Equal (expectedExperimentDecision , decision )
64
+ s .NoError (err )
65
+ s .mockExperimentService .AssertExpectations (s .T ())
66
+ s .mockExperimentService2 .AssertNotCalled (s .T (), "GetDecision" )
67
+
68
+ }
69
+
70
+ func (s * CompositeExperimentTestSuite ) TestGetDecisionFallthrough () {
71
+ // test that we move onto the next decision service if no decision is made
72
+ testUserContext := entities.UserContext {
73
+ ID : "test_user_1" ,
49
74
}
50
- decision , err := compositeExperimentService .GetDecision (testDecisionContext , testUserContext )
51
- mockExperimentDecisionService .AssertExpectations (t )
52
- mockExperimentDecisionService2 .AssertNotCalled (t , "GetDecision" )
53
75
54
- // test that we move on to the next decision service if no decision is made
55
- mockExperimentDecisionService = new (MockExperimentDecisionService )
56
- expectedExperimentDecision = ExperimentDecision {}
57
- mockExperimentDecisionService .On ("GetDecision" , testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
76
+ expectedVariation := testExp1111 .Variations ["2222" ]
77
+ expectedExperimentDecision := ExperimentDecision {}
78
+ s .mockExperimentService .On ("GetDecision" , s .testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
58
79
59
- mockExperimentDecisionService2 = new (MockExperimentDecisionService )
60
80
expectedExperimentDecision2 := ExperimentDecision {
61
81
Variation : & expectedVariation ,
62
82
}
63
- mockExperimentDecisionService2 . On ("GetDecision" , testDecisionContext , testUserContext ).Return (expectedExperimentDecision2 , nil )
83
+ s . mockExperimentService2 . On ("GetDecision" , s . testDecisionContext , testUserContext ).Return (expectedExperimentDecision2 , nil )
64
84
65
- compositeExperimentService = & CompositeExperimentService {
66
- experimentServices : []ExperimentService {mockExperimentDecisionService , mockExperimentDecisionService2 },
85
+ compositeExperimentService : = & CompositeExperimentService {
86
+ experimentServices : []ExperimentService {s . mockExperimentService , s . mockExperimentService2 },
67
87
}
68
- decision , err = compositeExperimentService .GetDecision (testDecisionContext , testUserContext )
88
+ decision , err : = compositeExperimentService .GetDecision (s . testDecisionContext , testUserContext )
69
89
70
- assert .NoError (t , err )
71
- assert .Equal (t , expectedExperimentDecision2 , decision )
72
- mockExperimentDecisionService .AssertExpectations (t )
73
- mockExperimentDecisionService2 .AssertExpectations (t )
90
+ s .NoError (err )
91
+ s .Equal (expectedExperimentDecision2 , decision )
92
+ s .mockExperimentService .AssertExpectations (s .T ())
93
+ s .mockExperimentService2 .AssertExpectations (s .T ())
94
+ }
74
95
96
+ func (s * CompositeExperimentTestSuite ) TestGetDecisionNoDecisionsMade () {
75
97
// test when no decisions are made
76
- mockExperimentDecisionService = new (MockExperimentDecisionService )
77
- expectedExperimentDecision = ExperimentDecision {}
78
- mockExperimentDecisionService .On ("GetDecision" , testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
79
-
80
- mockExperimentDecisionService2 = new (MockExperimentDecisionService )
81
- expectedExperimentDecision2 = ExperimentDecision {}
82
- mockExperimentDecisionService2 .On ("GetDecision" , testDecisionContext , testUserContext ).Return (expectedExperimentDecision2 , nil )
83
-
84
- compositeExperimentService = & CompositeExperimentService {
85
- experimentServices : []ExperimentService {mockExperimentDecisionService , mockExperimentDecisionService2 },
98
+ testUserContext := entities.UserContext {
99
+ ID : "test_user_1" ,
86
100
}
87
- decision , err = compositeExperimentService .GetDecision (testDecisionContext , testUserContext )
101
+ expectedExperimentDecision := ExperimentDecision {}
102
+ s .mockExperimentService .On ("GetDecision" , s .testDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
88
103
89
- assert .NoError (t , err )
90
- assert .Equal (t , expectedExperimentDecision2 , decision )
91
- mockExperimentDecisionService .AssertExpectations (t )
92
- mockExperimentDecisionService2 .AssertExpectations (t )
93
- }
104
+ expectedExperimentDecision2 := ExperimentDecision {}
105
+ s .mockExperimentService2 .On ("GetDecision" , s .testDecisionContext , testUserContext ).Return (expectedExperimentDecision2 , nil )
94
106
95
- func TestCompositeExperimentServiceGetDecisionTargeting (t * testing.T ) {
96
- testUserContext := entities.UserContext {
97
- ID : "test_user" ,
98
- }
99
- testAudienceMap := map [string ]entities.Audience {
100
- "5555" : testAudience5555 ,
107
+ compositeExperimentService := & CompositeExperimentService {
108
+ experimentServices : []ExperimentService {s .mockExperimentService , s .mockExperimentService2 },
101
109
}
102
- mockProjectConfig := new (mockProjectConfig )
103
- mockProjectConfig .On ("GetAudienceMap" ).Return (testAudienceMap )
110
+ decision , err := compositeExperimentService .GetDecision (s .testDecisionContext , testUserContext )
104
111
105
- testExperimentDecisionContext := ExperimentDecisionContext {
106
- Experiment : & testExp1112 ,
107
- ProjectConfig : mockProjectConfig ,
108
- }
109
- testCondTreeParams := entities .NewTreeParameters (& testUserContext , testAudienceMap )
110
-
111
- // Test user fails targeting
112
- mockAudienceTreeEvaluator := new (MockAudienceTreeEvaluator )
113
- mockAudienceTreeEvaluator .On ("Evaluate" , testExp1112 .AudienceConditionTree , testCondTreeParams ).Return (false )
114
- mockExperimentDecisionService := new (MockExperimentDecisionService )
115
- testCompositeExperimentService := & CompositeExperimentService {
116
- audienceTreeEvaluator : mockAudienceTreeEvaluator ,
117
- experimentServices : []ExperimentService {mockExperimentDecisionService },
118
- }
119
- decision , _ := testCompositeExperimentService .GetDecision (testExperimentDecisionContext , testUserContext )
120
- assert .Nil (t , decision .Variation )
121
- mockAudienceTreeEvaluator .AssertExpectations (t )
122
- mockExperimentDecisionService .AssertNotCalled (t , "GetDecision" )
112
+ s .NoError (err )
113
+ s .Equal (expectedExperimentDecision2 , decision )
114
+ s .mockExperimentService .AssertExpectations (s .T ())
115
+ s .mockExperimentService2 .AssertExpectations (s .T ())
116
+ }
123
117
124
- // Test user passes targeting, moves on to children decision services
125
- expectedExperimentDecision := ExperimentDecision {
126
- Variation : & testExp1112Var2222 ,
127
- }
128
- mockAudienceTreeEvaluator = new (MockAudienceTreeEvaluator )
129
- mockAudienceTreeEvaluator .On ("Evaluate" , testExp1112 .AudienceConditionTree , testCondTreeParams ).Return (true )
130
- mockExperimentDecisionService = new (MockExperimentDecisionService )
131
- mockExperimentDecisionService .On ("GetDecision" , testExperimentDecisionContext , testUserContext ).Return (expectedExperimentDecision , nil )
132
- testCompositeExperimentService = & CompositeExperimentService {
133
- audienceTreeEvaluator : mockAudienceTreeEvaluator ,
134
- experimentServices : []ExperimentService {mockExperimentDecisionService },
135
- }
136
- decision , _ = testCompositeExperimentService .GetDecision (testExperimentDecisionContext , testUserContext )
137
- assert .Equal (t , decision , expectedExperimentDecision )
138
- mockAudienceTreeEvaluator .AssertExpectations (t )
139
- mockExperimentDecisionService .AssertExpectations (t )
118
+ func TestCompositeExperimentTestSuite (t * testing.T ) {
119
+ suite .Run (t , new (CompositeExperimentTestSuite ))
140
120
}
0 commit comments