Skip to content

Commit

Permalink
test: add api 's classroom, course, user unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ozline committed Dec 1, 2024
1 parent e719419 commit a0a77a7
Show file tree
Hide file tree
Showing 3 changed files with 328 additions and 0 deletions.
135 changes: 135 additions & 0 deletions api/rpc/classroom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package rpc

import (
"context"
"fmt"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/classroom"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
)

func TestGetEmptyRoomRPC(t *testing.T) {
type testCase struct {
name string
mockResp []*model.Classroom
mockError error
expectedResult []*model.Classroom
expectingError bool
}

testCases := []testCase{
{
name: "GetEmptyRoomSuccess",
mockResp: []*model.Classroom{
{
Build: "东 1",
Location: "旗山东1-201",
Capacity: "60",
Type: "机房",
},
},
mockError: nil,
expectedResult: []*model.Classroom{
{
Build: "东 1",
Location: "旗山东1-201",
Capacity: "60",
Type: "机房",
},
},
},
{
name: "GetEmptyRoomRPCError",
mockResp: nil,
mockError: fmt.Errorf("RPC call failed"),
expectedResult: nil,
expectingError: true,
},
}

req := &classroom.EmptyRoomRequest{
Date: "2024-12-01",
Campus: "旗山校区",
StartTime: "1",
EndTime: "1",
}

defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock(GetEmptyRoomRPC).Return(tc.mockResp, tc.mockError).Build()
result, err := GetEmptyRoomRPC(context.Background(), req)
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}

func TestGetExamRoomInfoRPC(t *testing.T) {
type testCase struct {
name string
mockResp []*model.ExamRoomInfo
mockError error
expectedResult []*model.ExamRoomInfo
expectingError bool
}

testCases := []testCase{
{
name: "GetExamRoomInfoSuccess",
mockResp: []*model.ExamRoomInfo{
{
Name: "高等数学",
Credit: "4.5",
Teacher: "张三",
Location: "旗山校区教一201",
Time: "08:00-10:00",
Date: "2024-12-15",
},
},
mockError: nil,
expectedResult: []*model.ExamRoomInfo{
{
Name: "高等数学",
Credit: "4.5",
Teacher: "张三",
Location: "旗山校区教一201",
Time: "08:00-10:00",
Date: "2024-12-15",
},
},
},
{
name: "GetExamRoomInfoRPCError",
mockResp: nil,
mockError: fmt.Errorf("RPC call failed"),
expectedResult: nil,
expectingError: true,
},
}

req := &classroom.ExamRoomInfoRequest{}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock(GetExamRoomInfoRPC).Return(tc.mockResp, tc.mockError).Build()
result, err := GetExamRoomInfoRPC(context.Background(), req)
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}
126 changes: 126 additions & 0 deletions api/rpc/course_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package rpc

import (
"context"
"fmt"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/course"
"github.com/west2-online/fzuhelper-server/kitex_gen/model"
)

func TestGetCourseListRPC(t *testing.T) {
type testCase struct {
name string
mockResp []*model.Course
mockError error
expectedResult []*model.Course
expectingError bool
}

testCases := []testCase{
{
name: "GetCourseListSuccess",
mockResp: []*model.Course{
{
Name: "高级语言程序设计实践",
Teacher: "孙岚",
ScheduleRules: []*model.CourseScheduleRule{
{
Location: "铜盘A205",
StartClass: 3,
EndClass: 4,
StartWeek: 8,
EndWeek: 16,
Weekday: 3,
Single: true,
Double: true,
Adjust: false,
},
{
Location: "铜盘A508",
StartClass: 7,
EndClass: 8,
StartWeek: 8,
EndWeek: 16,
Weekday: 5,
Single: true,
Double: true,
Adjust: false,
},
},
RawScheduleRules: "08-16 星期3:3-4节 铜盘A205\\n08-16 星期5:7-8节 铜盘A508\\n",
RawAdjust: "",
Remark: "实践课教师必须与理论课教师相同。",
Syllabus: "https://jwcjwxt2.fzu.edu.cn:81/pyfa/jxdg/TeachingProgram_view.aspx?kcdm=01000100",
Lessonplan: "https://jwcjwxt2.fzu.edu.cn:81/pyfa/skjh/TeachingPlan_view.aspx?kkhm=20240101000100003",
},
},
mockError: nil,
expectedResult: []*model.Course{
{
Name: "高级语言程序设计实践",
Teacher: "孙岚",
ScheduleRules: []*model.CourseScheduleRule{
{
Location: "铜盘A205",
StartClass: 3,
EndClass: 4,
StartWeek: 8,
EndWeek: 16,
Weekday: 3,
Single: true,
Double: true,
Adjust: false,
},
{
Location: "铜盘A508",
StartClass: 7,
EndClass: 8,
StartWeek: 8,
EndWeek: 16,
Weekday: 5,
Single: true,
Double: true,
Adjust: false,
},
},
RawScheduleRules: "08-16 星期3:3-4节 铜盘A205\\n08-16 星期5:7-8节 铜盘A508\\n",
RawAdjust: "",
Remark: "实践课教师必须与理论课教师相同。",
Syllabus: "https://jwcjwxt2.fzu.edu.cn:81/pyfa/jxdg/TeachingProgram_view.aspx?kcdm=01000100",
Lessonplan: "https://jwcjwxt2.fzu.edu.cn:81/pyfa/skjh/TeachingPlan_view.aspx?kkhm=20240101000100003",
},
},
},
{
name: "GetCourseListRPCError",
mockResp: nil,
mockError: fmt.Errorf("RPC call failed"),
expectedResult: nil,
expectingError: true,
},
}

req := &course.CourseListRequest{
Term: "202401",
}

defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock(GetCourseListRPC).Return(tc.mockResp, tc.mockError).Build()
result, err := GetCourseListRPC(context.Background(), req)
if tc.expectingError {
assert.Nil(t, result)
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})
}
}
67 changes: 67 additions & 0 deletions api/rpc/user_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package rpc

import (
"context"
"fmt"
"testing"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"

"github.com/west2-online/fzuhelper-server/kitex_gen/user"
)

func TestGetLoginDataRPC(t *testing.T) {
type testCase struct {
name string
mockRespId string
mockRespCookies []string
mockError error
expectedId string
expectedCookies []string
expectingError bool
}

testCases := []testCase{
{
name: "GetLoginDataSuccess",
mockRespId: "123456",
mockRespCookies: []string{"session_token=abc123", "path=/"},
mockError: nil,
expectedId: "123456",
expectedCookies: []string{"session_token=abc123", "path=/"},
expectingError: false,
},
{
name: "GetLoginDataRPCError",
mockRespId: "",
mockRespCookies: nil,
mockError: fmt.Errorf("RPC call failed"),
expectedId: "",
expectedCookies: nil,
expectingError: true,
},
}

req := &user.GetLoginDataRequest{
Id: "test_user",
Password: "test_password",
}

defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockey.Mock(GetLoginDataRPC).Return(tc.mockRespId, tc.mockRespCookies, tc.mockError).Build()
id, cookies, err := GetLoginDataRPC(context.Background(), req)
if tc.expectingError {
assert.Empty(t, id)
assert.Nil(t, cookies)
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedId, id)
assert.Equal(t, tc.expectedCookies, cookies)
}
})
}
}

0 comments on commit a0a77a7

Please sign in to comment.