Skip to content

Commit

Permalink
fix: get_room_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
SchwarzSail committed Oct 1, 2024
1 parent 6643c6a commit be4588c
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
3 changes: 1 addition & 2 deletions cmd/classroom/service/get_room.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
54 changes: 54 additions & 0 deletions cmd/classroom/service/get_room_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit be4588c

Please sign in to comment.