Skip to content

Commit

Permalink
Cleanup database tests (#2654)
Browse files Browse the repository at this point in the history
  • Loading branch information
dhrubabasu authored Jan 23, 2024
1 parent 13bb7e4 commit 0b344bd
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 89 deletions.
16 changes: 8 additions & 8 deletions database/corruptabledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ import (

var errTest = errors.New("non-nil error")

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
baseDB := memdb.New()
db := New(baseDB)
test(t, db)
}
}

func newDB() *Database {
baseDB := memdb.New()
return New(baseDB)
}

func TestInterface(t *testing.T) {
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
test(t, newDB())
})
}
}

func FuzzKeyValue(f *testing.F) {
database.FuzzKeyValue(f, newDB())
}
Expand Down
14 changes: 8 additions & 6 deletions database/encdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
const testPassword = "lol totally a secure password" //nolint:gosec

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
unencryptedDB := memdb.New()
db, err := New([]byte(testPassword), unencryptedDB)
require.NoError(t, err)

test(t, db)
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
unencryptedDB := memdb.New()
db, err := New([]byte(testPassword), unencryptedDB)
require.NoError(t, err)

test(t, db)
})
}
}

Expand Down
14 changes: 8 additions & 6 deletions database/leveldb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import (
)

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
folder := t.TempDir()
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(t, err)
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
folder := t.TempDir()
db, err := New(folder, nil, logging.NoLog{}, "", prometheus.NewRegistry())
require.NoError(t, err)

test(t, db)
test(t, db)

_ = db.Close()
_ = db.Close()
})
}
}

Expand Down
6 changes: 4 additions & 2 deletions database/memdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ import (
)

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
test(t, New())
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
test(t, New())
})
}
}

Expand Down
14 changes: 8 additions & 6 deletions database/meterdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import (
)

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
baseDB := memdb.New()
db, err := New("", prometheus.NewRegistry(), baseDB)
require.NoError(t, err)

test(t, db)
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
baseDB := memdb.New()
db, err := New("", prometheus.NewRegistry(), baseDB)
require.NoError(t, err)

test(t, db)
})
}
}

Expand Down
10 changes: 6 additions & 4 deletions database/pebble/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ func newDB(t testing.TB) *Database {
}

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
db := newDB(t)
test(t, db)
_ = db.Close()
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
db := newDB(t)
test(t, db)
_ = db.Close()
})
}
}

Expand Down
18 changes: 10 additions & 8 deletions database/prefixdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ import (
)

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
db := memdb.New()
test(t, New([]byte("hello"), db))
test(t, New([]byte("world"), db))
test(t, New([]byte("wor"), New([]byte("ld"), db)))
test(t, New([]byte("ld"), New([]byte("wor"), db)))
test(t, NewNested([]byte("wor"), New([]byte("ld"), db)))
test(t, NewNested([]byte("ld"), New([]byte("wor"), db)))
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
db := memdb.New()
test(t, New([]byte("hello"), db))
test(t, New([]byte("world"), db))
test(t, New([]byte("wor"), New([]byte("ld"), db)))
test(t, New([]byte("ld"), New([]byte("wor"), db)))
test(t, NewNested([]byte("wor"), New([]byte("ld"), db)))
test(t, NewNested([]byte("ld"), New([]byte("wor"), db)))
})
}
}

Expand Down
10 changes: 6 additions & 4 deletions database/rpcdb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ func setupDB(t testing.TB) *testDatabase {
}

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
db := setupDB(t)
test(t, db.client)
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
db := setupDB(t)
test(t, db.client)

db.closeFn()
db.closeFn()
})
}
}

Expand Down
76 changes: 38 additions & 38 deletions database/test_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,44 +24,44 @@ import (
)

// Tests is a list of all database tests
var Tests = []func(t *testing.T, db Database){
TestSimpleKeyValue,
TestOverwriteKeyValue,
TestEmptyKey,
TestKeyEmptyValue,
TestSimpleKeyValueClosed,
TestNewBatchClosed,
TestBatchPut,
TestBatchDelete,
TestBatchReset,
TestBatchReuse,
TestBatchRewrite,
TestBatchReplay,
TestBatchReplayPropagateError,
TestBatchInner,
TestBatchLargeSize,
TestIteratorSnapshot,
TestIterator,
TestIteratorStart,
TestIteratorPrefix,
TestIteratorStartPrefix,
TestIteratorMemorySafety,
TestIteratorClosed,
TestIteratorError,
TestIteratorErrorAfterRelease,
TestCompactNoPanic,
TestMemorySafetyDatabase,
TestMemorySafetyBatch,
TestAtomicClear,
TestClear,
TestAtomicClearPrefix,
TestClearPrefix,
TestModifyValueAfterPut,
TestModifyValueAfterBatchPut,
TestModifyValueAfterBatchPutReplay,
TestConcurrentBatches,
TestManySmallConcurrentKVPairBatches,
TestPutGetEmpty,
var Tests = map[string]func(t *testing.T, db Database){
"SimpleKeyValue": TestSimpleKeyValue,
"OverwriteKeyValue": TestOverwriteKeyValue,
"EmptyKey": TestEmptyKey,
"KeyEmptyValue": TestKeyEmptyValue,
"SimpleKeyValueClosed": TestSimpleKeyValueClosed,
"NewBatchClosed": TestNewBatchClosed,
"BatchPut": TestBatchPut,
"BatchDelete": TestBatchDelete,
"BatchReset": TestBatchReset,
"BatchReuse": TestBatchReuse,
"BatchRewrite": TestBatchRewrite,
"BatchReplay": TestBatchReplay,
"BatchReplayPropagateError": TestBatchReplayPropagateError,
"BatchInner": TestBatchInner,
"BatchLargeSize": TestBatchLargeSize,
"IteratorSnapshot": TestIteratorSnapshot,
"Iterator": TestIterator,
"IteratorStart": TestIteratorStart,
"IteratorPrefix": TestIteratorPrefix,
"IteratorStartPrefix": TestIteratorStartPrefix,
"IteratorMemorySafety": TestIteratorMemorySafety,
"IteratorClosed": TestIteratorClosed,
"IteratorError": TestIteratorError,
"IteratorErrorAfterRelease": TestIteratorErrorAfterRelease,
"CompactNoPanic": TestCompactNoPanic,
"MemorySafetyDatabase": TestMemorySafetyDatabase,
"MemorySafetyBatch": TestMemorySafetyBatch,
"AtomicClear": TestAtomicClear,
"Clear": TestClear,
"AtomicClearPrefix": TestAtomicClearPrefix,
"ClearPrefix": TestClearPrefix,
"ModifyValueAfterPut": TestModifyValueAfterPut,
"ModifyValueAfterBatchPut": TestModifyValueAfterBatchPut,
"ModifyValueAfterBatchPutReplay": TestModifyValueAfterBatchPutReplay,
"ConcurrentBatches": TestConcurrentBatches,
"ManySmallConcurrentKVPairBatches": TestManySmallConcurrentKVPairBatches,
"PutGetEmpty": TestPutGetEmpty,
}

// TestSimpleKeyValue tests to make sure that simple Put + Get + Delete + Has
Expand Down
8 changes: 5 additions & 3 deletions database/versiondb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
)

func TestInterface(t *testing.T) {
for _, test := range database.Tests {
baseDB := memdb.New()
test(t, New(baseDB))
for name, test := range database.Tests {
t.Run(name, func(t *testing.T) {
baseDB := memdb.New()
test(t, New(baseDB))
})
}
}

Expand Down
10 changes: 6 additions & 4 deletions x/merkledb/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,12 @@ func Test_MerkleDB_GetValues_Safety(t *testing.T) {

func Test_MerkleDB_DB_Interface(t *testing.T) {
for _, bf := range validBranchFactors {
for _, test := range database.Tests {
db, err := getBasicDBWithBranchFactor(bf)
require.NoError(t, err)
test(t, db)
for name, test := range database.Tests {
t.Run(fmt.Sprintf("%s_%d", name, bf), func(t *testing.T) {
db, err := getBasicDBWithBranchFactor(bf)
require.NoError(t, err)
test(t, db)
})
}
}
}
Expand Down

0 comments on commit 0b344bd

Please sign in to comment.