-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgeneric_variable_methods_test.go
More file actions
176 lines (159 loc) · 5.23 KB
/
generic_variable_methods_test.go
File metadata and controls
176 lines (159 loc) · 5.23 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package featurevisor
import "testing"
type typedConfig struct {
Theme string `json:"theme"`
}
type nestedPalette struct {
Name string `json:"name"`
Active bool `json:"active"`
}
type nestedLayout struct {
Columns int `json:"columns"`
Meta map[string]any `json:"meta"`
}
type complexConfig struct {
Theme string `json:"theme"`
Layout nestedLayout `json:"layout"`
Palettes []nestedPalette `json:"palettes"`
Flags []string `json:"flags"`
Threshold float64 `json:"threshold"`
}
type rolloutStep struct {
Name string `json:"name"`
Percentage float64 `json:"percentage"`
}
func TestGenericVariableMethods(t *testing.T) {
jsonDatafile := `{
"schemaVersion": "2",
"revision": "1.0",
"segments": {},
"features": {
"typed": {
"key": "typed",
"bucketBy": "userId",
"variablesSchema": {
"items": {
"key": "items",
"type": "array",
"defaultValue": ["a", "b", "c"]
},
"config": {
"key": "config",
"type": "object",
"defaultValue": {"theme": "dark"}
},
"complexConfig": {
"key": "complexConfig",
"type": "object",
"defaultValue": {
"theme": "light",
"layout": {
"columns": 3,
"meta": {
"region": "eu",
"version": 2
}
},
"palettes": [
{"name": "primary", "active": true},
{"name": "secondary", "active": false}
],
"flags": ["beta", "edge"],
"threshold": 0.85
}
},
"rolloutPlan": {
"key": "rolloutPlan",
"type": "array",
"defaultValue": [
{"name": "phase-1", "percentage": 10},
{"name": "phase-2", "percentage": 55.5},
{"name": "phase-3", "percentage": 100}
]
}
},
"traffic": [
{
"key": "all",
"segments": "*",
"percentage": 100000,
"enabled": true,
"allocation": []
}
]
}
}
}`
var datafile DatafileContent
if err := datafile.FromJSON(jsonDatafile); err != nil {
t.Fatalf("failed to parse datafile: %v", err)
}
sdk := CreateInstance(Options{Datafile: datafile})
context := Context{"userId": "123"}
var arr []string
if err := sdk.GetVariableArrayInto("typed", "items", context, &arr); err != nil {
t.Fatalf("expected array decode to succeed, got error: %v", err)
}
if len(arr) != 3 || arr[0] != "a" || arr[2] != "c" {
t.Fatalf("expected typed array values, got %#v", arr)
}
var config typedConfig
if err := sdk.GetVariableObjectInto("typed", "config", context, &config); err != nil {
t.Fatalf("expected object decode to succeed, got error: %v", err)
}
if config.Theme != "dark" {
t.Fatalf("expected typed object with theme=dark, got %#v", config)
}
var complex complexConfig
if err := sdk.GetVariableObjectInto("typed", "complexConfig", context, OverrideOptions{}, &complex); err != nil {
t.Fatalf("expected complex object decode to succeed, got error: %v", err)
}
if complex.Theme != "light" || complex.Layout.Columns != 3 || complex.Layout.Meta["region"] != "eu" {
t.Fatalf("unexpected complex config core fields: %#v", complex)
}
if len(complex.Palettes) != 2 || complex.Palettes[0].Name != "primary" || !complex.Palettes[0].Active {
t.Fatalf("unexpected complex config palettes: %#v", complex.Palettes)
}
if len(complex.Flags) != 2 || complex.Flags[1] != "edge" || complex.Threshold != 0.85 {
t.Fatalf("unexpected complex config flags/threshold: %#v", complex)
}
var rollout []rolloutStep
if err := sdk.GetVariableArrayInto("typed", "rolloutPlan", context, OverrideOptions{}, &rollout); err != nil {
t.Fatalf("expected rollout decode to succeed, got error: %v", err)
}
if len(rollout) != 3 {
t.Fatalf("expected 3 rollout entries, got %#v", rollout)
}
if rollout[0].Name != "phase-1" || rollout[1].Percentage != 55.5 || rollout[2].Percentage != 100 {
t.Fatalf("unexpected rollout values: %#v", rollout)
}
child := sdk.Spawn(Context{"country": "nl"})
var childArr []string
if err := child.GetVariableArrayInto("typed", "items", &childArr); err != nil {
t.Fatalf("expected child array decode to succeed, got error: %v", err)
}
if len(childArr) != 3 || childArr[1] != "b" {
t.Fatalf("expected child typed array values, got %#v", childArr)
}
var childConfig typedConfig
if err := child.GetVariableObjectInto("typed", "config", &childConfig); err != nil {
t.Fatalf("expected child object decode to succeed, got error: %v", err)
}
if childConfig.Theme != "dark" {
t.Fatalf("expected child typed object with theme=dark, got %#v", childConfig)
}
var childComplex complexConfig
if err := child.GetVariableObjectInto("typed", "complexConfig", Context{}, &childComplex); err != nil {
t.Fatalf("expected child complex object decode to succeed, got error: %v", err)
}
if childComplex.Layout.Meta["region"] != "eu" || childComplex.Palettes[1].Name != "secondary" {
t.Fatalf("unexpected child complex config: %#v", childComplex)
}
var childRollout []rolloutStep
if err := child.GetVariableArrayInto("typed", "rolloutPlan", Context{}, &childRollout); err != nil {
t.Fatalf("expected child rollout decode to succeed, got error: %v", err)
}
if len(childRollout) != 3 || childRollout[2].Name != "phase-3" {
t.Fatalf("unexpected child rollout values: %#v", childRollout)
}
}