Skip to content

Commit b570ef5

Browse files
test: improve coverage for config/... (#132)
* improve coverage for config/... * addressing after code review
1 parent c1c5396 commit b570ef5

File tree

8 files changed

+199
-24
lines changed

8 files changed

+199
-24
lines changed

optimizely/config/datafileprojectconfig/config_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,28 @@ import (
2222
"testing"
2323

2424
"github.com/optimizely/go-sdk/optimizely/entities"
25+
2526
"github.com/stretchr/testify/assert"
2627
)
2728

29+
func TestNewDatafileProjectConfigNil(t *testing.T) {
30+
projectConfig, err := NewDatafileProjectConfig(nil)
31+
assert.NotNil(t, err)
32+
assert.Nil(t, projectConfig)
33+
}
34+
35+
func TestNewDatafileProjectConfigNotNil(t *testing.T) {
36+
dpc := DatafileProjectConfig{accountID: "123", revision: "1", projectID: "12345"}
37+
jsonDatafileStr := `{"accountID": "123", "revision": "1", "projectId": "12345"}`
38+
jsonDatafile := []byte(jsonDatafileStr)
39+
projectConfig, err := NewDatafileProjectConfig(jsonDatafile)
40+
assert.Nil(t, err)
41+
assert.NotNil(t, projectConfig)
42+
assert.Equal(t, dpc.accountID, projectConfig.accountID)
43+
assert.Equal(t, dpc.revision, projectConfig.revision)
44+
assert.Equal(t, dpc.projectID, projectConfig.projectID)
45+
}
46+
2847
func TestGetProjectID(t *testing.T) {
2948
projectID := "projectID"
3049
config := &DatafileProjectConfig{
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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 mappers
18+
19+
import (
20+
"testing"
21+
22+
datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
23+
"github.com/optimizely/go-sdk/optimizely/entities"
24+
25+
"github.com/stretchr/testify/assert"
26+
)
27+
28+
func TestMapAttributesWithEmptyList(t *testing.T) {
29+
30+
attributeMap, attributeKeyToIDMap := MapAttributes(nil)
31+
32+
expectedAttributeMap := map[string]entities.Attribute{}
33+
expectedAttributeKeyToIDMap := map[string]string{}
34+
35+
assert.Equal(t, attributeMap, expectedAttributeMap)
36+
assert.Equal(t, attributeKeyToIDMap, expectedAttributeKeyToIDMap)
37+
}
38+
func TestMapAttributes(t *testing.T) {
39+
40+
attrList := []datafileEntities.Attribute{{ID: "1", Key: "one"}, {ID: "2", Key: "two"},
41+
{ID: "3", Key: "three"}, {ID: "2", Key: "four"}, {ID: "5", Key: "one"}}
42+
43+
attributeMap, attributeKeyToIDMap := MapAttributes(attrList)
44+
45+
expectedAttributeMap := map[string]entities.Attribute{"1": {"1", "one"},
46+
"2": {"2", "two"}, "3": {"3", "three"}, "5": {"5", "one"}}
47+
expectedAttributeKeyToIDMap := map[string]string{"one": "5", "three": "3", "two": "2"}
48+
49+
assert.Equal(t, attributeMap, expectedAttributeMap)
50+
assert.Equal(t, attributeKeyToIDMap, expectedAttributeKeyToIDMap)
51+
}

optimizely/config/datafileprojectconfig/mappers/audience_test.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,36 @@
1414
* limitations under the License. *
1515
***************************************************************************/
1616

17+
// Package mappers //
1718
package mappers
19+
20+
import (
21+
"testing"
22+
23+
datafileEntities "github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
24+
"github.com/optimizely/go-sdk/optimizely/entities"
25+
26+
"github.com/stretchr/testify/assert"
27+
)
28+
29+
func TestMapAudiencesEmptyList(t *testing.T) {
30+
31+
audienceMap := MapAudiences(nil)
32+
33+
expectedAudienceMap := map[string]entities.Audience{}
34+
35+
assert.Equal(t, audienceMap, expectedAudienceMap)
36+
37+
}
38+
func TestMapAudiences(t *testing.T) {
39+
40+
audienceList := []datafileEntities.Audience{{ID: "1", Name: "one"}, {ID: "2", Name: "two"},
41+
{ID: "3", Name: "three"}, {ID: "2", Name: "four"}, {ID: "5", Name: "one"}}
42+
43+
audienceMap := MapAudiences(audienceList)
44+
45+
expectedAudienceMap := map[string]entities.Audience{"1": {ID: "1", Name: "one"}, "2": {ID: "2", Name: "two"},
46+
"3": {ID: "3", Name: "three"}, "5": {ID: "5", Name: "one"}}
47+
48+
assert.Equal(t, audienceMap, expectedAudienceMap)
49+
}

optimizely/config/datafileprojectconfig/mappers/condition_trees_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,16 @@ import (
2424
"github.com/stretchr/testify/assert"
2525
)
2626

27+
func TestBuildAudienceConditionTreeEmpty(t *testing.T) {
28+
conditionString := ""
29+
var conditions interface{}
30+
json.Unmarshal([]byte(conditionString), &conditions)
31+
conditionTree, err := buildAudienceConditionTree(conditions)
32+
33+
assert.NotNil(t, err)
34+
assert.Equal(t, (*entities.TreeNode)(nil), conditionTree)
35+
}
36+
2737
func TestBuildAudienceConditionTreeSimpleAudienceCondition(t *testing.T) {
2838
conditionString := "[ \"and\", [ \"or\", [ \"or\", \"12\", \"123\", \"1234\"] ] ]"
2939
var conditions interface{}

optimizely/config/polling_manager_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package config
1818

1919
import (
2020
"testing"
21+
"time"
2122

2223
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig"
2324
"github.com/optimizely/go-sdk/optimizely/notification"
@@ -55,6 +56,7 @@ func TestNewPollingProjectConfigManagerWithOptions(t *testing.T) {
5556
assert.Nil(t, err)
5657
assert.NotNil(t, actual)
5758
assert.Equal(t, projectConfig, actual)
59+
exeCtx.TerminateAndWait() // just sending signal and improving coverage
5860
}
5961

6062
func TestNewPollingProjectConfigManagerWithNull(t *testing.T) {
@@ -155,4 +157,38 @@ func TestNewPollingProjectConfigManagerOnDecision(t *testing.T) {
155157

156158
err = configManager.RemoveOnProjectConfigUpdate(id)
157159
assert.Nil(t, err)
160+
161+
err = configManager.RemoveOnProjectConfigUpdate(id)
162+
assert.Nil(t, err)
163+
}
164+
165+
func TestDefaultRequester(t *testing.T) {
166+
167+
sdkKey := "test_sdk_key"
168+
DefaultRequester(sdkKey)
169+
exeCtx := utils.NewCancelableExecutionCtx()
170+
configManager := NewPollingProjectConfigManager(exeCtx, sdkKey, DefaultRequester(sdkKey))
171+
requester := configManager.requester
172+
assert.NotNil(t, requester)
173+
assert.Equal(t, requester.String(), "{url: https://cdn.optimizely.com/datafiles/test_sdk_key.json, timeout: 5s, retries: 1}")
174+
}
175+
176+
func TestPollingInterval(t *testing.T) {
177+
178+
sdkKey := "test_sdk_key"
179+
DefaultRequester(sdkKey)
180+
exeCtx := utils.NewCancelableExecutionCtx()
181+
configManager := NewPollingProjectConfigManager(exeCtx, sdkKey, PollingInterval(5*time.Second))
182+
183+
assert.Equal(t, configManager.pollingInterval, 5*time.Second)
184+
}
185+
186+
func TestInitialDatafile(t *testing.T) {
187+
188+
sdkKey := "test_sdk_key"
189+
DefaultRequester(sdkKey)
190+
exeCtx := utils.NewCancelableExecutionCtx()
191+
configManager := NewPollingProjectConfigManager(exeCtx, sdkKey, InitialDatafile([]byte("test")))
192+
193+
assert.Equal(t, configManager.initDatafile, []byte("test"))
158194
}

optimizely/config/static_manager.go

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,14 @@
1818
package config
1919

2020
import (
21-
"bytes"
2221
"errors"
2322
"fmt"
24-
"io"
25-
"net/http"
2623
"sync"
2724

2825
"github.com/optimizely/go-sdk/optimizely"
2926
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig"
3027
"github.com/optimizely/go-sdk/optimizely/notification"
28+
"github.com/optimizely/go-sdk/optimizely/utils"
3129
)
3230

3331
// StaticProjectConfigManager maintains a static copy of the project config
@@ -38,30 +36,14 @@ type StaticProjectConfigManager struct {
3836

3937
// NewStaticProjectConfigManagerFromURL returns new instance of StaticProjectConfigManager for URL
4038
func NewStaticProjectConfigManagerFromURL(sdkKey string) (*StaticProjectConfigManager, error) {
41-
downloadFile := func() ([]byte, error) {
42-
response, err := http.Get(fmt.Sprintf(DatafileURLTemplate, sdkKey))
43-
if err != nil {
44-
return nil, err
45-
}
46-
defer response.Body.Close()
47-
if response.StatusCode != http.StatusOK {
48-
return nil, errors.New(response.Status)
49-
}
50-
var data bytes.Buffer
51-
_, err = io.Copy(&data, response.Body)
52-
if err != nil {
53-
return nil, err
54-
}
55-
return data.Bytes(), nil
56-
}
57-
58-
body, err := downloadFile()
5939

60-
if err != nil {
61-
return nil, err
40+
requester := utils.NewHTTPRequester(fmt.Sprintf(DatafileURLTemplate, sdkKey))
41+
datafile, code, e := requester.Get()
42+
if e != nil {
43+
cmLogger.Error(fmt.Sprintf("request returned with http code=%d", code), e)
6244
}
6345

64-
return NewStaticProjectConfigManagerFromPayload(body)
46+
return NewStaticProjectConfigManagerFromPayload(datafile)
6547
}
6648

6749
// NewStaticProjectConfigManagerFromPayload returns new instance of StaticProjectConfigManager for payload

optimizely/config/static_manager_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package config
1818

1919
import (
20+
"errors"
21+
"github.com/optimizely/go-sdk/optimizely/notification"
2022
"testing"
2123

2224
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig"
@@ -30,3 +32,44 @@ func TestNewStaticProjectConfigManager(t *testing.T) {
3032
actual, _ := configManager.GetConfig()
3133
assert.Equal(t, projectConfig, actual)
3234
}
35+
36+
func TestNewStaticProjectConfigManagerFromPayload(t *testing.T) {
37+
38+
mockDatafile := []byte(`{"accountId":"42","projectId":"123""}`)
39+
configManager, err := NewStaticProjectConfigManagerFromPayload(mockDatafile)
40+
assert.NotNil(t, err)
41+
42+
mockDatafile = []byte(`{"accountId":"42","projectId":"123"}`)
43+
configManager, err = NewStaticProjectConfigManagerFromPayload(mockDatafile)
44+
assert.Nil(t, err)
45+
46+
actual, _ := configManager.GetConfig()
47+
assert.NotNil(t, actual)
48+
}
49+
50+
func TestNewStaticProjectConfigManagerFromURL(t *testing.T) {
51+
52+
configManager, err := NewStaticProjectConfigManagerFromURL("no_key_exists")
53+
assert.NotNil(t, err)
54+
assert.Nil(t, configManager)
55+
}
56+
57+
func TestNewStaticProjectConfigManagerOnDecision(t *testing.T) {
58+
mockDatafile := []byte(`{"accountId":"42","projectId":"123"}`)
59+
configManager, err := NewStaticProjectConfigManagerFromPayload(mockDatafile)
60+
assert.Nil(t, err)
61+
62+
callback := func(notification notification.ProjectConfigUpdateNotification) {
63+
64+
}
65+
id, err := configManager.OnProjectConfigUpdate(callback)
66+
67+
assert.NotNil(t, err)
68+
assert.Equal(t, err, errors.New("method OnProjectConfigUpdate does not have any effect on StaticProjectConfigManager"))
69+
assert.Equal(t, id, 0)
70+
71+
err = configManager.RemoveOnProjectConfigUpdate(id)
72+
assert.NotNil(t, err)
73+
assert.Equal(t, err, errors.New("method RemoveOnProjectConfigUpdate does not have any effect on StaticProjectConfigManager"))
74+
75+
}

optimizely/utils/requester.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ type Requester interface {
4343

4444
Post(body interface{}, headers ...Header) (response []byte, code int, err error)
4545
PostObj(body interface{}, result interface{}, headers ...Header) error
46+
47+
String() string
4648
}
4749

4850
// Header element to be sent

0 commit comments

Comments
 (0)