Skip to content

Commit 24a0977

Browse files
committed
chore: 新增测试用例
1 parent c601973 commit 24a0977

4 files changed

Lines changed: 591 additions & 0 deletions

File tree

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
1+
/*
2+
Copyright 2024 The west2-online Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package course
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/bytedance/mockey"
25+
"github.com/stretchr/testify/assert"
26+
"gorm.io/gorm"
27+
28+
"github.com/west2-online/fzuhelper-server/pkg/db/model"
29+
"github.com/west2-online/fzuhelper-server/pkg/utils"
30+
)
31+
32+
func TestDBCourse_CreateCustomCourse(t *testing.T) {
33+
type testCase struct {
34+
name string
35+
mockError error
36+
input *model.UserCustomCourse
37+
expectingError bool
38+
}
39+
40+
inputCourse := &model.UserCustomCourse{
41+
StuId: "222200311",
42+
Term: "202401",
43+
CourseId: "uuid-1",
44+
Name: "自习",
45+
Location: "图书馆",
46+
StartClass: 1,
47+
EndClass: 2,
48+
StartWeek: 1,
49+
EndWeek: 16,
50+
Weekday: 1,
51+
}
52+
53+
testCases := []testCase{
54+
{
55+
name: "CreateCustomCourse_Success",
56+
mockError: nil,
57+
input: inputCourse,
58+
expectingError: false,
59+
},
60+
{
61+
name: "CreateCustomCourse_DBError",
62+
mockError: fmt.Errorf("db error"),
63+
input: inputCourse,
64+
expectingError: true,
65+
},
66+
}
67+
68+
defer mockey.UnPatchAll()
69+
for _, tc := range testCases {
70+
mockey.PatchConvey(tc.name, t, func() {
71+
mockGormDB := new(gorm.DB)
72+
mockSnowflake := new(utils.Snowflake)
73+
mockDBCourse := NewDBCourse(mockGormDB, mockSnowflake)
74+
75+
mockey.Mock((*gorm.DB).WithContext).To(func(ctx context.Context) *gorm.DB {
76+
return mockGormDB
77+
}).Build()
78+
mockey.Mock((*gorm.DB).Create).To(func(value interface{}) *gorm.DB {
79+
if tc.mockError != nil {
80+
mockGormDB.Error = tc.mockError
81+
return mockGormDB
82+
}
83+
return mockGormDB
84+
}).Build()
85+
86+
err := mockDBCourse.CreateCustomCourse(context.Background(), tc.input)
87+
88+
if tc.expectingError {
89+
assert.Error(t, err)
90+
} else {
91+
assert.NoError(t, err)
92+
}
93+
})
94+
}
95+
}
96+
97+
func TestDBCourse_CheckDuplicateCustomCourse(t *testing.T) {
98+
type testCase struct {
99+
name string
100+
mockError error
101+
mockCount int64
102+
expectingError bool
103+
expectedResult bool
104+
}
105+
106+
testCases := []testCase{
107+
{
108+
name: "CheckDuplicateCustomCourse_Duplicate",
109+
mockError: nil,
110+
mockCount: 1,
111+
expectingError: false,
112+
expectedResult: true,
113+
},
114+
{
115+
name: "CheckDuplicateCustomCourse_NotDuplicate",
116+
mockError: nil,
117+
mockCount: 0,
118+
expectingError: false,
119+
expectedResult: false,
120+
},
121+
{
122+
name: "CheckDuplicateCustomCourse_DBError",
123+
mockError: fmt.Errorf("db error"),
124+
mockCount: 0,
125+
expectingError: true,
126+
expectedResult: false,
127+
},
128+
}
129+
130+
defer mockey.UnPatchAll()
131+
for _, tc := range testCases {
132+
mockey.PatchConvey(tc.name, t, func() {
133+
mockGormDB := new(gorm.DB)
134+
mockSnowflake := new(utils.Snowflake)
135+
mockDBCourse := NewDBCourse(mockGormDB, mockSnowflake)
136+
137+
mockey.Mock((*gorm.DB).WithContext).To(func(ctx context.Context) *gorm.DB {
138+
return mockGormDB
139+
}).Build()
140+
mockey.Mock((*gorm.DB).Model).To(func(value interface{}) *gorm.DB {
141+
return mockGormDB
142+
}).Build()
143+
mockey.Mock((*gorm.DB).Where).To(func(query interface{}, args ...interface{}) *gorm.DB {
144+
return mockGormDB
145+
}).Build()
146+
mockey.Mock((*gorm.DB).Count).To(func(count *int64) *gorm.DB {
147+
if tc.mockError != nil {
148+
mockGormDB.Error = tc.mockError
149+
return mockGormDB
150+
}
151+
*count = tc.mockCount
152+
return mockGormDB
153+
}).Build()
154+
155+
result, err := mockDBCourse.CheckDuplicateCustomCourse(
156+
context.Background(),
157+
"222200311", "202401",
158+
"自习", "图书馆",
159+
1, 2, 1, 16, 1,
160+
)
161+
162+
if tc.expectingError {
163+
assert.Error(t, err)
164+
assert.False(t, result)
165+
} else {
166+
assert.NoError(t, err)
167+
assert.Equal(t, tc.expectedResult, result)
168+
}
169+
})
170+
}
171+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
Copyright 2024 The west2-online Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package course
18+
19+
import (
20+
"context"
21+
"fmt"
22+
"testing"
23+
24+
"github.com/bytedance/mockey"
25+
"github.com/stretchr/testify/assert"
26+
"gorm.io/gorm"
27+
28+
"github.com/west2-online/fzuhelper-server/pkg/utils"
29+
)
30+
31+
func TestDBCourse_DeleteCustomCourse(t *testing.T) {
32+
type testCase struct {
33+
name string
34+
mockError error
35+
stuId string
36+
term string
37+
courseId string
38+
expectingError bool
39+
}
40+
41+
testCases := []testCase{
42+
{
43+
name: "DeleteCustomCourse_Success",
44+
mockError: nil,
45+
stuId: "222200311",
46+
term: "202401",
47+
courseId: "uuid-1",
48+
expectingError: false,
49+
},
50+
{
51+
name: "DeleteCustomCourse_DBError",
52+
mockError: fmt.Errorf("db error"),
53+
stuId: "222200311",
54+
term: "202401",
55+
courseId: "uuid-1",
56+
expectingError: true,
57+
},
58+
}
59+
60+
defer mockey.UnPatchAll()
61+
for _, tc := range testCases {
62+
mockey.PatchConvey(tc.name, t, func() {
63+
mockGormDB := new(gorm.DB)
64+
mockSnowflake := new(utils.Snowflake)
65+
mockDBCourse := NewDBCourse(mockGormDB, mockSnowflake)
66+
67+
mockey.Mock((*gorm.DB).WithContext).To(func(ctx context.Context) *gorm.DB {
68+
return mockGormDB
69+
}).Build()
70+
mockey.Mock((*gorm.DB).Where).To(func(query interface{}, args ...interface{}) *gorm.DB {
71+
return mockGormDB
72+
}).Build()
73+
mockey.Mock((*gorm.DB).Delete).To(func(value interface{}, conds ...interface{}) *gorm.DB {
74+
if tc.mockError != nil {
75+
mockGormDB.Error = tc.mockError
76+
return mockGormDB
77+
}
78+
return mockGormDB
79+
}).Build()
80+
81+
err := mockDBCourse.DeleteCustomCourse(context.Background(), tc.stuId, tc.term, tc.courseId)
82+
83+
if tc.expectingError {
84+
assert.Error(t, err)
85+
} else {
86+
assert.NoError(t, err)
87+
}
88+
})
89+
}
90+
}

0 commit comments

Comments
 (0)