Skip to content

Commit

Permalink
redo unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
shijiesheng committed Oct 22, 2024
1 parent 3c14611 commit 9d99ecf
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 25 deletions.
57 changes: 33 additions & 24 deletions tools/cli/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/olekukonko/tablewriter"
"github.com/olivere/elastic"
"github.com/pborman/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/urfave/cli/v2"

Expand All @@ -39,13 +40,22 @@ import (
"github.com/uber/cadence/common/types"
)

type cliAppSuite struct {
suite.Suite
app *cli.App
mockCtrl *gomock.Controller
serverFrontendClient *frontend.MockClient
serverAdminClient *admin.MockClient
}
type (
cliAppSuite struct {
suite.Suite
app *cli.App
mockCtrl *gomock.Controller
serverFrontendClient *frontend.MockClient
serverAdminClient *admin.MockClient
}

testcase struct {
name string
command string
err string
mock func()
}
)

var _ ClientFactory = (*clientFactoryMock)(nil)

Expand Down Expand Up @@ -105,6 +115,22 @@ func (s *cliAppSuite) TearDownTest() {
s.mockCtrl.Finish() // assert mock’s expectations
}

func (s *cliAppSuite) testcaseHelper(testcases []testcase) {
for _, tt := range testcases {
s.T().Run(tt.name, func(t *testing.T) {
if tt.mock != nil {
tt.mock()
}
err := s.app.Run(strings.Split(tt.command, " "))
if tt.err != "" {
assert.ErrorContains(t, err, tt.err)
} else {
assert.NoError(t, err)
}
})
}
}

func (s *cliAppSuite) TestAppCommands() {
for _, test := range commands {
cmd := s.app.Command(test)
Expand Down Expand Up @@ -417,15 +443,6 @@ func (s *cliAppSuite) TestSignalWorkflow_Failed() {
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "signal", "-w", "wid", "-n", "signal-name"}))
}

func (s *cliAppSuite) TestQueryWorkflow() {
resp := &types.QueryWorkflowResponse{
QueryResult: []byte("query-result"),
}
s.serverFrontendClient.EXPECT().QueryWorkflow(gomock.Any(), gomock.Any()).Return(resp, nil)
err := s.app.Run([]string{"", "--do", domainName, "workflow", "query", "-w", "wid", "-qt", "query-type-test"})
s.Nil(err)
}

func (s *cliAppSuite) TestQueryWorkflowUsingStackTrace() {
resp := &types.QueryWorkflowResponse{
QueryResult: []byte("query-result"),
Expand All @@ -435,14 +452,6 @@ func (s *cliAppSuite) TestQueryWorkflowUsingStackTrace() {
s.Nil(err)
}

func (s *cliAppSuite) TestQueryWorkflow_Failed() {
resp := &types.QueryWorkflowResponse{
QueryResult: []byte("query-result"),
}
s.serverFrontendClient.EXPECT().QueryWorkflow(gomock.Any(), gomock.Any()).Return(resp, &types.BadRequestError{"faked error"})
s.Error(s.app.Run([]string{"", "--do", domainName, "workflow", "query", "-w", "wid", "-qt", "query-type-test"}))
}

var (
closeStatus = types.WorkflowExecutionCloseStatusCompleted

Expand Down
25 changes: 25 additions & 0 deletions tools/cli/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,31 @@ import (
"github.com/uber/cadence/common/types"
)

func Test_JSONHistorySerializer(t *testing.T) {
serializer := JSONHistorySerializer{}
h := &types.History{
Events: []*types.HistoryEvent{
{
ID: 1,
Version: 1,
EventType: types.EventTypeWorkflowExecutionStarted.Ptr(),
},
{
ID: 2,
Version: 1,
EventType: types.EventTypeDecisionTaskScheduled.Ptr(),
},
},
}
data, err := serializer.Serialize(h)
assert.NoError(t, err)
assert.NotNil(t, data)

h2, err := serializer.Deserialize(data)
assert.NoError(t, err)
assert.Equal(t, h, h2)
}

func Test_ParseIntMultiRange(t *testing.T) {
tests := []struct {
name string
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/workflow_commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,7 @@ func describeWorkflowHelper(c *cli.Context, wid, rid string) error {
} else {
o, err = convertDescribeWorkflowExecutionResponse(resp, frontendClient, c)
if err != nil {
return commoncli.Problem("WF helper describe failed: ", err)
return commoncli.Problem("convert describe workflow response failed: ", err)
}
}

Expand Down
Loading

0 comments on commit 9d99ecf

Please sign in to comment.