@@ -55,42 +55,28 @@ func TestGetCourseList(t *testing.T) {
5555 type testCase struct {
5656 name string
5757 url string
58- mockResp * course. CourseListResponse
58+ mockResp [] * model. Course
5959 mockErr error
6060 expectContains string
61- expectAbsence string
6261 }
6362
6463 testCases := []testCase {
6564 {
66- name : "success no custom courses (v1 must not emit custom_courses) " ,
65+ name : "success" ,
6766 url : "/api/v1/jwch/course/list?term=202401" ,
68- mockResp : & course.CourseListResponse {Data : []* model.Course {}},
69- expectContains : `"code":"10000","message":"ok","data":[]` ,
70- expectAbsence : `custom_courses` ,
71- },
72- {
73- name : "success with custom courses (v1 still suppresses custom_courses)" ,
74- url : "/api/v1/jwch/course/list?term=202401" ,
75- mockResp : & course.CourseListResponse {
76- Data : []* model.Course {},
77- CustomCourses : []* course.CustomCourseItem {
78- {Name : "x" , Location : "y" , StartClass : 1 , EndClass : 2 , StartWeek : 1 , EndWeek : 2 , Weekday : 1 },
79- },
80- },
81- expectContains : `"code":"10000","message":"ok","data":[]` ,
82- expectAbsence : `custom_courses` ,
67+ mockResp : []* model.Course {},
68+ expectContains : `{"code":"10000","message":"ok","data":[]}` ,
8369 },
8470 {
8571 name : "rpc error" ,
8672 url : "/api/v1/jwch/course/list?term=202401" ,
8773 mockErr : errno .InternalServiceError ,
88- expectContains : `"code":"50001","message":"内部服务错误"` ,
74+ expectContains : `{ "code":"50001","message":"内部服务错误"} ` ,
8975 },
9076 {
9177 name : "bind error" ,
9278 url : "/api/v1/jwch/course/list" ,
93- expectContains : `"code":"20001","message":"参数错误` ,
79+ expectContains : `{ "code":"20001","message":"参数错误, ` ,
9480 },
9581 }
9682
@@ -100,17 +86,13 @@ func TestGetCourseList(t *testing.T) {
10086 defer mockey .UnPatchAll ()
10187 for _ , tc := range testCases {
10288 mockey .PatchConvey (tc .name , t , func () {
103- mockey .Mock (rpc .GetCourseListRPC ).To (func (ctx context.Context , req * course.CourseListRequest ) (* course. CourseListResponse , error ) {
89+ mockey .Mock (rpc .GetCourseListRPC ).To (func (ctx context.Context , req * course.CourseListRequest ) ([] * model. Course , error ) {
10490 return tc .mockResp , tc .mockErr
10591 }).Build ()
10692
10793 res := ut .PerformRequest (router , consts .MethodGet , tc .url , nil )
10894 assert .Equal (t , consts .StatusOK , res .Result ().StatusCode ())
109- body := string (res .Result ().Body ())
110- assert .Contains (t , body , tc .expectContains )
111- if tc .expectAbsence != "" {
112- assert .NotContains (t , body , tc .expectAbsence )
113- }
95+ assert .Contains (t , string (res .Result ().Body ()), tc .expectContains )
11496 })
11597 }
11698}
@@ -127,13 +109,13 @@ func TestGetCourseListV2(t *testing.T) {
127109
128110 testCases := []testCase {
129111 {
130- name : "v2 success no custom courses -> empty array emitted " ,
112+ name : "success" ,
131113 url : "/api/v2/jwch/course/list?term=202401" ,
132114 mockResp : & course.CourseListResponse {Data : []* model.Course {}},
133- expectContains : `"code":"10000","message":"Success ","data":{"base":{"code":10000,"msg":"Success"}," data":[],"custom_courses":[]}` ,
115+ expectContains : `{ "code":"10000","message":"ok ","data":{"base":null," data":[],"custom_courses":[]} }` ,
134116 },
135117 {
136- name : "v2 success with custom courses -> populated array emitted " ,
118+ name : "success" ,
137119 url : "/api/v2/jwch/course/list?term=202401" ,
138120 mockResp : & course.CourseListResponse {
139121 Data : []* model.Course {},
@@ -144,16 +126,15 @@ func TestGetCourseListV2(t *testing.T) {
144126 expectContains : `"custom_courses":[{"name":"x"` ,
145127 },
146128 {
147- name : "v2 rpc error" ,
129+ name : "rpc error" ,
148130 url : "/api/v2/jwch/course/list?term=202401" ,
149131 mockErr : errno .InternalServiceError ,
150- expectContains : `"code":"50001","message":"内部服务错误"` ,
151- expectAbsence : `custom_courses` ,
132+ expectContains : `{"code":"50001","message":"内部服务错误"}` ,
152133 },
153134 {
154- name : "v2 bind error" ,
135+ name : "bind error" ,
155136 url : "/api/v2/jwch/course/list" ,
156- expectContains : `"code":"20001","message":"参数错误` ,
137+ expectContains : `{ "code":"20001","message":"参数错误, ` ,
157138 },
158139 }
159140
@@ -178,6 +159,106 @@ func TestGetCourseListV2(t *testing.T) {
178159 }
179160}
180161
162+ func TestDeleteCustomCourse (t * testing.T ) {
163+ type testCase struct {
164+ name string
165+ url string
166+ body string
167+ mockErr error
168+ expectContains string
169+ }
170+
171+ testCases := []testCase {
172+ {
173+ name : "success" ,
174+ url : "/api/v2/jwch/course/custom" ,
175+ body : `{"term":"202401","course_id":"114514"}` ,
176+ expectContains : `{"code":"10000","message":"ok"}` ,
177+ },
178+ {
179+ name : "rpc error" ,
180+ url : "/api/v2/jwch/course/custom" ,
181+ body : `{"term":"202401","course_id":"114514"}` ,
182+ mockErr : errno .InternalServiceError ,
183+ expectContains : `{"code":"50001","message":"内部服务错误"}` ,
184+ },
185+ }
186+
187+ router := route .NewEngine (& config.Options {})
188+ router .DELETE ("/api/v2/jwch/course/custom" , DeleteCustomCourse )
189+
190+ defer mockey .UnPatchAll ()
191+ for _ , tc := range testCases {
192+ mockey .PatchConvey (tc .name , t , func () {
193+ mockey .Mock (rpc .DeleteCustomCourseRPC ).To (func (ctx context.Context , req * course.DeleteCustomCourseRequest ) error {
194+ return tc .mockErr
195+ }).Build ()
196+
197+ body := & ut.Body {
198+ Body : bytes .NewBufferString (tc .body ),
199+ Len : len (tc .body ),
200+ }
201+ res := ut .PerformRequest (router , consts .MethodDelete , tc .url , body , ut.Header {
202+ Key : "Content-Type" ,
203+ Value : "application/json" ,
204+ })
205+ assert .Equal (t , consts .StatusOK , res .Result ().StatusCode ())
206+ assert .Contains (t , string (res .Result ().Body ()), tc .expectContains )
207+ })
208+ }
209+ }
210+
211+ func TestUpsertCustomCourse (t * testing.T ) {
212+ type testCase struct {
213+ name string
214+ url string
215+ body string
216+ mockResp * course.UpsertCustomCourseResponse
217+ mockErr error
218+ expectContains string
219+ }
220+
221+ testCases := []testCase {
222+ {
223+ name : "success" ,
224+ url : "/api/v2/jwch/course/upsert" ,
225+ body : `{"term":"202401","course":{"name":"x","location":"y","start_class":1,"end_class":2,"start_week":1,"end_week":2,"weekday":1}}` ,
226+ mockResp : & course.UpsertCustomCourseResponse {},
227+ expectContains : `{"code":"10000","message":"ok","data":` ,
228+ },
229+ {
230+ name : "rpc error" ,
231+ url : "/api/v2/jwch/course/upsert" ,
232+ body : `{"term":"202401","course":{"name":"x","location":"y","start_class":1,"end_class":2,"start_week":1,"end_week":2,"weekday":1}}` ,
233+ mockErr : errno .InternalServiceError ,
234+ expectContains : `{"code":"50001","message":"内部服务错误"}` ,
235+ },
236+ }
237+
238+ router := route .NewEngine (& config.Options {})
239+ router .POST ("/api/v2/jwch/course/upsert" , UpsertCustomCourse )
240+
241+ defer mockey .UnPatchAll ()
242+ for _ , tc := range testCases {
243+ mockey .PatchConvey (tc .name , t , func () {
244+ mockey .Mock (rpc .UpsertCustomCourseRPC ).To (func (ctx context.Context , req * course.UpsertCustomCourseRequest ) (* course.UpsertCustomCourseResponse , error ) {
245+ return tc .mockResp , tc .mockErr
246+ }).Build ()
247+
248+ body := & ut.Body {
249+ Body : bytes .NewBufferString (tc .body ),
250+ Len : len (tc .body ),
251+ }
252+ res := ut .PerformRequest (router , consts .MethodPost , tc .url , body , ut.Header {
253+ Key : "Content-Type" ,
254+ Value : "application/json" ,
255+ })
256+ assert .Equal (t , consts .StatusOK , res .Result ().StatusCode ())
257+ assert .Contains (t , string (res .Result ().Body ()), tc .expectContains )
258+ })
259+ }
260+ }
261+
181262func TestGetTermList (t * testing.T ) {
182263 type testCase struct {
183264 name string
0 commit comments