diff --git a/cmd/classroom/service/get_room.go b/cmd/classroom/service/get_room.go index 6e26e667..4d53aaaa 100644 --- a/cmd/classroom/service/get_room.go +++ b/cmd/classroom/service/get_room.go @@ -5,14 +5,13 @@ import ( "github.com/pkg/errors" "github.com/west2-online/fzuhelper-server/cmd/classroom/dal/cache" "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" - "github.com/west2-online/fzuhelper-server/pkg/errno" ) func (s *ClassroomService) GetEmptyRoom(req *classroom.EmptyRoomRequest) ([]string, error) { //从redis中获取数据 key := fmt.Sprintf("%s.%s.%s.%s", req.Date, req.Campus, req.StartTime, req.EndTime) if ok := cache.IsExistRoomInfo(s.ctx, key); !ok { - return nil, errors.Wrap(errno.InternalServiceError, "service.GetEmptyRoom: room info not exist") + return nil, errors.New("service.GetEmptyRoom: room info not exist") } emptyRoomList, err := cache.GetEmptyRoomCache(s.ctx, key) if err != nil { diff --git a/cmd/classroom/service/get_room_test.go b/cmd/classroom/service/get_room_test.go index 6d43c336..d35f620f 100644 --- a/cmd/classroom/service/get_room_test.go +++ b/cmd/classroom/service/get_room_test.go @@ -1 +1,55 @@ package service + +import ( + "context" + "github.com/bytedance/mockey" + "github.com/stretchr/testify/assert" + "github.com/west2-online/fzuhelper-server/cmd/classroom/dal/cache" + "github.com/west2-online/fzuhelper-server/kitex_gen/classroom" + "testing" +) + +func TestGetEmptyRoom_RoomInfoNotExist(t *testing.T) { + classroomService := NewClassroomService(context.Background()) + + // 模拟请求参数 + req := &classroom.EmptyRoomRequest{ + Date: "2024-10-01", + Campus: "旗山校区", + StartTime: "1", + EndTime: "1", + } + + // Mock cache.IsExistRoomInfo 返回 false + mockey.Mock(cache.IsExistRoomInfo).Return(false).Build() + + result, err := classroomService.GetEmptyRoom(req) + + // 断言返回结果 + assert.Nil(t, result) + assert.EqualError(t, err, "service.GetEmptyRoom: room info not exist") +} + +func TestGetEmptyRoom_RoomInfoExist(t *testing.T) { + classroomService := NewClassroomService(context.Background()) + + req := &classroom.EmptyRoomRequest{ + Date: "2024-10-01", + Campus: "旗山校区", + StartTime: "1", + EndTime: "1", + } + + // Mock cache.IsExistRoomInfo 返回 true + mockey.Mock(cache.IsExistRoomInfo).Return(true).Build() + + expectedRooms := []string{"旗山东1"} + mockey.Mock(cache.GetEmptyRoomCache).Return(expectedRooms, nil).Build() + + // 调用 GetEmptyRoom 方法 + result, err := classroomService.GetEmptyRoom(req) + + // 断言返回结果 + assert.NoError(t, err) + assert.Equal(t, expectedRooms, result) +} diff --git a/go.mod b/go.mod index c4060a20..6316c78a 100644 --- a/go.mod +++ b/go.mod @@ -32,6 +32,7 @@ require ( github.com/antchfx/xpath v1.2.4 // indirect github.com/apache/skywalking-go v0.5.0 // indirect github.com/bytedance/go-tagexpr/v2 v2.9.2 // indirect + github.com/bytedance/mockey v1.2.12 // indirect github.com/bytedance/sonic/loader v0.2.0 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect @@ -41,9 +42,13 @@ require ( github.com/go-logr/logr v1.4.2 // indirect github.com/go-resty/resty/v2 v2.7.0 // indirect github.com/google/uuid v1.6.0 // indirect + github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 // indirect github.com/henrylee2cn/ameda v1.4.10 // indirect github.com/henrylee2cn/goutil v0.0.0-20210127050712-89660552f6f8 // indirect + github.com/jtolds/gls v4.20.0+incompatible // indirect github.com/nyaruka/phonenumbers v1.0.55 // indirect + github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d // indirect + github.com/smartystreets/goconvey v1.6.4 // indirect k8s.io/apimachinery v0.31.1 // indirect k8s.io/klog/v2 v2.130.1 // indirect k8s.io/utils v0.0.0-20240921022957-49e7df575cb6 // indirect