-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add common module, adjust the paper data structure to optional instead of required * fix: Use paradigms to reduce logic duplication * test: add tests in rpc,common_service,common_service_handler * feat: add error handle in common service * fix: add util.HandleBaseRespWithCookie in common rpc * fix: delete url service * fix: Adjust the go mod file to pass the lint test * fix: ci * fix: Modified to pass CI test --------- Co-authored-by: jiuxia211 <[email protected]>
- Loading branch information
Showing
30 changed files
with
13,822 additions
and
7,281 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,210 @@ | ||
/* | ||
Copyright 2024 The west2-online Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package api | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
"github.com/bytedance/mockey" | ||
"github.com/bytedance/sonic" | ||
"github.com/cloudwego/hertz/pkg/common/config" | ||
"github.com/cloudwego/hertz/pkg/common/ut" | ||
"github.com/cloudwego/hertz/pkg/protocol/consts" | ||
"github.com/cloudwego/hertz/pkg/route" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"github.com/west2-online/fzuhelper-server/api/rpc" | ||
"github.com/west2-online/fzuhelper-server/kitex_gen/common" | ||
"github.com/west2-online/fzuhelper-server/kitex_gen/model" | ||
) | ||
|
||
func TestGetTerm(t *testing.T) { | ||
f := func(str string) *string { | ||
return &str | ||
} | ||
|
||
type TestCase struct { | ||
Name string | ||
expectedError bool | ||
expectedErrorInfo error | ||
expectedResult string | ||
expectedTermInfo *model.TermInfo | ||
url string | ||
} | ||
|
||
expectedTermInfo := &model.TermInfo{ | ||
TermId: f("201501"), | ||
Term: f("201501"), | ||
SchoolYear: f("2015"), | ||
Events: []*model.TermEvent{ | ||
{ | ||
Name: f("学生注册"), | ||
StartDate: f("2015-08-29"), | ||
EndDate: f("2015-08-30"), | ||
}, | ||
{ | ||
Name: f("学生补考"), | ||
StartDate: f("2015-08-29"), | ||
EndDate: f("2015-09-06"), | ||
}, | ||
{ | ||
Name: f("正式上课"), | ||
StartDate: f("2015-08-31"), | ||
EndDate: f("2015-08-31"), | ||
}, | ||
{ | ||
Name: f("新生报到"), | ||
StartDate: f("2015-09-07"), | ||
EndDate: f("2015-09-07"), | ||
}, | ||
{ | ||
Name: f("校运会"), | ||
StartDate: f("2015-11-12"), | ||
EndDate: f("2015-11-14"), | ||
}, | ||
{ | ||
Name: f("期末考试"), | ||
StartDate: f("2016-01-16"), | ||
EndDate: f("2016-01-22"), | ||
}, | ||
{ | ||
Name: f("寒假"), | ||
StartDate: f("2016-01-23"), | ||
EndDate: f("2016-02-28"), | ||
}, | ||
}, | ||
} | ||
|
||
data, err := sonic.Marshal(expectedTermInfo) | ||
assert.Nil(t, err) | ||
|
||
testCases := []TestCase{ | ||
{ | ||
Name: "GetTermSuccessfully", | ||
expectedError: false, | ||
expectedErrorInfo: nil, | ||
expectedResult: `{"code":"10000","message":"Success","data":` + string(data) + `}`, | ||
expectedTermInfo: expectedTermInfo, | ||
url: "/api/v1/terms/info?term=201501", | ||
}, | ||
{ | ||
Name: "GetTermError", | ||
expectedError: true, | ||
expectedErrorInfo: errors.New("etTermRPC: RPC called failed"), | ||
expectedResult: `{"code":"50001","message":"etTermRPC: RPC called failed"}`, | ||
expectedTermInfo: nil, | ||
url: "/api/v1/terms/info?term=201501", | ||
}, | ||
{ | ||
Name: "BindAndValidateError", | ||
expectedError: false, | ||
expectedErrorInfo: nil, | ||
expectedResult: `{"code":"20001","message":"参数错误, 'term' field is a 'required' parameter,` + | ||
` but the request body does not have this parameter 'term'"}`, | ||
expectedTermInfo: nil, | ||
url: "/api/v1/terms/info", | ||
}, | ||
} | ||
|
||
router := route.NewEngine(&config.Options{}) | ||
router.GET("/api/v1/terms/info", GetTerm) | ||
|
||
defer mockey.UnPatchAll() | ||
for _, tc := range testCases { | ||
mockey.PatchConvey(tc.Name, t, func() { | ||
mockey.Mock(rpc.GetTermRPC).To(func(ctx context.Context, req *common.TermRequest) (*model.TermInfo, error) { | ||
return tc.expectedTermInfo, tc.expectedErrorInfo | ||
}).Build() | ||
|
||
result := ut.PerformRequest(router, "GET", tc.url, nil) | ||
assert.Equal(t, consts.StatusOK, result.Result().StatusCode()) | ||
assert.Equal(t, tc.expectedResult, string(result.Result().Body())) | ||
}) | ||
} | ||
} | ||
|
||
func TestGetTermsList(t *testing.T) { | ||
f := func(str string) *string { | ||
return &str | ||
} | ||
|
||
type TestCase struct { | ||
Name string | ||
expectedError bool | ||
expectedErrorInfo error | ||
expectedResult string | ||
expectedTermInfo *model.TermList | ||
} | ||
|
||
expectedTermList := &model.TermList{ | ||
CurrentTerm: f("202401"), | ||
Terms: []*model.Term{ | ||
{ | ||
TermId: f("2024012024082620250117"), | ||
SchoolYear: f("2024"), | ||
Term: f("202401"), | ||
StartDate: f("2024-08-26"), | ||
EndDate: f("2025-01-17"), | ||
}, | ||
{ | ||
TermId: f("2024022025022420250704"), | ||
SchoolYear: f("2024"), | ||
Term: f("202402"), | ||
StartDate: f("2025-02-24"), | ||
EndDate: f("2025-07-04"), | ||
}, | ||
}, | ||
} | ||
|
||
data, err := sonic.Marshal(expectedTermList) | ||
assert.Nil(t, err) | ||
|
||
testCases := []TestCase{ | ||
{ | ||
Name: "GetTermsListSuccessfully", | ||
expectedError: false, | ||
expectedErrorInfo: nil, | ||
expectedResult: `{"code":"10000","message":"Success","data":` + string(data) + `}`, | ||
expectedTermInfo: expectedTermList, | ||
}, | ||
{ | ||
Name: "GetTermsListError", | ||
expectedError: true, | ||
expectedErrorInfo: errors.New("etTermRPC: RPC called failed"), | ||
expectedResult: `{"code":"50001","message":"etTermRPC: RPC called failed"}`, | ||
expectedTermInfo: nil, | ||
}, | ||
} | ||
|
||
router := route.NewEngine(&config.Options{}) | ||
router.GET("/api/v1/terms/list", GetTermsList) | ||
|
||
defer mockey.UnPatchAll() | ||
for _, tc := range testCases { | ||
mockey.PatchConvey(tc.Name, t, func() { | ||
mockey.Mock(rpc.GetTermsListRPC).To(func(ctx context.Context, req *common.TermListRequest) (*model.TermList, error) { | ||
return tc.expectedTermInfo, tc.expectedErrorInfo | ||
}).Build() | ||
url := "/api/v1/terms/list" | ||
result := ut.PerformRequest(router, "GET", url, nil) | ||
assert.Equal(t, consts.StatusOK, result.Result().StatusCode()) | ||
assert.Equal(t, tc.expectedResult, string(result.Result().Body())) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.