Skip to content

Commit

Permalink
fix: Unit test template bug
Browse files Browse the repository at this point in the history
--bug=1
  • Loading branch information
studyzy authored and kevwan committed Nov 8, 2024
1 parent 7b631c7 commit 057c48f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
1 change: 1 addition & 0 deletions tools/goctl/api/gogen/genlogictest.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ func genLogicTestByRoute(dir, rootPkg string, cfg *config.Config, group spec.Gro
"returnString": returnString,
"request": requestString,
"hasRequest": len(requestType) > 0,
"hasResponse": len(route.ResponseTypeName()) > 0,
"requestType": requestType,
"hasDoc": len(route.JoinedDoc()) > 0,
"doc": getDoc(route.JoinedDoc()),
Expand Down
9 changes: 5 additions & 4 deletions tools/goctl/api/gogen/handler_test.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ func Test{{.HandlerName}}(t *testing.T) {
// new service context
c := config.Config{}
svcCtx := svc.NewServiceContext(c)
// init mock service context here

tests := []struct {
name string
Expand All @@ -29,7 +30,7 @@ func Test{{.HandlerName}}(t *testing.T) {
name: "invalid request body",
reqBody: "invalid",
wantStatus: http.StatusBadRequest,
wantResp: `{"code":400,"msg":"invalid request"}`, // Adjust based on actual error response
wantResp: "unsupported type", // Adjust based on actual error response
setupMocks: func() {
// No setup needed for this test case
},
Expand All @@ -39,8 +40,8 @@ func Test{{.HandlerName}}(t *testing.T) {
{{if .HasRequest}}reqBody: types.{{.RequestType}}{
//TODO: add fields here
},
{{end}}wantStatus: http.StatusUnauthorized,
wantResp: `{"code":401,"msg":"unauthorized"}`, // Adjust based on actual error response
{{end}}wantStatus: http.StatusBadRequest,
wantResp: "error", // Adjust based on actual error response
setupMocks: func() {
// Mock login logic to return an error
},
Expand Down Expand Up @@ -73,7 +74,7 @@ func Test{{.HandlerName}}(t *testing.T) {
handler.ServeHTTP(rr, req)
t.Log(rr.Body.String())
assert.Equal(t, tt.wantStatus, rr.Code)
assert.JSONEq(t, tt.wantResp, rr.Body.String())
assert.Contains(t, rr.Body.String(), tt.wantResp)
})
}
}
19 changes: 9 additions & 10 deletions tools/goctl/api/gogen/logic_test.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -23,29 +23,29 @@ func Test{{.logic}}_{{.function}}(t *testing.T) {
checkResp func{{.responseType}}
}{
{
name: "successful",
name: "response error",
ctx: context.Background(),
setupMocks: func() {
// Mock data for this test case
// mock data for this test case
},
{{if .hasRequest}}req: &{{.requestType}}{
// TODO: init your request here
},{{end}}
wantErr: false,
wantErr: true,
checkResp: func{{.responseType}} {
// TODO: Add your check logic here
},
},
{
name: "response error",
name: "successful",
ctx: context.Background(),
setupMocks: func() {
// mock data for this test case
// Mock data for this test case
},
{{if .hasRequest}}req: &{{.requestType}}{
// TODO: init your request here
},{{end}}
wantErr: true,
wantErr: false,
checkResp: func{{.responseType}} {
// TODO: Add your check logic here
},
Expand All @@ -56,15 +56,14 @@ func Test{{.logic}}_{{.function}}(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
tt.setupMocks()
l := New{{.logic}}(tt.ctx, mockSvcCtx)
resp, err := l.{{.function}}({{if .hasRequest}}tt.req{{end}})
{{if .hasResponse}}resp, {{end}}err := l.{{.function}}({{if .hasRequest}}tt.req{{end}})
if tt.wantErr {
assert.Error(t, err)
assert.Nil(t, resp)
} else {
require.NoError(t, err)
assert.NotNil(t, resp)
tt.checkResp(resp, err)
{{if .hasResponse}}assert.NotNil(t, resp){{end}}
}
tt.checkResp({{if .hasResponse}}resp, {{end}}err)
})
}
}

0 comments on commit 057c48f

Please sign in to comment.