config_test.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package config
  2. import (
  3. "testing"
  4. )
  5. type testConfigWithMap struct {
  6. Modes map[string]string `json:"modes"`
  7. Exprs map[string]string `json:"exprs"`
  8. Name string `json:"name"`
  9. }
  10. func TestUpdateConfigFromMap_MapReplacement(t *testing.T) {
  11. cfg := &testConfigWithMap{
  12. Modes: map[string]string{
  13. "model-a": "tiered_expr",
  14. "model-b": "tiered_expr",
  15. },
  16. Exprs: map[string]string{
  17. "model-a": "p * 5 + c * 25",
  18. "model-b": "p * 10 + c * 50",
  19. },
  20. Name: "billing",
  21. }
  22. // Simulate removing model-a: new value only has model-b
  23. err := UpdateConfigFromMap(cfg, map[string]string{
  24. "modes": `{"model-b": "tiered_expr"}`,
  25. "exprs": `{"model-b": "p * 10 + c * 50"}`,
  26. })
  27. if err != nil {
  28. t.Fatalf("UpdateConfigFromMap failed: %v", err)
  29. }
  30. if _, ok := cfg.Modes["model-a"]; ok {
  31. t.Errorf("Modes still contains model-a after it was removed from the update; got %v", cfg.Modes)
  32. }
  33. if _, ok := cfg.Exprs["model-a"]; ok {
  34. t.Errorf("Exprs still contains model-a after it was removed from the update; got %v", cfg.Exprs)
  35. }
  36. if cfg.Modes["model-b"] != "tiered_expr" {
  37. t.Errorf("Modes[model-b] = %q, want %q", cfg.Modes["model-b"], "tiered_expr")
  38. }
  39. if cfg.Exprs["model-b"] != "p * 10 + c * 50" {
  40. t.Errorf("Exprs[model-b] = %q, want %q", cfg.Exprs["model-b"], "p * 10 + c * 50")
  41. }
  42. }
  43. func TestUpdateConfigFromMap_EmptyMapClearsAll(t *testing.T) {
  44. cfg := &testConfigWithMap{
  45. Modes: map[string]string{
  46. "model-a": "tiered_expr",
  47. },
  48. Exprs: map[string]string{
  49. "model-a": "p * 5 + c * 25",
  50. },
  51. }
  52. err := UpdateConfigFromMap(cfg, map[string]string{
  53. "modes": `{}`,
  54. "exprs": `{}`,
  55. })
  56. if err != nil {
  57. t.Fatalf("UpdateConfigFromMap failed: %v", err)
  58. }
  59. if len(cfg.Modes) != 0 {
  60. t.Errorf("Modes should be empty after updating with {}, got %v", cfg.Modes)
  61. }
  62. if len(cfg.Exprs) != 0 {
  63. t.Errorf("Exprs should be empty after updating with {}, got %v", cfg.Exprs)
  64. }
  65. }
  66. func TestUpdateConfigFromMap_ScalarFieldsUnchanged(t *testing.T) {
  67. cfg := &testConfigWithMap{
  68. Modes: map[string]string{"m": "v"},
  69. Name: "old",
  70. }
  71. err := UpdateConfigFromMap(cfg, map[string]string{
  72. "name": "new",
  73. })
  74. if err != nil {
  75. t.Fatalf("UpdateConfigFromMap failed: %v", err)
  76. }
  77. if cfg.Name != "new" {
  78. t.Errorf("Name = %q, want %q", cfg.Name, "new")
  79. }
  80. // modes was not in configMap, should remain unchanged
  81. if cfg.Modes["m"] != "v" {
  82. t.Errorf("Modes should be unchanged, got %v", cfg.Modes)
  83. }
  84. }