Skip to content

Commit

Permalink
Add test coverage for new domain methods
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed Jan 16, 2025
1 parent 70679b4 commit 3bb8b67
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 5 deletions.
5 changes: 1 addition & 4 deletions core/go/internal/domainmgr/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -823,10 +823,7 @@ func (d *domain) GetStates(ctx context.Context, req *prototk.GetStatesRequest) (
}

_, states, err := c.dCtx.GetStates(c.dbTX, schemaID, req.StateIds)
if err != nil {
return nil, err
}
return &prototk.GetStatesResponse{
States: toProtoStates(states),
}, nil
}, err
}
49 changes: 49 additions & 0 deletions core/go/internal/domainmgr/domain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,6 +1022,55 @@ func TestRecoverSignerFailCases(t *testing.T) {
assert.Regexp(t, "PD011638", err)
}

func TestSendTransactionFailCases(t *testing.T) {
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas())
defer done()

_, err := td.d.SendTransaction(td.ctx, &prototk.SendTransactionRequest{
Transaction: &prototk.TransactionInput{
ContractAddress: "badnotgood",
FunctionAbiJson: `{}`,
ParamsJson: `{}`,
},
})
require.ErrorContains(t, err, "bad address")

_, err = td.d.SendTransaction(td.ctx, &prototk.SendTransactionRequest{
Transaction: &prototk.TransactionInput{
ContractAddress: "0x05d936207F04D81a85881b72A0D17854Ee8BE45A",
FunctionAbiJson: `bad`,
ParamsJson: `{}`,
},
})
require.ErrorContains(t, err, "invalid character")
}

func TestGetStatesFailCases(t *testing.T) {
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas())
defer done()

_, err := td.d.GetStates(td.ctx, &prototk.GetStatesRequest{
StateQueryContext: "bad",
})
require.ErrorContains(t, err, "PD011649")

_, err = td.d.GetStates(td.ctx, &prototk.GetStatesRequest{
StateQueryContext: td.c.id,
SchemaId: "bad",
})
require.ErrorContains(t, err, "PD011641")

schemaID := tktypes.RandBytes32()
td.mdc.On("GetStates", mock.Anything, schemaID, []string{"id1"}).Return(nil, nil, fmt.Errorf("pop"))

_, err = td.d.GetStates(td.ctx, &prototk.GetStatesRequest{
StateQueryContext: td.c.id,
SchemaId: schemaID.String(),
StateIds: []string{"id1"},
})
require.EqualError(t, err, "pop")
}

func TestMapStateLockType(t *testing.T) {
for _, pldType := range pldapi.StateLockType("").Options() {
assert.NotNil(t, mapStateLockType(pldapi.StateLockType(pldType)))
Expand Down
45 changes: 45 additions & 0 deletions core/go/internal/domainmgr/private_smart_contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,51 @@ func TestRecoverSignature(t *testing.T) {
assert.Equal(t, kp.Address.String(), res.Verifier)
}

func TestSendTransaction(t *testing.T) {
txID := uuid.New()
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas(), func(mc *mockComponents) {
mc.txManager.On("SendTransaction", mock.Anything, mock.Anything).Return(&txID, nil)
})
defer done()
assert.Nil(t, td.d.initError.Load())

_, err := td.d.SendTransaction(td.ctx, &prototk.SendTransactionRequest{
Transaction: &prototk.TransactionInput{
ContractAddress: "0x05d936207F04D81a85881b72A0D17854Ee8BE45A",
FunctionAbiJson: `{}`,
ParamsJson: `{}`,
},
})
require.NoError(t, err)
}

func TestSendTransactionFail(t *testing.T) {
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas(), func(mc *mockComponents) {
mc.txManager.On("SendTransaction", mock.Anything, mock.Anything).Return(nil, fmt.Errorf("pop"))
})
defer done()
assert.Nil(t, td.d.initError.Load())

_, err := td.d.SendTransaction(td.ctx, &prototk.SendTransactionRequest{
Transaction: &prototk.TransactionInput{
ContractAddress: "0x05d936207F04D81a85881b72A0D17854Ee8BE45A",
FunctionAbiJson: `{}`,
ParamsJson: `{}`,
},
})
require.EqualError(t, err, "pop")
}

func TestLocalNodeName(t *testing.T) {
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas())
defer done()
assert.Nil(t, td.d.initError.Load())

res, err := td.d.LocalNodeName(td.ctx, &prototk.LocalNodeNameRequest{})
require.NoError(t, err)
assert.Equal(t, "node1", res.Name)
}

func TestDomainInitTransactionMissingInput(t *testing.T) {
td, done := newTestDomain(t, false, goodDomainConf(), mockSchemas())
defer done()
Expand Down
41 changes: 40 additions & 1 deletion core/go/internal/plugins/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type testDomainManager struct {
recoverSigner func(context.Context, *prototk.RecoverSignerRequest) (*prototk.RecoverSignerResponse, error)
sendTransaction func(context.Context, *prototk.SendTransactionRequest) (*prototk.SendTransactionResponse, error)
localNodeName func(context.Context, *prototk.LocalNodeNameRequest) (*prototk.LocalNodeNameResponse, error)
getStates func(context.Context, *prototk.GetStatesRequest) (*prototk.GetStatesResponse, error)
}

func (tp *testDomainManager) FindAvailableStates(ctx context.Context, req *prototk.FindAvailableStatesRequest) (*prototk.FindAvailableStatesResponse, error) {
Expand Down Expand Up @@ -73,7 +74,7 @@ func (tp *testDomainManager) LocalNodeName(ctx context.Context, req *prototk.Loc
}

func (tp *testDomainManager) GetStates(ctx context.Context, req *prototk.GetStatesRequest) (*prototk.GetStatesResponse, error) {
return nil, nil
return tp.getStates(ctx, req)
}

func domainConnectFactory(ctx context.Context, client prototk.PluginControllerClient) (grpc.BidiStreamingClient[prototk.DomainMessage, prototk.DomainMessage], error) {
Expand Down Expand Up @@ -289,6 +290,26 @@ func TestDomainRequestsOK(t *testing.T) {
}, nil
}

tdm.sendTransaction = func(ctx context.Context, str *prototk.SendTransactionRequest) (*prototk.SendTransactionResponse, error) {
assert.Equal(t, str.Transaction.From, "user1")
return &prototk.SendTransactionResponse{
Id: "tx1",
}, nil
}

tdm.localNodeName = func(ctx context.Context, lnr *prototk.LocalNodeNameRequest) (*prototk.LocalNodeNameResponse, error) {
return &prototk.LocalNodeNameResponse{
Name: "node1",
}, nil
}

tdm.getStates = func(ctx context.Context, gsr *prototk.GetStatesRequest) (*prototk.GetStatesResponse, error) {
assert.Equal(t, "schema1", gsr.SchemaId)
return &prototk.GetStatesResponse{
States: []*prototk.StoredState{{}},
}, nil
}

ctx, pc, done := newTestDomainPluginManager(t, &testManagers{
testDomainManager: tdm,
})
Expand Down Expand Up @@ -431,6 +452,24 @@ func TestDomainRequestsOK(t *testing.T) {
})
require.NoError(t, err)
assert.Equal(t, "some verifier", string(rsr.Verifier))

str, err := callbacks.SendTransaction(ctx, &prototk.SendTransactionRequest{
Transaction: &prototk.TransactionInput{
From: "user1",
},
})
require.NoError(t, err)
assert.Equal(t, "tx1", str.Id)

lnr, err := callbacks.LocalNodeName(ctx, &prototk.LocalNodeNameRequest{})
require.NoError(t, err)
assert.Equal(t, "node1", lnr.Name)

gsr, err := callbacks.GetStates(ctx, &prototk.GetStatesRequest{
SchemaId: "schema1",
})
require.NoError(t, err)
assert.Len(t, gsr.States, 1)
}

func TestDomainRegisterFail(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions core/go/internal/statemgr/domain_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,12 @@ func TestStateContextMintSpendMint(t *testing.T) {
// Flush the states to the database
syncFlushContext(t, dc)

// Query state by ID
_, statesByID, err := dc.GetStates(ss.p.DB(), schemaID, []string{states[0].ID.String()})
require.NoError(t, err)
assert.Len(t, statesByID, 1)
assert.Equal(t, int64(50), parseFakeCoin(t, statesByID[0]).Amount.Int64())

// Check the DB persisted state is what we expect
_, states, err = dc.FindAvailableStates(ss.p.DB(), schemaID, query.NewQueryBuilder().Sort("owner", "amount").Query())
require.NoError(t, err)
Expand Down

0 comments on commit 3bb8b67

Please sign in to comment.