Skip to content

Commit 2d89df4

Browse files
author
Mike Davis
authored
Add unit tests for datafile project config. (#103)
1 parent 4457b50 commit 2d89df4

File tree

2 files changed

+400
-50
lines changed

2 files changed

+400
-50
lines changed

optimizely/config/datafileprojectconfig/config.go

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

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

2423
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/mappers"
@@ -76,47 +75,13 @@ func (c DatafileProjectConfig) GetBotFiltering() bool {
7675
return c.botFiltering
7776
}
7877

79-
// NewDatafileProjectConfig initializes a new datafile from a json byte array using the default JSON datafile parser
80-
func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, error) {
81-
datafile, err := Parse(jsonDatafile)
82-
if err != nil {
83-
logger.Error("Error parsing datafile.", err)
84-
return nil, err
85-
}
86-
87-
attributeMap, attributeKeyToIDMap := mappers.MapAttributes(datafile.Attributes)
88-
experimentMap, experimentKeyMap := mappers.MapExperiments(datafile.Experiments)
89-
rolloutMap := mappers.MapRollouts(datafile.Rollouts)
90-
eventMap := mappers.MapEvents(datafile.Events)
91-
mergedAudiences := append(datafile.TypedAudiences, datafile.Audiences...)
92-
config := &DatafileProjectConfig{
93-
accountID: datafile.AccountID,
94-
anonymizeIP: datafile.AnonymizeIP,
95-
attributeKeyToIDMap: attributeKeyToIDMap,
96-
audienceMap: mappers.MapAudiences(mergedAudiences),
97-
attributeMap: attributeMap,
98-
botFiltering: datafile.BotFiltering,
99-
experimentKeyToIDMap: experimentKeyMap,
100-
experimentMap: experimentMap,
101-
eventMap: eventMap,
102-
featureMap: mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentMap),
103-
projectID: datafile.ProjectID,
104-
revision: datafile.Revision,
105-
rolloutMap: rolloutMap,
106-
}
107-
108-
logger.Info("Datafile is valid.")
109-
return config, nil
110-
}
111-
11278
// GetEventByKey returns the event with the given key
11379
func (c DatafileProjectConfig) GetEventByKey(eventKey string) (entities.Event, error) {
11480
if event, ok := c.eventMap[eventKey]; ok {
11581
return event, nil
11682
}
11783

118-
errMessage := fmt.Sprintf("Event with key %s not found", eventKey)
119-
return entities.Event{}, errors.New(errMessage)
84+
return entities.Event{}, fmt.Errorf(`event with key "%s" not found`, eventKey)
12085
}
12186

12287
// GetFeatureByKey returns the feature with the given key
@@ -125,15 +90,14 @@ func (c DatafileProjectConfig) GetFeatureByKey(featureKey string) (entities.Feat
12590
return feature, nil
12691
}
12792

128-
errMessage := fmt.Sprintf("Feature with key %s not found", featureKey)
129-
return entities.Feature{}, errors.New(errMessage)
93+
return entities.Feature{}, fmt.Errorf(`feature with key "%s" not found`, featureKey)
13094
}
13195

13296
// GetVariableByKey returns the featureVariable with the given key
13397
func (c DatafileProjectConfig) GetVariableByKey(featureKey, variableKey string) (entities.Variable, error) {
13498

13599
var variable entities.Variable
136-
var err = fmt.Errorf("variable with key %s not found", featureKey)
100+
var err = fmt.Errorf(`variable with key "%s" not found`, featureKey)
137101
if feature, ok := c.featureMap[featureKey]; ok {
138102
for _, v := range feature.Variables {
139103
if v.Key == variableKey {
@@ -149,11 +113,12 @@ func (c DatafileProjectConfig) GetVariableByKey(featureKey, variableKey string)
149113
// GetAttributeByKey returns the attribute with the given key
150114
func (c DatafileProjectConfig) GetAttributeByKey(key string) (entities.Attribute, error) {
151115
if attributeID, ok := c.attributeKeyToIDMap[key]; ok {
152-
return c.attributeMap[attributeID], nil
116+
if attribute, ok := c.attributeMap[attributeID]; ok {
117+
return attribute, nil
118+
}
153119
}
154120

155-
errMessage := fmt.Sprintf(`Attribute with key "%s" not found`, key)
156-
return entities.Attribute{}, errors.New(errMessage)
121+
return entities.Attribute{}, fmt.Errorf(`attribute with key "%s" not found`, key)
157122
}
158123

159124
// GetFeatureList returns an array of all the features
@@ -170,8 +135,7 @@ func (c DatafileProjectConfig) GetAudienceByID(audienceID string) (entities.Audi
170135
return audience, nil
171136
}
172137

173-
errMessage := fmt.Sprintf(`Audience with ID "%s" not found`, audienceID)
174-
return entities.Audience{}, errors.New(errMessage)
138+
return entities.Audience{}, fmt.Errorf(`audience with ID "%s" not found`, audienceID)
175139
}
176140

177141
// GetAudienceMap returns the audience map
@@ -182,12 +146,12 @@ func (c DatafileProjectConfig) GetAudienceMap() map[string]entities.Audience {
182146
// GetExperimentByKey returns the experiment with the given key
183147
func (c DatafileProjectConfig) GetExperimentByKey(experimentKey string) (entities.Experiment, error) {
184148
if experimentID, ok := c.experimentKeyToIDMap[experimentKey]; ok {
185-
experiment := c.experimentMap[experimentID]
186-
return experiment, nil
149+
if experiment, ok := c.experimentMap[experimentID]; ok {
150+
return experiment, nil
151+
}
187152
}
188153

189-
errMessage := fmt.Sprintf(`Experiment with key "%s" not found`, experimentKey)
190-
return entities.Experiment{}, errors.New(errMessage)
154+
return entities.Experiment{}, fmt.Errorf(`experiment with key "%s" not found`, experimentKey)
191155
}
192156

193157
// GetGroupByID returns the group with the given ID
@@ -196,6 +160,38 @@ func (c DatafileProjectConfig) GetGroupByID(groupID string) (entities.Group, err
196160
return group, nil
197161
}
198162

199-
errMessage := fmt.Sprintf(`Group with ID "%s" not found`, groupID)
200-
return entities.Group{}, errors.New(errMessage)
163+
return entities.Group{}, fmt.Errorf(`group with ID "%s" not found`, groupID)
164+
}
165+
166+
// NewDatafileProjectConfig initializes a new datafile from a json byte array using the default JSON datafile parser
167+
func NewDatafileProjectConfig(jsonDatafile []byte) (*DatafileProjectConfig, error) {
168+
datafile, err := Parse(jsonDatafile)
169+
if err != nil {
170+
logger.Error("Error parsing datafile.", err)
171+
return nil, err
172+
}
173+
174+
attributeMap, attributeKeyToIDMap := mappers.MapAttributes(datafile.Attributes)
175+
experimentMap, experimentKeyMap := mappers.MapExperiments(datafile.Experiments)
176+
rolloutMap := mappers.MapRollouts(datafile.Rollouts)
177+
eventMap := mappers.MapEvents(datafile.Events)
178+
mergedAudiences := append(datafile.TypedAudiences, datafile.Audiences...)
179+
config := &DatafileProjectConfig{
180+
accountID: datafile.AccountID,
181+
anonymizeIP: datafile.AnonymizeIP,
182+
attributeKeyToIDMap: attributeKeyToIDMap,
183+
audienceMap: mappers.MapAudiences(mergedAudiences),
184+
attributeMap: attributeMap,
185+
botFiltering: datafile.BotFiltering,
186+
experimentKeyToIDMap: experimentKeyMap,
187+
experimentMap: experimentMap,
188+
eventMap: eventMap,
189+
featureMap: mappers.MapFeatures(datafile.FeatureFlags, rolloutMap, experimentMap),
190+
projectID: datafile.ProjectID,
191+
revision: datafile.Revision,
192+
rolloutMap: rolloutMap,
193+
}
194+
195+
logger.Info("Datafile is valid.")
196+
return config, nil
201197
}

0 commit comments

Comments
 (0)