github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/pkg/config/env_test.go (about) 1 package config 2 3 import ( 4 "testing" 5 6 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/configtypes" 7 ) 8 9 func Test_setByPath(t *testing.T) { 10 var result struct { 11 Int int 12 Uint uint 13 Bool bool 14 String string 15 configtypes.Config 16 } 17 18 if err := setByPath(&result, []string{"INT"}, "42"); err != nil { 19 t.Fatal(err) 20 } 21 if v := result.Int; v != 42 { 22 t.Fatal("wrong value", v) 23 } 24 25 if err := setByPath(&result, []string{"UINT"}, "2"); err != nil { 26 t.Fatal(err) 27 } 28 if v := result.Uint; v != 2 { 29 t.Fatal("wrong value", v) 30 } 31 32 if err := setByPath(&result, []string{"BOOL"}, "true"); err != nil { 33 t.Fatal(err) 34 } 35 if v := result.Bool; !v { 36 t.Fatal("wrong value", v) 37 } 38 39 if err := setByPath(&result, []string{"STRING"}, "hello"); err != nil { 40 t.Fatal(err) 41 } 42 if v := result.String; v != "hello" { 43 t.Fatal("wrong value", v) 44 } 45 46 if err := setByPath(&result, []string{"CONFIG", "VERSION", "CURRENT"}, "v3.0.0-release"); err != nil { 47 t.Fatal(err) 48 } 49 if v := result.Version.Current; v != "v3.0.0-release" { 50 t.Fatal("wrong value", v) 51 } 52 53 if err := setByPath(&result, []string{"CONFIG", "KEYS", "PROVIDER", "APIKEY"}, "test-key"); err != nil { 54 t.Fatal(err) 55 } 56 if v := result.Keys["provider"].ApiKey; v != "test-key" { 57 t.Fatal("wrong value", v) 58 } 59 // Make sure we don't override the whole map when setting another key in already initialized map. 60 if err := setByPath(&result, []string{"CONFIG", "KEYS", "PROVIDER", "SECRET"}, "secret"); err != nil { 61 t.Fatal(err) 62 } 63 if v := result.Keys["provider"].ApiKey; v != "test-key" { 64 t.Fatal("wrong value", v) 65 } 66 if v := result.Keys["provider"].Secret; v != "secret" { 67 t.Fatal("wrong value", v) 68 } 69 70 // Make sure we don't override the whole map when setting another key in already initialized map. 71 if err := setByPath(&result, []string{"CONFIG", "CHAINS", "MAINNET", "SCRAPE", "APPSPERCHUNK"}, "2000000"); err != nil { 72 t.Fatal(err) 73 } 74 if v := result.Chains["mainnet"].Scrape.AppsPerChunk; v != 2000000 { 75 t.Fatal("wrong value", v) 76 } 77 }