github.com/dshekhar95/sub_dgraph@v0.0.0-20230424164411-6be28e40bbf1/dgraph/cmd/root_test.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 "io" 6 "strings" 7 "testing" 8 ) 9 10 func TestConvertJSON(t *testing.T) { 11 hier := `{ 12 "mutations": "strict", 13 "badger": { 14 "compression": "zstd:1", 15 "goroutines": 5 16 }, 17 "raft": { 18 "idx": 2, 19 "learner": true 20 }, 21 "security": { 22 "whitelist": "127.0.0.1,0.0.0.0" 23 } 24 }` 25 conv, err := io.ReadAll(convertJSON(hier)) 26 if err != nil { 27 t.Fatal("error reading from convertJSON") 28 } 29 unchanged, err := io.ReadAll(convertJSON(string(conv))) 30 if err != nil { 31 t.Fatal("error reading from convertJSON") 32 } 33 if string(unchanged) != string(conv) { 34 t.Fatal("convertJSON mutating already flattened string") 35 } 36 // need both permutations because convertJSON iterates through Go hashmaps in undefined order 37 if (!strings.Contains(string(conv), "compression=zstd:1; goroutines=5;") && 38 !strings.Contains(string(conv), "goroutines=5; compression=zstd:1;")) || 39 (!strings.Contains(string(conv), "idx=2; learner=true;") && 40 !strings.Contains(string(conv), "learner=true; idx=2;")) || 41 !strings.Contains(string(conv), "whitelist=127.0.0.1,0.0.0.0") { 42 fmt.Println(string(conv)) 43 t.Fatal("convertJSON not converting properly") 44 } 45 } 46 47 func TestConvertYAML(t *testing.T) { 48 hier := ` 49 mutations: strict 50 badger: 51 compression: zstd:1 52 goroutines: 5 53 raft: 54 idx: 2 55 learner: true 56 security: 57 whitelist: "127.0.0.1,0.0.0.0"` 58 59 conv, err := io.ReadAll(convertYAML(hier)) 60 if err != nil { 61 t.Fatal("error reading from convertYAML") 62 } 63 unchanged, err := io.ReadAll(convertYAML(string(conv))) 64 if err != nil { 65 t.Fatal("error reading from convertYAML") 66 } 67 if string(unchanged) != string(conv) { 68 t.Fatal("convertYAML mutating already flattened string") 69 } 70 if !strings.Contains(string(conv), "compression=zstd:1; goroutines=5;") || 71 !strings.Contains(string(conv), "idx=2; learner=true;") || 72 !strings.Contains(string(conv), "whitelist=127.0.0.1,0.0.0.0") { 73 t.Fatal("convertYAML not converting properly") 74 } 75 }