@@ -8,6 +8,10 @@ import (
88 "strings"
99 "testing"
1010
11+ "github.com/golang/mock/gomock"
12+
13+ "github.com/cosmos/cosmos-sdk/orm/testing/ormmocks"
14+
1115 "google.golang.org/protobuf/reflect/protoreflect"
1216 "gotest.tools/v3/assert"
1317 "gotest.tools/v3/golden"
@@ -34,6 +38,19 @@ type keeper struct {
3438 store testpb.BankStore
3539}
3640
41+ func NewKeeper (db ormdb.ModuleDB ) (Keeper , error ) {
42+ store , err := testpb .NewBankStore (db )
43+ return keeper {store }, err
44+ }
45+
46+ type Keeper interface {
47+ Send (ctx context.Context , from , to , denom string , amount uint64 ) error
48+ Mint (ctx context.Context , acct , denom string , amount uint64 ) error
49+ Burn (ctx context.Context , acct , denom string , amount uint64 ) error
50+ Balance (ctx context.Context , acct , denom string ) (uint64 , error )
51+ Supply (ctx context.Context , denom string ) (uint64 , error )
52+ }
53+
3754func (k keeper ) Send (ctx context.Context , from , to , denom string , amount uint64 ) error {
3855 err := k .safeSubBalance (ctx , from , denom , amount )
3956 if err != nil {
@@ -151,11 +168,6 @@ func (k keeper) safeSubBalance(ctx context.Context, acct, denom string, amount u
151168 }
152169}
153170
154- func newKeeper (db ormdb.ModuleDB ) (keeper , error ) {
155- store , err := testpb .NewBankStore (db )
156- return keeper {store }, err
157- }
158-
159171func TestModuleDB (t * testing.T ) {
160172 // create db & debug context
161173 db , err := ormdb .NewModuleDB (TestBankSchema , ormdb.ModuleDBOptions {})
@@ -171,7 +183,7 @@ func TestModuleDB(t *testing.T) {
171183 ))
172184
173185 // create keeper
174- k , err := newKeeper (db )
186+ k , err := NewKeeper (db )
175187 assert .NilError (t , err )
176188
177189 // mint coins
@@ -250,3 +262,39 @@ func TestModuleDB(t *testing.T) {
250262 assert .NilError (t , db .ImportJSON (ctx2 , source ))
251263 testkv .AssertBackendsEqual (t , backend , backend2 )
252264}
265+
266+ func TestHooks (t * testing.T ) {
267+ ctrl := gomock .NewController (t )
268+ db , err := ormdb .NewModuleDB (TestBankSchema , ormdb.ModuleDBOptions {})
269+ assert .NilError (t , err )
270+ hooks := ormmocks .NewMockHooks (ctrl )
271+ ctx := ormtable .WrapContextDefault (ormtest .NewMemoryBackend ().WithHooks (hooks ))
272+ k , err := NewKeeper (db )
273+ assert .NilError (t , err )
274+
275+ denom := "foo"
276+ acct1 := "bob"
277+ acct2 := "sally"
278+
279+ hooks .EXPECT ().OnInsert (ormmocks .Eq (& testpb.Balance {Address : acct1 , Denom : denom , Amount : 10 }))
280+ hooks .EXPECT ().OnInsert (ormmocks .Eq (& testpb.Supply {Denom : denom , Amount : 10 }))
281+ assert .NilError (t , k .Mint (ctx , acct1 , denom , 10 ))
282+
283+ hooks .EXPECT ().OnUpdate (
284+ ormmocks .Eq (& testpb.Balance {Address : acct1 , Denom : denom , Amount : 10 }),
285+ ormmocks .Eq (& testpb.Balance {Address : acct1 , Denom : denom , Amount : 5 }),
286+ )
287+ hooks .EXPECT ().OnInsert (
288+ ormmocks .Eq (& testpb.Balance {Address : acct2 , Denom : denom , Amount : 5 }),
289+ )
290+ assert .NilError (t , k .Send (ctx , acct1 , acct2 , denom , 5 ))
291+
292+ hooks .EXPECT ().OnUpdate (
293+ ormmocks .Eq (& testpb.Supply {Denom : denom , Amount : 10 }),
294+ ormmocks .Eq (& testpb.Supply {Denom : denom , Amount : 5 }),
295+ )
296+ hooks .EXPECT ().OnDelete (
297+ ormmocks .Eq (& testpb.Balance {Address : acct1 , Denom : denom , Amount : 5 }),
298+ )
299+ assert .NilError (t , k .Burn (ctx , acct1 , denom , 5 ))
300+ }
0 commit comments