Skip to content

Commit de222a1

Browse files
yasirfolio3Michael Ng
authored andcommitted
(fix): Fixed GetDecision for CompositeFeatureService and key for VisitorAttribute. (#149)
1 parent d604829 commit de222a1

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

pkg/decision/composite_feature_service.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
package decision
1919

2020
import (
21-
"errors"
2221
"fmt"
2322

2423
"github.com/optimizely/go-sdk/pkg/entities"
@@ -44,15 +43,16 @@ func NewCompositeFeatureService(compositeExperimentService ExperimentService) *C
4443

4544
// GetDecision returns a decision for the given feature and user context
4645
func (f CompositeFeatureService) GetDecision(decisionContext FeatureDecisionContext, userContext entities.UserContext) (FeatureDecision, error) {
46+
var featureDecision = FeatureDecision{}
47+
var err error
4748
for _, featureDecisionService := range f.featureServices {
48-
featureDecision, err := featureDecisionService.GetDecision(decisionContext, userContext)
49+
featureDecision, err = featureDecisionService.GetDecision(decisionContext, userContext)
4950
if err != nil {
5051
cfLogger.Debug(fmt.Sprintf("%v", err))
5152
}
5253
if featureDecision.Variation != nil && err == nil {
5354
return featureDecision, err
5455
}
5556
}
56-
57-
return FeatureDecision{}, errors.New("no decision was made")
57+
return featureDecision, err
5858
}

pkg/decision/composite_feature_service_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,33 @@ func (s *CompositeFeatureServiceTestSuite) TestGetDecisionReturnsError() {
128128
s.mockFeatureService2.AssertExpectations(s.T())
129129
}
130130

131+
func (s *CompositeFeatureServiceTestSuite) TestGetDecisionReturnsLastDecisionWithError() {
132+
// test that GetDecision returns the last decision with error if all decision services return error
133+
testUserContext := entities.UserContext{
134+
ID: "test_user_1",
135+
}
136+
137+
expectedDecision := FeatureDecision{
138+
Variation: &testExp1113Var2223,
139+
}
140+
s.mockFeatureService.On("GetDecision", s.testFeatureDecisionContext, testUserContext).Return(expectedDecision, errors.New("Error making decision"))
141+
142+
s.mockFeatureService2.On("GetDecision", s.testFeatureDecisionContext, testUserContext).Return(expectedDecision, errors.New("test error"))
143+
144+
compositeFeatureService := &CompositeFeatureService{
145+
featureServices: []FeatureService{
146+
s.mockFeatureService,
147+
s.mockFeatureService2,
148+
},
149+
}
150+
decision, err := compositeFeatureService.GetDecision(s.testFeatureDecisionContext, testUserContext)
151+
s.Equal(expectedDecision, decision)
152+
s.Error(err)
153+
s.Equal(err.Error(), "test error")
154+
s.mockFeatureService.AssertExpectations(s.T())
155+
s.mockFeatureService2.AssertExpectations(s.T())
156+
}
157+
131158
func (s *CompositeFeatureServiceTestSuite) TestNewCompositeFeatureService() {
132159
// Assert that the service is instantiated with the correct child services in the right order
133160
compositeExperimentService := NewCompositeExperimentService()

pkg/event/factory.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ func getEventAttributes(projectConfig pkg.ProjectConfig, attributes map[string]i
240240
efLogger.Debug(fmt.Sprintf("Unrecognized attribute %s provided. Pruning before sending event to Optimizely.", key))
241241
continue
242242
}
243-
visitorAttribute.Key = attribute.Key
243+
visitorAttribute.Key = key
244244
visitorAttribute.Value = value
245245
visitorAttribute.AttributeType = attributeType
246246

0 commit comments

Comments
 (0)