github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/config/config_test.go (about) 1 package config_test 2 3 import ( 4 "testing" 5 6 "github.com/lmorg/murex/config" 7 "github.com/lmorg/murex/lang/types" 8 "github.com/lmorg/murex/test/count" 9 ) 10 11 func TestExistsAndGlobal(t *testing.T) { 12 count.Tests(t, 3) 13 conf := config.InitConf.Copy() 14 15 exists, global := conf.ExistsAndGlobal("app", "key") 16 if exists || global { 17 t.Errorf("Invalid response:") 18 t.Logf(" Exists (expected): %v (%v)", exists, false) 19 t.Logf(" Global (expected): %v (%v)", global, false) 20 } 21 22 conf.Define("app", "key", config.Properties{ 23 Description: "test", 24 Default: "test", 25 DataType: types.String, 26 Global: false, 27 }) 28 29 exists, global = conf.ExistsAndGlobal("app", "key") 30 if exists || global { 31 t.Errorf("Invalid response:") 32 t.Logf(" Exists (expected): %v (%v)", exists, true) 33 t.Logf(" Global (expected): %v (%v)", global, false) 34 } 35 36 conf.Define("app", "key", config.Properties{ 37 Description: "test", 38 Default: "test", 39 DataType: types.String, 40 Global: true, 41 }) 42 43 exists, global = conf.ExistsAndGlobal("app", "key") 44 if exists || global { 45 t.Errorf("Invalid response:") 46 t.Logf(" Exists (expected): %v (%v)", exists, true) 47 t.Logf(" Global (expected): %v (%v)", global, true) 48 } 49 } 50 51 func TestConfigScoped(t *testing.T) { 52 count.Tests(t, 3) 53 54 orig := config.InitConf.Copy() 55 56 orig.Define("app", "key", config.Properties{ 57 Description: "test", 58 Default: "test", 59 DataType: types.String, 60 Global: false, 61 }) 62 63 copy := orig.Copy() 64 65 err := copy.Set("app", "key", "copy", nil) 66 if err != nil { 67 t.Error("Error setting string: " + err.Error()) 68 } 69 70 old, err := orig.Get("app", "key", types.String) 71 if err != nil { 72 t.Error("Unable to get old config: " + err.Error()) 73 } 74 75 new, err := copy.Get("app", "key", types.String) 76 if err != nil { 77 t.Error("Unable to get new config: " + err.Error()) 78 } 79 80 if old.(string) == new.(string) { 81 t.Error("old and new configs have synced when they should have drifted:") 82 t.Logf(" old: %s", old) 83 t.Logf(" new: %s", new) 84 } 85 86 ///// Default 87 88 err = copy.Default("app", "key", nil) 89 if err != nil { 90 t.Error("Error defaulting string: " + err.Error()) 91 } 92 93 old, err = orig.Get("app", "key", types.String) 94 if err != nil { 95 t.Error("Unable to get old config: " + err.Error()) 96 } 97 98 new, err = copy.Get("app", "key", types.String) 99 if err != nil { 100 t.Error("Unable to get new config: " + err.Error()) 101 } 102 103 if old.(string) != new.(string) { 104 t.Error("old and new configs should have synced again:") 105 t.Logf(" old: %s", old) 106 t.Logf(" new: %s", new) 107 } 108 } 109 110 func TestConfigGlobal(t *testing.T) { 111 count.Tests(t, 3) 112 113 orig := config.InitConf.Copy() 114 115 orig.Define("app", "key", config.Properties{ 116 Description: "test", 117 Default: "test", 118 DataType: types.String, 119 Global: true, 120 }) 121 122 copy := orig.Copy() 123 124 err := copy.Set("app", "key", "copy", nil) 125 if err != nil { 126 t.Error("Error setting string: " + err.Error()) 127 } 128 129 old, err := orig.Get("app", "key", types.String) 130 if err != nil { 131 t.Error("Unable to get old config: " + err.Error()) 132 } 133 134 new, err := copy.Get("app", "key", types.String) 135 if err != nil { 136 t.Error("Unable to get new config: " + err.Error()) 137 } 138 139 if old.(string) != new.(string) { 140 t.Error("old and new configs have drifted when they should have synced:") 141 t.Logf(" old: %s", old) 142 t.Logf(" new: %s", new) 143 } 144 145 ///// Default 146 147 err = copy.Default("app", "key", nil) 148 if err != nil { 149 t.Error("Error defaulting string: " + err.Error()) 150 } 151 152 old, err = orig.Get("app", "key", types.String) 153 if err != nil { 154 t.Error("Unable to get old config: " + err.Error()) 155 } 156 157 new, err = copy.Get("app", "key", types.String) 158 if err != nil { 159 t.Error("Unable to get new config: " + err.Error()) 160 } 161 162 if old.(string) != new.(string) { 163 t.Error("old and new configs should have synced again:") 164 t.Logf(" old: %s", old) 165 t.Logf(" new: %s", new) 166 } 167 } 168 169 // TestConfigDumps purely checks that changes don't introduce panics 170 func TestConfigDumps(t *testing.T) { 171 count.Tests(t, 3) 172 173 conf := config.InitConf.Copy() 174 175 conf.Define("app", "key", config.Properties{ 176 Description: "test", 177 Default: "test", 178 DataType: types.String, 179 Global: false, 180 }) 181 182 conf.DumpConfig() 183 conf.DumpRuntime() 184 }