| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- 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)
- }
- }
|