forked from QuantumNous/new-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig_test.go
More file actions
96 lines (84 loc) · 2.38 KB
/
Copy pathconfig_test.go
File metadata and controls
96 lines (84 loc) · 2.38 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
package config
import (
"testing"
)
type testConfigWithMap struct {
Modes map[string]string `json:"modes"`
Exprs map[string]string `json:"exprs"`
Name string `json:"name"`
}
func TestUpdateConfigFromMap_MapReplacement(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{
"model-a": "tiered_expr",
"model-b": "tiered_expr",
},
Exprs: map[string]string{
"model-a": "p * 5 + c * 25",
"model-b": "p * 10 + c * 50",
},
Name: "billing",
}
// Simulate removing model-a: new value only has model-b
err := UpdateConfigFromMap(cfg, map[string]string{
"modes": `{"model-b": "tiered_expr"}`,
"exprs": `{"model-b": "p * 10 + c * 50"}`,
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if _, ok := cfg.Modes["model-a"]; ok {
t.Errorf("Modes still contains model-a after it was removed from the update; got %v", cfg.Modes)
}
if _, ok := cfg.Exprs["model-a"]; ok {
t.Errorf("Exprs still contains model-a after it was removed from the update; got %v", cfg.Exprs)
}
if cfg.Modes["model-b"] != "tiered_expr" {
t.Errorf("Modes[model-b] = %q, want %q", cfg.Modes["model-b"], "tiered_expr")
}
if cfg.Exprs["model-b"] != "p * 10 + c * 50" {
t.Errorf("Exprs[model-b] = %q, want %q", cfg.Exprs["model-b"], "p * 10 + c * 50")
}
}
func TestUpdateConfigFromMap_EmptyMapClearsAll(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{
"model-a": "tiered_expr",
},
Exprs: map[string]string{
"model-a": "p * 5 + c * 25",
},
}
err := UpdateConfigFromMap(cfg, map[string]string{
"modes": `{}`,
"exprs": `{}`,
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if len(cfg.Modes) != 0 {
t.Errorf("Modes should be empty after updating with {}, got %v", cfg.Modes)
}
if len(cfg.Exprs) != 0 {
t.Errorf("Exprs should be empty after updating with {}, got %v", cfg.Exprs)
}
}
func TestUpdateConfigFromMap_ScalarFieldsUnchanged(t *testing.T) {
cfg := &testConfigWithMap{
Modes: map[string]string{"m": "v"},
Name: "old",
}
err := UpdateConfigFromMap(cfg, map[string]string{
"name": "new",
})
if err != nil {
t.Fatalf("UpdateConfigFromMap failed: %v", err)
}
if cfg.Name != "new" {
t.Errorf("Name = %q, want %q", cfg.Name, "new")
}
// modes was not in configMap, should remain unchanged
if cfg.Modes["m"] != "v" {
t.Errorf("Modes should be unchanged, got %v", cfg.Modes)
}
}