-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathflags_state_test.go
81 lines (65 loc) · 2.2 KB
/
flags_state_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package ldclient
import (
"encoding/json"
"testing"
"github.com/stretchr/testify/assert"
)
func TestFlagsStateCanGetFlagValue(t *testing.T) {
flag := FeatureFlag{Key: "key"}
state := newFeatureFlagsState()
state.addFlag(&flag, "value", intPtr(1), nil, false)
assert.Equal(t, "value", state.GetFlagValue("key"))
}
func TestFlagsStateUnknownFlagReturnsNilValue(t *testing.T) {
state := newFeatureFlagsState()
assert.Nil(t, state.GetFlagValue("key"))
}
func TestFlagsStateCanGetFlagReason(t *testing.T) {
flag := FeatureFlag{Key: "key"}
state := newFeatureFlagsState()
state.addFlag(&flag, "value", intPtr(1), evalReasonOffInstance, false)
assert.Equal(t, evalReasonOffInstance, state.GetFlagReason("key"))
}
func TestFlagsStateUnknownFlagReturnsNilReason(t *testing.T) {
state := newFeatureFlagsState()
assert.Nil(t, state.GetFlagReason("key"))
}
func TestFlagsStateReturnsNilReasonIfReasonsWereNotRecored(t *testing.T) {
flag := FeatureFlag{Key: "key"}
state := newFeatureFlagsState()
state.addFlag(&flag, "value", intPtr(1), nil, false)
assert.Nil(t, state.GetFlagReason("key"))
}
func TestFlagsStateToValuesMap(t *testing.T) {
flag1 := FeatureFlag{Key: "key1"}
flag2 := FeatureFlag{Key: "key2"}
state := newFeatureFlagsState()
state.addFlag(&flag1, "value1", intPtr(0), nil, false)
state.addFlag(&flag2, "value2", intPtr(1), nil, false)
expected := map[string]interface{}{"key1": "value1", "key2": "value2"}
assert.Equal(t, expected, state.ToValuesMap())
}
func TestFlagsStateToJSON(t *testing.T) {
date := uint64(1000)
flag1 := FeatureFlag{Key: "key1", Version: 100, TrackEvents: false}
flag2 := FeatureFlag{Key: "key2", Version: 200, TrackEvents: true, DebugEventsUntilDate: &date}
state := newFeatureFlagsState()
state.addFlag(&flag1, "value1", intPtr(0), nil, false)
state.addFlag(&flag2, "value2", intPtr(1), nil, false)
expectedString := `{
"key1":"value1",
"key2":"value2",
"$flagsState":{
"key1":{
"variation":0,"version":100
},
"key2": {
"variation":1,"version":200,"trackEvents":true,"debugEventsUntilDate":1000
}
},
"$valid":true
}`
actualBytes, err := json.Marshal(state)
assert.NoError(t, err)
assert.JSONEq(t, expectedString, string(actualBytes))
}