-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: add test examples for prefixdb.go #22752
Changes from 23 commits
6bf5fbd
687b732
32d9110
6097cc0
3ff753f
c825214
fa71970
3851378
a2955ef
ccf8c42
0cc52f2
e854dbc
c2d1698
18c263c
075ebbb
d152a6b
402149b
1c569e2
8917ceb
a51ebf6
42c75b6
86e7052
579aebc
39f67a7
8b6f1ca
b88d678
aa87378
d83d4f8
7026bc9
e2cae82
245dafa
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package db_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"cosmossdk.io/store/db" | ||
"cosmossdk.io/store/mock" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestPrefixDB(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
defer mockCtrl.Finish() | ||
|
||
mockDB := mock.NewMockKVStoreWithBatch(mockCtrl) | ||
prefix := []byte("test:") | ||
pdb := db.NewPrefixDB(mockDB, prefix) | ||
|
||
key := []byte("key1") | ||
value := []byte("value1") | ||
mockDB.EXPECT().Set(gomock.Eq(append(prefix, key...)), gomock.Eq(value)).Return(nil) | ||
|
||
err := pdb.Set(key, value) | ||
require.NoError(t, err) | ||
|
||
mockDB.EXPECT().Get(gomock.Eq(append(prefix, key...))).Return(value, nil) | ||
|
||
returnedValue, err := pdb.Get(key) | ||
require.NoError(t, err) | ||
require.Equal(t, value, returnedValue) | ||
|
||
Comment on lines
+20
to
+32
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Add test cases for error scenarios and edge cases The current test coverage for Set/Get operations could be enhanced by adding:
Example test cases to add: // Test error propagation
mockDB.EXPECT().Set(gomock.Any(), gomock.Any()).Return(errors.New("db error"))
err := pdb.Set(key, value)
require.Error(t, err)
// Test nil value
mockDB.EXPECT().Set(gomock.Any(), nil).Return(nil)
err = pdb.Set(key, nil)
require.NoError(t, err) |
||
mockDB.EXPECT().Has(gomock.Eq(append(prefix, key...))).Return(true, nil) | ||
|
||
has, err := pdb.Has(key) | ||
require.NoError(t, err) | ||
require.True(t, has) | ||
|
||
mockDB.EXPECT().Delete(gomock.Eq(append(prefix, key...))).Return(nil) | ||
|
||
err = pdb.Delete(key) | ||
require.NoError(t, err) | ||
|
||
mockDB.EXPECT().Has(gomock.Eq(append(prefix, key...))).Return(false, nil) | ||
|
||
has, err = pdb.Has(key) | ||
require.NoError(t, err) | ||
require.False(t, has) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Enhance Has/Delete operations test coverage The test coverage for Has/Delete operations should include:
Example test cases to add: // Test Has error propagation
mockDB.EXPECT().Has(gomock.Any()).Return(false, errors.New("db error"))
_, err = pdb.Has(key)
require.Error(t, err)
// Test Delete non-existent key
mockDB.EXPECT().Delete(gomock.Any()).Return(nil)
err = pdb.Delete([]byte("non-existent-key"))
require.NoError(t, err) 🧰 Tools🪛 golangci-lint (1.62.2)49-49: File is not (gci) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Test coverage needs significant improvement The current test file is missing critical test coverage for required interface methods:
Required test additions:
🔗 Analysis chainVerify test coverage with additional scenarios While the test provides good basic coverage, consider adding:
Run the following script to check test coverage: Let me gather more information about the interface methods and implementations to provide a comprehensive assessment. 🏁 Scripts executedThe following scripts were executed for the analysis: Script: #!/bin/bash
# Description: Check test coverage for prefixdb package
# Search for PrefixDB methods that might need testing
ast-grep --pattern 'type PrefixDB struct {
$$$
}'
# Search for interface methods that should be tested
ast-grep --pattern 'type DB interface {
$$$
}'
Length of output: 547 Script: #!/bin/bash
# Search for all methods implemented by PrefixDB
ast-grep --pattern 'func (p *PrefixDB) $_($$$) $$$'
# Search for KVStoreWithBatch interface definition
ast-grep --pattern 'type KVStoreWithBatch interface {
$$$
}'
# Search for any existing iterator or batch related tests
rg -l "Iterator|Batch" store/db/prefixdb_test.go
Length of output: 500 Script: #!/bin/bash
# Let's check the KVStore interface methods
ast-grep --pattern 'type KVStore interface {
$$$
}'
# Check PrefixDB implementation for iterator and batch methods
rg -A 5 "func \(p \*PrefixDB\)" store/db/prefixdb.go
# Check if there are any other test files for PrefixDB
fd prefixdb.*test
Length of output: 1513 🧰 Tools🪛 golangci-lint (1.62.2)6-6: File is not (gci) 9-9: File is not (gci) 49-49: File is not (gci) 8-8: File is not (gofumpt) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you make lint-fix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, it was done with
go fmt