github.com/gnolang/gno@v0.0.0-20240520182011-228e9d0192ce/tm2/pkg/db/common_test.go (about) 1 package db_test 2 3 import ( 4 "encoding/binary" 5 "fmt" 6 "sync" 7 "testing" 8 9 "github.com/gnolang/gno/tm2/pkg/db" 10 "github.com/gnolang/gno/tm2/pkg/db/internal" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 // ---------------------------------------- 16 // Test Helper functions. 17 18 func checkValue(t *testing.T, db db.DB, key []byte, valueWanted []byte) { 19 t.Helper() 20 21 valueGot := db.Get(key) 22 assert.Equal(t, valueWanted, valueGot) 23 } 24 25 func checkValid(t *testing.T, itr db.Iterator, expected bool) { 26 t.Helper() 27 28 valid := itr.Valid() 29 require.Equal(t, expected, valid) 30 } 31 32 func checkNext(t *testing.T, itr db.Iterator, expected bool) { 33 t.Helper() 34 35 itr.Next() 36 valid := itr.Valid() 37 require.Equal(t, expected, valid) 38 } 39 40 func checkNextPanics(t *testing.T, itr db.Iterator) { 41 t.Helper() 42 43 assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't") 44 } 45 46 func checkDomain(t *testing.T, itr db.Iterator, start, end []byte) { 47 t.Helper() 48 49 ds, de := itr.Domain() 50 assert.Equal(t, start, ds, "checkDomain domain start incorrect") 51 assert.Equal(t, end, de, "checkDomain domain end incorrect") 52 } 53 54 func checkItem(t *testing.T, itr db.Iterator, key []byte, value []byte) { 55 t.Helper() 56 57 k, v := itr.Key(), itr.Value() 58 assert.Exactly(t, key, k) 59 assert.Exactly(t, value, v) 60 } 61 62 func checkInvalid(t *testing.T, itr db.Iterator) { 63 t.Helper() 64 65 checkValid(t, itr, false) 66 checkKeyPanics(t, itr) 67 checkValuePanics(t, itr) 68 checkNextPanics(t, itr) 69 } 70 71 func checkKeyPanics(t *testing.T, itr db.Iterator) { 72 t.Helper() 73 74 assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't") 75 } 76 77 func checkValuePanics(t *testing.T, itr db.Iterator) { 78 t.Helper() 79 80 assert.Panics(t, func() { itr.Value() }, "checkValuePanics expected panic but didn't") 81 } 82 83 // ---------------------------------------- 84 // mockDB 85 86 // NOTE: not actually goroutine safe. 87 // If you want something goroutine safe, maybe you just want a MemDB. 88 type mockDB struct { 89 mtx sync.Mutex 90 calls map[string]int 91 } 92 93 func newMockDB() *mockDB { 94 return &mockDB{ 95 calls: make(map[string]int), 96 } 97 } 98 99 func (mdb *mockDB) Mutex() *sync.Mutex { 100 return &(mdb.mtx) 101 } 102 103 func (mdb *mockDB) Get([]byte) []byte { 104 mdb.calls["Get"]++ 105 return nil 106 } 107 108 func (mdb *mockDB) Has([]byte) bool { 109 mdb.calls["Has"]++ 110 return false 111 } 112 113 func (mdb *mockDB) Set([]byte, []byte) { 114 mdb.calls["Set"]++ 115 } 116 117 func (mdb *mockDB) SetSync([]byte, []byte) { 118 mdb.calls["SetSync"]++ 119 } 120 121 func (mdb *mockDB) SetNoLock([]byte, []byte) { 122 mdb.calls["SetNoLock"]++ 123 } 124 125 func (mdb *mockDB) SetNoLockSync([]byte, []byte) { 126 mdb.calls["SetNoLockSync"]++ 127 } 128 129 func (mdb *mockDB) Delete([]byte) { 130 mdb.calls["Delete"]++ 131 } 132 133 func (mdb *mockDB) DeleteSync([]byte) { 134 mdb.calls["DeleteSync"]++ 135 } 136 137 func (mdb *mockDB) DeleteNoLock([]byte) { 138 mdb.calls["DeleteNoLock"]++ 139 } 140 141 func (mdb *mockDB) DeleteNoLockSync([]byte) { 142 mdb.calls["DeleteNoLockSync"]++ 143 } 144 145 func (mdb *mockDB) Iterator(start, end []byte) db.Iterator { 146 mdb.calls["Iterator"]++ 147 return &internal.MockIterator{} 148 } 149 150 func (mdb *mockDB) ReverseIterator(start, end []byte) db.Iterator { 151 mdb.calls["ReverseIterator"]++ 152 return &internal.MockIterator{} 153 } 154 155 func (mdb *mockDB) Close() { 156 mdb.calls["Close"]++ 157 } 158 159 func (mdb *mockDB) NewBatch() db.Batch { 160 mdb.calls["NewBatch"]++ 161 return &internal.MemBatch{DB: mdb} 162 } 163 164 func (mdb *mockDB) Print() { 165 mdb.calls["Print"]++ 166 fmt.Printf("mockDB{%v}", mdb.Stats()) 167 } 168 169 func (mdb *mockDB) Stats() map[string]string { 170 mdb.calls["Stats"]++ 171 172 res := make(map[string]string) 173 for key, count := range mdb.calls { 174 res[key] = fmt.Sprintf("%d", count) 175 } 176 return res 177 } 178 179 func int642Bytes(i int64) []byte { 180 buf := make([]byte, 8) 181 binary.BigEndian.PutUint64(buf, uint64(i)) 182 return buf 183 } 184 185 func bytes2Int64(buf []byte) int64 { 186 return int64(binary.BigEndian.Uint64(buf)) 187 } 188 189 // For testing convenience. 190 func bz(s string) []byte { 191 return []byte(s) 192 }