|
| 1 | +// Copyright (C) 2019-2025, Ava Labs, Inc. All rights reserved. |
| 2 | +// See the file LICENSE for licensing terms. |
| 3 | + |
| 4 | +package dbtest |
| 5 | + |
| 6 | +import ( |
| 7 | + "bytes" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | + |
| 12 | + "github.com/ava-labs/avalanchego/database" |
| 13 | +) |
| 14 | + |
| 15 | +// Tests is a list of all database tests |
| 16 | +var Tests = []struct { |
| 17 | + Name string |
| 18 | + Test func(t *testing.T, newDB func() database.HeightIndex) |
| 19 | +}{ |
| 20 | + {"TestPutGet", TestPutGet}, |
| 21 | + {"TestHas", TestHas}, |
| 22 | + {"TestCloseAndPut", TestCloseAndPut}, |
| 23 | + {"TestCloseAndGet", TestCloseAndGet}, |
| 24 | + {"TestCloseAndHas", TestCloseAndHas}, |
| 25 | + {"TestClose", TestClose}, |
| 26 | +} |
| 27 | + |
| 28 | +type putArgs struct { |
| 29 | + height uint64 |
| 30 | + data []byte |
| 31 | +} |
| 32 | + |
| 33 | +func TestPutGet(t *testing.T, newDB func() database.HeightIndex) { |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + puts []putArgs |
| 37 | + queryHeight uint64 |
| 38 | + want []byte |
| 39 | + wantErr error |
| 40 | + }{ |
| 41 | + { |
| 42 | + name: "normal operation", |
| 43 | + puts: []putArgs{ |
| 44 | + {1, []byte("test data 1")}, |
| 45 | + }, |
| 46 | + queryHeight: 1, |
| 47 | + want: []byte("test data 1"), |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "not found error when getting on non-existing height", |
| 51 | + puts: []putArgs{ |
| 52 | + {1, []byte("test data")}, |
| 53 | + }, |
| 54 | + queryHeight: 2, |
| 55 | + wantErr: database.ErrNotFound, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "overwriting data on same height", |
| 59 | + puts: []putArgs{ |
| 60 | + {1, []byte("original data")}, |
| 61 | + {1, []byte("overwritten data")}, |
| 62 | + }, |
| 63 | + queryHeight: 1, |
| 64 | + want: []byte("overwritten data"), |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "put and get nil data", |
| 68 | + puts: []putArgs{ |
| 69 | + {1, nil}, |
| 70 | + }, |
| 71 | + queryHeight: 1, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: "put and get empty bytes", |
| 75 | + puts: []putArgs{ |
| 76 | + {1, []byte{}}, |
| 77 | + }, |
| 78 | + queryHeight: 1, |
| 79 | + }, |
| 80 | + { |
| 81 | + name: "put and get large data", |
| 82 | + puts: []putArgs{ |
| 83 | + {1, make([]byte, 1000)}, |
| 84 | + }, |
| 85 | + queryHeight: 1, |
| 86 | + want: make([]byte, 1000), |
| 87 | + }, |
| 88 | + } |
| 89 | + |
| 90 | + for _, tt := range tests { |
| 91 | + t.Run(tt.name, func(t *testing.T) { |
| 92 | + db := newDB() |
| 93 | + defer func() { |
| 94 | + require.NoError(t, db.Close()) |
| 95 | + }() |
| 96 | + |
| 97 | + // Perform all puts |
| 98 | + for _, write := range tt.puts { |
| 99 | + require.NoError(t, db.Put(write.height, write.data)) |
| 100 | + } |
| 101 | + |
| 102 | + // modify the original value of the put data to ensure the saved |
| 103 | + // value won't be changed after Get |
| 104 | + if len(tt.puts) > int(tt.queryHeight) && tt.puts[tt.queryHeight].data != nil { |
| 105 | + copy(tt.puts[tt.queryHeight].data, []byte("modified data")) |
| 106 | + } |
| 107 | + |
| 108 | + // Query the specific height |
| 109 | + retrievedData, err := db.Get(tt.queryHeight) |
| 110 | + require.ErrorIs(t, err, tt.wantErr) |
| 111 | + require.True(t, bytes.Equal(tt.want, retrievedData)) |
| 112 | + |
| 113 | + // modify the data returned from Get and ensure it won't change the |
| 114 | + // data from a second Get |
| 115 | + copy(retrievedData, []byte("modified data")) |
| 116 | + newData, err := db.Get(tt.queryHeight) |
| 117 | + require.ErrorIs(t, err, tt.wantErr) |
| 118 | + require.True(t, bytes.Equal(tt.want, newData)) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func TestHas(t *testing.T, newDB func() database.HeightIndex) { |
| 124 | + tests := []struct { |
| 125 | + name string |
| 126 | + puts []putArgs |
| 127 | + queryHeight uint64 |
| 128 | + want bool |
| 129 | + }{ |
| 130 | + { |
| 131 | + name: "non-existent item", |
| 132 | + queryHeight: 1, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "existing item with data", |
| 136 | + puts: []putArgs{{1, []byte("test data")}}, |
| 137 | + queryHeight: 1, |
| 138 | + want: true, |
| 139 | + }, |
| 140 | + { |
| 141 | + name: "existing item with nil data", |
| 142 | + puts: []putArgs{{1, nil}}, |
| 143 | + queryHeight: 1, |
| 144 | + want: true, |
| 145 | + }, |
| 146 | + { |
| 147 | + name: "existing item with empty bytes", |
| 148 | + puts: []putArgs{{1, []byte{}}}, |
| 149 | + queryHeight: 1, |
| 150 | + want: true, |
| 151 | + }, |
| 152 | + { |
| 153 | + name: "has returns true on overridden height", |
| 154 | + puts: []putArgs{ |
| 155 | + {1, []byte("original data")}, |
| 156 | + {1, []byte("overridden data")}, |
| 157 | + }, |
| 158 | + queryHeight: 1, |
| 159 | + want: true, |
| 160 | + }, |
| 161 | + } |
| 162 | + |
| 163 | + for _, tt := range tests { |
| 164 | + t.Run(tt.name, func(t *testing.T) { |
| 165 | + db := newDB() |
| 166 | + defer func() { |
| 167 | + require.NoError(t, db.Close()) |
| 168 | + }() |
| 169 | + |
| 170 | + // Perform all puts |
| 171 | + for _, write := range tt.puts { |
| 172 | + require.NoError(t, db.Put(write.height, write.data)) |
| 173 | + } |
| 174 | + |
| 175 | + ok, err := db.Has(tt.queryHeight) |
| 176 | + require.NoError(t, err) |
| 177 | + require.Equal(t, tt.want, ok) |
| 178 | + }) |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +func TestCloseAndPut(t *testing.T, newDB func() database.HeightIndex) { |
| 183 | + db := newDB() |
| 184 | + require.NoError(t, db.Close()) |
| 185 | + |
| 186 | + // Try to put after close - should return error |
| 187 | + err := db.Put(1, []byte("test")) |
| 188 | + require.ErrorIs(t, err, database.ErrClosed) |
| 189 | +} |
| 190 | + |
| 191 | +func TestCloseAndGet(t *testing.T, newDB func() database.HeightIndex) { |
| 192 | + db := newDB() |
| 193 | + require.NoError(t, db.Close()) |
| 194 | + |
| 195 | + // Try to get after close - should return error |
| 196 | + _, err := db.Get(1) |
| 197 | + require.ErrorIs(t, err, database.ErrClosed) |
| 198 | +} |
| 199 | + |
| 200 | +func TestCloseAndHas(t *testing.T, newDB func() database.HeightIndex) { |
| 201 | + db := newDB() |
| 202 | + require.NoError(t, db.Close()) |
| 203 | + |
| 204 | + // Try to has after close - should return error |
| 205 | + _, err := db.Has(1) |
| 206 | + require.ErrorIs(t, err, database.ErrClosed) |
| 207 | +} |
| 208 | + |
| 209 | +func TestClose(t *testing.T, newDB func() database.HeightIndex) { |
| 210 | + db := newDB() |
| 211 | + require.NoError(t, db.Close()) |
| 212 | + |
| 213 | + // Second close should return error |
| 214 | + err := db.Close() |
| 215 | + require.ErrorIs(t, err, database.ErrClosed) |
| 216 | +} |
0 commit comments