Skip to content

Commit

Permalink
update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentysc committed Feb 2, 2024
1 parent cb72c4d commit 27409a5
Showing 1 changed file with 112 additions and 28 deletions.
140 changes: 112 additions & 28 deletions projection/proposal/proposal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/stretchr/testify/assert"
testify_mock "github.com/stretchr/testify/mock"

"github.com/crypto-com/chain-indexing/appinterface/cosmosapp"
"github.com/crypto-com/chain-indexing/appinterface/projection/rdbparambase"
rdbparambase_types "github.com/crypto-com/chain-indexing/appinterface/projection/rdbparambase/types"
rdbparambase_view "github.com/crypto-com/chain-indexing/appinterface/projection/rdbparambase/view"
Expand All @@ -31,12 +32,12 @@ import (
model_gov_v1 "github.com/crypto-com/chain-indexing/usecase/model/gov/v1"
)

func NewProposalProjection(rdbConn rdb.Conn) *proposal.Proposal {
func NewProposalProjection(rdbConn rdb.Conn, client cosmosapp.Client) *proposal.Proposal {

return proposal.NewProposal(
nil,
rdbConn,
nil,
client,
"",
nil,
)
Expand Down Expand Up @@ -69,7 +70,7 @@ func TestProposal_HandleEvents(t *testing.T) {
testCases := []struct {
Name string
Events []entity_event.Event
MockFunc func(events []entity_event.Event) []*testify_mock.Mock
MockFunc func(events []entity_event.Event, mockClient *cosmosapp.MockClient) []*testify_mock.Mock
}{
{
Name: "HandleMsgSubmitTextProposal",
Expand Down Expand Up @@ -99,7 +100,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -239,7 +240,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -389,7 +390,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -538,7 +539,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -683,7 +684,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -825,7 +826,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -974,7 +975,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -1097,7 +1098,7 @@ func TestProposal_HandleEvents(t *testing.T) {
ProposalId: "ProposalId",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand All @@ -1120,6 +1121,82 @@ func TestProposal_HandleEvents(t *testing.T) {

mockParamsView := &rdbparambase_view.MockParamsView{}
mocks = append(mocks, &mockParamsView.Mock)
mockClient.
On("ProposalById", "ProposalId").
Return(cosmosapp.Proposal{
ProposalID: "ProposalId",
VotingEndTime: "1970-01-01T00:00:00.000000001Z",
}, nil)

proposal.ParamBaseGetView = func(
_ *rdbparambase.Base,
_ *rdb.Handle,
) rdbparambase_view.Params {
return mockParamsView
}

proposal.ParamBaseGetView = func(
_ *rdbparambase.Base,
_ *rdb.Handle,
) rdbparambase_view.Params {
return mockParamsView
}

maybeVotingEndTime := utctime.UTCTime{}.Add(time.Duration(1))
mockProposalsView.
On("Update", &view.ProposalRow{
ProposalId: "ProposalId",
Status: "VOTING_PERIOD",
MaybeVotingStartTime: &utctime.UTCTime{},
MaybeVotingEndTime: &maybeVotingEndTime,
}).
Return(nil)

return mocks
},
},
{
Name: "HandleProposalVotingPeriodStarted with default voting end time",
Events: []entity_event.Event{
&usecase_event.ProposalVotingPeriodStarted{
Base: entity_event.NewBase(entity_event.BaseParams{
Name: usecase_event.PROPOSAL_VOTING_PERIOD_STARTED,
Version: 1,
BlockHeight: 1,
}),
ProposalId: "ProposalId",
},
},
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
mockProposalsView.
On("FindById", "ProposalId").
Return(&view.ProposalWithMonikerRow{
ProposalRow: view.ProposalRow{
ProposalId: "ProposalId",
Status: "Status",
MaybeVotingStartTime: nil,
MaybeVotingEndTime: nil,
},
}, nil)

proposal.NewProposals = func(
_ *rdb.Handle,
) view.Proposals {
return mockProposalsView
}

mockParamsView := &rdbparambase_view.MockParamsView{}
mocks = append(mocks, &mockParamsView.Mock)
mockClient.
On("ProposalById", "ProposalId").
Return(cosmosapp.Proposal{
ProposalID: "ProposalId",
VotingEndTime: "",
}, nil)

mockParamsView.
On("FindDurationBy", rdbparambase_types.ParamAccessor{
Module: "gov",
Expand All @@ -1134,6 +1211,13 @@ func TestProposal_HandleEvents(t *testing.T) {
return mockParamsView
}

proposal.ParamBaseGetView = func(
_ *rdbparambase.Base,
_ *rdb.Handle,
) rdbparambase_view.Params {
return mockParamsView
}

maybeVotingEndTime := utctime.UTCTime{}.Add(time.Duration(1))
mockProposalsView.
On("Update", &view.ProposalRow{
Expand All @@ -1159,7 +1243,7 @@ func TestProposal_HandleEvents(t *testing.T) {
ProposalId: "ProposalId",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand Down Expand Up @@ -1200,7 +1284,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Result: "proposal_passed",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand Down Expand Up @@ -1242,7 +1326,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Result: "proposal_failed",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand Down Expand Up @@ -1284,7 +1368,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Result: "proposal_rejected",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand Down Expand Up @@ -1334,7 +1418,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
Expand Down Expand Up @@ -1436,7 +1520,7 @@ func TestProposal_HandleEvents(t *testing.T) {
},
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {
mockProposalsView := &view.MockProposalsView{}
mocks = append(mocks, &mockProposalsView.Mock)
mockProposalsView.
Expand Down Expand Up @@ -1535,7 +1619,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Option: "Option",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -1637,7 +1721,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Option: "Option",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -1734,7 +1818,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -1839,7 +1923,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -1958,7 +2042,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -2075,7 +2159,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -2217,7 +2301,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -2380,7 +2464,7 @@ func TestProposal_HandleEvents(t *testing.T) {
Metadata: "Metadata",
},
},
MockFunc: func(events []entity_event.Event) (mocks []*testify_mock.Mock) {
MockFunc: func(events []entity_event.Event, mockClient *cosmosapp.MockClient) (mocks []*testify_mock.Mock) {

mockValidatorsView := &rdbvalidatorbase_view.MockValidatorsView{}
mocks = append(mocks, &mockValidatorsView.Mock)
Expand Down Expand Up @@ -2501,8 +2585,8 @@ func TestProposal_HandleEvents(t *testing.T) {
mockRDbConn := NewMockRDbConn()
mockTx := NewMockRDbTx()
mockRDbConn.On("Begin").Return(mockTx, nil)

mocks := tc.MockFunc(tc.Events)
mockClient := &cosmosapp.MockClient{}
mocks := tc.MockFunc(tc.Events, mockClient)
mocks = append(mocks, &mockRDbConn.Mock)
mocks = append(mocks, &mockTx.Mock)

Expand Down Expand Up @@ -2532,7 +2616,7 @@ func TestProposal_HandleEvents(t *testing.T) {
return nil
}

projection := NewProposalProjection(mockRDbConn)
projection := NewProposalProjection(mockRDbConn, mockClient)
err := projection.HandleEvents(1, tc.Events)
assert.NoError(t, err)

Expand Down

0 comments on commit 27409a5

Please sign in to comment.