Skip to content
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

Merged
merged 31 commits into from
Dec 7, 2024
Merged
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
6bf5fbd
refact(math/):refact ApproxRoot for readality
xujiangyu Oct 15, 2024
687b732
Merge branch 'cosmos:main' into main
xujiangyu Oct 15, 2024
32d9110
Merge branch 'cosmos:main' into main
xujiangyu Oct 16, 2024
6097cc0
Merge branch 'cosmos:main' into main
xujiangyu Oct 24, 2024
3ff753f
test:(math/dec): add test case for ApproxRoot()
heren-ke Oct 24, 2024
c825214
Merge branch 'main' into main
xujiangyu Oct 25, 2024
fa71970
Merge branch 'cosmos:main' into main
xujiangyu Oct 25, 2024
3851378
Merge branch 'main' into main
xujiangyu Oct 26, 2024
a2955ef
Merge branch 'main' into main
xujiangyu Oct 28, 2024
ccf8c42
Merge branch 'main' into main
xujiangyu Oct 28, 2024
0cc52f2
Merge branch 'cosmos:main' into main
xujiangyu Oct 28, 2024
e854dbc
Merge branch 'cosmos:main' into main
xujiangyu Oct 29, 2024
c2d1698
Merge branch 'cosmos:main' into main
xujiangyu Oct 29, 2024
18c263c
test: fix test case for ApproxRoot
heren-ke Oct 30, 2024
075ebbb
Merge branch 'cosmos:main' into main
xujiangyu Oct 31, 2024
d152a6b
Merge branch 'cosmos:main' into main
xujiangyu Nov 7, 2024
402149b
Merge branch 'cosmos:main' into main
xujiangyu Nov 19, 2024
1c569e2
Merge branch 'cosmos:main' into main
xujiangyu Nov 21, 2024
8917ceb
Merge branch 'cosmos:main' into main
xujiangyu Nov 22, 2024
a51ebf6
Merge branch 'cosmos:main' into main
xujiangyu Dec 3, 2024
42c75b6
Merge branch 'cosmos:main' into main
xujiangyu Dec 4, 2024
86e7052
test: add test cases for prefixdb.go
heren-ke Dec 4, 2024
579aebc
Merge branch 'cosmos:main' into main
xujiangyu Dec 4, 2024
39f67a7
Merge branch 'cosmos:main' into main
xujiangyu Dec 5, 2024
8b6f1ca
Merge branch 'cosmos:main' into main
xujiangyu Dec 5, 2024
b88d678
Merge branch 'main' into main
xujiangyu Dec 5, 2024
aa87378
Merge branch 'cosmos:main' into main
xujiangyu Dec 6, 2024
d83d4f8
Merge branch 'main' into main
xujiangyu Dec 6, 2024
7026bc9
Merge branch 'cosmos:main' into main
xujiangyu Dec 6, 2024
e2cae82
format prefixdb_test.go
heren-ke Dec 6, 2024
245dafa
Merge branch 'main' into main
xujiangyu Dec 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions store/db/prefixdb_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package db_test

import (
"testing"

"cosmossdk.io/store/db"
"cosmossdk.io/store/mock"
"github.com/stretchr/testify/require"
"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
Copy link
Contributor

Choose a reason for hiding this comment

The 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:

  1. Error scenarios from the underlying DB
  2. Edge cases with nil/empty values
  3. Concurrent access scenarios
  4. Large key/value pairs

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)
}
Comment on lines +33 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codebase verification

⚠️ Potential issue

Missing critical test coverage for interface methods

The test file lacks coverage for several required interface methods:

  • Iterator and ReverseIterator methods from KVStore interface
  • Get and Set methods from BasicKVStore interface
  • Batch operations and Close method from KVStoreWithBatch interface

The current test file only covers Has and Delete operations, which represents a small subset of the required functionality. This limited test coverage could lead to undiscovered bugs in the prefix database implementation.

🔗 Analysis chain

Add tests for missing interface methods

The test file is missing coverage for several critical interface methods:

  1. Iterator and ReverseIterator methods
  2. Batch operations (KVStoreWithBatch interface)
  3. Error scenarios for Has/Delete operations

Let's verify the required interface methods:


Let me gather more information about the existing test coverage:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check interface methods that need test coverage
ast-grep --pattern 'type KVStore interface {
  $$$
}'

# Check batch interface methods
ast-grep --pattern 'type KVStoreWithBatch interface {
  $$$
}'

Length of output: 1780


Script:

#!/bin/bash
# Check existing test coverage for Iterator and ReverseIterator
rg -A 5 "func Test.*Iterator" store/db/prefixdb_test.go

# Check for batch operations tests
rg -A 5 "func Test.*Batch" store/db/prefixdb_test.go

# Check BasicKVStore interface to understand all required methods
ast-grep --pattern 'type BasicKVStore interface {
  $$$
}'

Length of output: 832

Loading