Repository navigation
Expand file tree
/
Copy pathinstance_variations_test.go
More file actions
108 lines (97 loc) · 2.95 KB
/
Copy pathinstance_variations_test.go
File metadata and controls
108 lines (97 loc) · 2.95 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
package featurevisor
import (
"testing"
)
func TestVariationsWithForceRules(t *testing.T) {
jsonDatafile := `{
"schemaVersion": "2",
"revision": "1.0",
"features": {
"test": {
"key": "test",
"bucketBy": "userId",
"variations": [
{"value": "control"},
{"value": "treatment"}
],
"force": [
{
"conditions": "[{\"attribute\":\"userId\",\"operator\":\"equals\",\"value\":\"user-gb\"}]",
"enabled": false
},
{
"segments": "netherlands",
"enabled": false
}
],
"traffic": [
{
"key": "1",
"segments": "*",
"percentage": 100000,
"allocation": [
{"variation": "control", "range": [0, 0]},
{"variation": "treatment", "range": [0, 100000]}
]
}
]
},
"testWithNoVariation": {
"key": "testWithNoVariation",
"bucketBy": "userId",
"traffic": [
{
"key": "1",
"segments": "*",
"percentage": 100000,
"allocation": []
}
]
}
},
"segments": {
"netherlands": {
"key": "netherlands",
"conditions": "[{\"attribute\":\"country\",\"operator\":\"equals\",\"value\":\"nl\"}]"
}
}
}`
var datafile DatafileContent
if err := datafile.FromJSON(jsonDatafile); err != nil {
t.Fatalf("Failed to parse datafile JSON: %v", err)
}
sdk := CreateFeaturevisor(FeaturevisorOptions{
Datafile: datafile,
})
context := Context{"userId": "123"}
// Should get treatment variation for normal case
variation := sdk.GetVariation("test", context, OverrideOptions{})
if variation == nil || *variation != "treatment" {
t.Errorf("Expected variation to be 'treatment', got '%v'", variation)
}
// Should get treatment variation for Swiss users
variation = sdk.GetVariation("test", Context{"userId": "user-ch"}, OverrideOptions{})
if variation == nil || *variation != "treatment" {
t.Errorf("Expected variation to be 'treatment' for Swiss users, got '%v'", variation)
}
// Should return nil for non-existing feature
variation = sdk.GetVariation("nonExistingFeature", context, OverrideOptions{})
if variation != nil {
t.Errorf("Expected variation to be nil for non-existing feature, got '%v'", variation)
}
// Should return nil for disabled feature (user-gb)
variation = sdk.GetVariation("test", Context{"userId": "user-gb"}, OverrideOptions{})
if variation != nil {
t.Errorf("Expected variation to be nil for disabled feature (user-gb), got '%v'", variation)
}
// Should return nil for disabled feature (Dutch users)
variation = sdk.GetVariation("test", Context{"userId": "123", "country": "nl"}, OverrideOptions{})
if variation != nil {
t.Errorf("Expected variation to be nil for disabled feature (Dutch users), got '%v'", variation)
}
// Should return nil for feature with no variations
variation = sdk.GetVariation("testWithNoVariation", context, OverrideOptions{})
if variation != nil {
t.Errorf("Expected variation to be nil for feature with no variations, got '%v'", variation)
}
}