@@ -30,40 +30,59 @@ import (
3030
3131func TestDBCourse_UpdateCustomCourse (t * testing.T ) {
3232 type testCase struct {
33- name string
34- mockError error
35- stuId string
36- term string
37- courseId string
38- updates map [string ]interface {}
39- expectingError bool
33+ name string
34+ mockError error
35+ mockRowsAffected int64
36+ stuId string
37+ term string
38+ courseId string
39+ updates map [string ]interface {}
40+ expectingError bool
41+ expectedRows int64
4042 }
4143
4244 testCases := []testCase {
4345 {
44- name : "UpdateCustomCourse_Success" ,
45- mockError : nil ,
46- stuId : "222200311" ,
47- term : "202401" ,
48- courseId : "uuid-1" ,
46+ name : "UpdateCustomCourse_Success" ,
47+ mockError : nil ,
48+ mockRowsAffected : 1 ,
49+ stuId : "222200311" ,
50+ term : "202401" ,
51+ courseId : "uuid-1" ,
4952 updates : map [string ]interface {}{
5053 "name" : "自习(更新)" ,
5154 "location" : "图书馆3楼" ,
5255 "start_class" : 3 ,
5356 "end_class" : 4 ,
5457 },
5558 expectingError : false ,
59+ expectedRows : 1 ,
5660 },
5761 {
58- name : "UpdateCustomCourse_DBError" ,
59- mockError : fmt .Errorf ("db error" ),
60- stuId : "222200311" ,
61- term : "202401" ,
62- courseId : "uuid-1" ,
62+ name : "UpdateCustomCourse_NotFound" ,
63+ mockError : nil ,
64+ mockRowsAffected : 0 ,
65+ stuId : "222200311" ,
66+ term : "202401" ,
67+ courseId : "not-exist" ,
68+ updates : map [string ]interface {}{
69+ "name" : "自习(更新)" ,
70+ },
71+ expectingError : false ,
72+ expectedRows : 0 ,
73+ },
74+ {
75+ name : "UpdateCustomCourse_DBError" ,
76+ mockError : fmt .Errorf ("db error" ),
77+ mockRowsAffected : 0 ,
78+ stuId : "222200311" ,
79+ term : "202401" ,
80+ courseId : "uuid-1" ,
6381 updates : map [string ]interface {}{
6482 "name" : "自习(更新)" ,
6583 },
6684 expectingError : true ,
85+ expectedRows : 0 ,
6786 },
6887 }
6988
@@ -84,20 +103,22 @@ func TestDBCourse_UpdateCustomCourse(t *testing.T) {
84103 return mockGormDB
85104 }).Build ()
86105 mockey .Mock ((* gorm .DB ).Updates ).To (func (values interface {}) * gorm.DB {
106+ mockGormDB .RowsAffected = tc .mockRowsAffected
87107 if tc .mockError != nil {
88108 mockGormDB .Error = tc .mockError
89109 return mockGormDB
90110 }
91111 return mockGormDB
92112 }).Build ()
93113
94- err := mockDBCourse .UpdateCustomCourse (context .Background (), tc .stuId , tc .term , tc .courseId , tc .updates )
114+ rows , err := mockDBCourse .UpdateCustomCourse (context .Background (), tc .stuId , tc .term , tc .courseId , tc .updates )
95115
96116 if tc .expectingError {
97117 assert .Error (t , err )
98118 } else {
99119 assert .NoError (t , err )
100120 }
121+ assert .Equal (t , tc .expectedRows , rows )
101122 })
102123 }
103124}
0 commit comments