github.com/koko1123/flow-go-1@v0.29.6/ledger/complete/mtrie/trie/json_test.go (about) 1 package trie_test 2 3 import ( 4 "bytes" 5 "strings" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 11 "github.com/koko1123/flow-go-1/ledger" 12 "github.com/koko1123/flow-go-1/ledger/common/pathfinder" 13 "github.com/koko1123/flow-go-1/ledger/complete" 14 "github.com/koko1123/flow-go-1/ledger/complete/mtrie" 15 "github.com/koko1123/flow-go-1/ledger/complete/mtrie/trie" 16 "github.com/koko1123/flow-go-1/module/metrics" 17 ) 18 19 func Test_DumpJSONEmpty(t *testing.T) { 20 21 trie := trie.NewEmptyMTrie() 22 23 var buffer bytes.Buffer 24 err := trie.DumpAsJSON(&buffer) 25 require.NoError(t, err) 26 27 json := buffer.String() 28 assert.Empty(t, json) 29 } 30 31 func Test_DumpJSONNonEmpty(t *testing.T) { 32 33 forest, err := mtrie.NewForest(complete.DefaultCacheSize, &metrics.NoopCollector{}, nil) 34 require.NoError(t, err) 35 36 emptyRootHash := forest.GetEmptyRootHash() 37 38 key1 := ledger.NewKey([]ledger.KeyPart{ 39 ledger.NewKeyPart(0, []byte("si")), 40 ledger.NewKeyPart(5, []byte("vis")), 41 ledger.NewKeyPart(3, []byte("pacem")), 42 }) 43 44 key2 := ledger.NewKey([]ledger.KeyPart{ 45 ledger.NewKeyPart(3, []byte("ex")), 46 ledger.NewKeyPart(6, []byte("navicula")), 47 ledger.NewKeyPart(9, []byte("navis")), 48 }) 49 50 key3 := ledger.NewKey([]ledger.KeyPart{ 51 ledger.NewKeyPart(9, []byte("lorem")), 52 ledger.NewKeyPart(0, []byte("ipsum")), 53 ledger.NewKeyPart(5, []byte("dolor")), 54 }) 55 56 update, err := ledger.NewUpdate(ledger.State(emptyRootHash), []ledger.Key{key1, key2, key3}, []ledger.Value{{1}, {2}, {3}}) 57 require.NoError(t, err) 58 59 trieUpdate, err := pathfinder.UpdateToTrieUpdate(update, 0) 60 require.NoError(t, err) 61 62 newHash, err := forest.Update(trieUpdate) 63 require.NoError(t, err) 64 65 newTrie, err := forest.GetTrie(newHash) 66 require.NoError(t, err) 67 68 var buffer bytes.Buffer 69 70 err = newTrie.DumpAsJSON(&buffer) 71 require.NoError(t, err) 72 73 json := buffer.String() 74 split := strings.Split(json, "\n") 75 76 //filter out empty strings 77 jsons := make([]string, 0) 78 for _, s := range split { 79 if len(s) > 0 { 80 jsons = append(jsons, s) 81 } 82 } 83 84 require.Len(t, jsons, 3) 85 86 // key 1 87 require.Contains(t, jsons, "{\"Key\":{\"KeyParts\":[{\"Type\":0,\"Value\":\"7369\"},{\"Type\":5,\"Value\":\"766973\"},{\"Type\":3,\"Value\":\"706163656d\"}]},\"Value\":\"01\"}") 88 89 // key 2 90 require.Contains(t, jsons, "{\"Key\":{\"KeyParts\":[{\"Type\":3,\"Value\":\"6578\"},{\"Type\":6,\"Value\":\"6e61766963756c61\"},{\"Type\":9,\"Value\":\"6e61766973\"}]},\"Value\":\"02\"}") 91 92 // key 3 93 require.Contains(t, jsons, "{\"Key\":{\"KeyParts\":[{\"Type\":9,\"Value\":\"6c6f72656d\"},{\"Type\":0,\"Value\":\"697073756d\"},{\"Type\":5,\"Value\":\"646f6c6f72\"}]},\"Value\":\"03\"}") 94 }