-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathinstance_deprecated_features_test.go
More file actions
117 lines (102 loc) · 2.69 KB
/
instance_deprecated_features_test.go
File metadata and controls
117 lines (102 loc) · 2.69 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
package featurevisor
import (
"strings"
"testing"
)
func TestDeprecatedFeatures(t *testing.T) {
var deprecatedCount int
var capturedLogs []string
// Create a custom logger to capture warnings
level := LogLevelWarn
handler := LogHandler(func(level LogLevel, message LogMessage, details LogDetails) {
if level == LogLevelWarn && strings.Contains(string(message), "is deprecated") {
deprecatedCount++
}
capturedLogs = append(capturedLogs, string(message))
})
customLogger := NewLogger(CreateLoggerOptions{
Level: &level,
Handler: &handler,
})
jsonDatafile := `{
"schemaVersion": "2",
"revision": "1.0",
"features": {
"test": {
"key": "test",
"bucketBy": "userId",
"variations": [
{"value": "control"},
{"value": "treatment"}
],
"traffic": [
{
"key": "1",
"segments": "*",
"percentage": 100000,
"allocation": [
{"variation": "control", "range": [0, 100000]},
{"variation": "treatment", "range": [0, 0]}
]
}
]
},
"deprecatedTest": {
"key": "deprecatedTest",
"deprecated": true,
"bucketBy": "userId",
"variations": [
{"value": "control"},
{"value": "treatment"}
],
"traffic": [
{
"key": "1",
"segments": "*",
"percentage": 100000,
"allocation": [
{"variation": "control", "range": [0, 100000]},
{"variation": "treatment", "range": [0, 0]}
]
}
]
}
},
"segments": {}
}`
var datafile DatafileContent
if err := datafile.FromJSON(jsonDatafile); err != nil {
t.Fatalf("Failed to parse datafile JSON: %v", err)
}
sdk := CreateInstance(Options{
Datafile: datafile,
Logger: customLogger,
})
context := Context{"userId": "123"}
testVariation := sdk.GetVariation("test", context, OverrideOptions{})
deprecatedTestVariation := sdk.GetVariation("deprecatedTest", context, OverrideOptions{})
if testVariation == nil || *testVariation != "control" {
t.Errorf("Expected test variation to be 'control', got '%v'", testVariation)
}
if deprecatedTestVariation == nil || *deprecatedTestVariation != "control" {
t.Errorf("Expected deprecated test variation to be 'control', got '%v'", deprecatedTestVariation)
}
if deprecatedCount != 1 {
t.Errorf("Expected 1 deprecated warning, got %d", deprecatedCount)
}
// Check that the warning message contains the expected content
foundDeprecatedWarning := false
for _, log := range capturedLogs {
if strings.Contains(log, "deprecated") {
foundDeprecatedWarning = true
break
}
}
if !foundDeprecatedWarning {
t.Error("Expected to find deprecated warning in logs")
}
}
// Helper function to create bool pointers
func boolPtr(b bool) *bool {
return &b
}