Skip to content

Commit

Permalink
test: add test in db
Browse files Browse the repository at this point in the history
  • Loading branch information
FantasyRL committed Dec 30, 2024
1 parent fe925b9 commit d81b084
Show file tree
Hide file tree
Showing 3 changed files with 263 additions and 0 deletions.
63 changes: 63 additions & 0 deletions internal/user/service/get_logindata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,66 @@ limitations under the License.
*/

package service

/*
func TestUserService_GetLoginData(t *testing.T) {
type testCase struct {
name string
expectedId string
expectedCookie []*http.Cookie
mockError error
expectingError bool
}
testCases := []testCase{
{
name: "success",
expectedId: "2024102301000",
expectedCookie: []*http.Cookie{
&http.Cookie{
Name: "test",
},
},
},
{
name: "jwch error",
mockError: errno.InternalServiceError,
expectingError: true,
},
}
req := &user.GetLoginDataRequest{
Id: "102301000",
Password: "102301000",
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockClientSet := new(base.ClientSet)
mockClientSet.SFClient = new(utils.Snowflake)
mockClientSet.DBClient = new(db.Database)
userService := NewUserService(context.Background(), "", nil, mockClientSet)
mockey.Mock((*jwch.Student).GetIdentifierAndCookies).To(func() (string, []*http.Cookie, error) {
if tc.expectingError {
return "", nil, tc.mockError
}
return tc.expectedId, tc.expectedCookie, nil
}).Build()
mockey.Mock((*UserService).insertStudentInfo).IncludeCurrentGoRoutine().To(func(req *user.GetLoginDataRequest, stu *jwch.Student) { return }).Build()
mockey.Mock((*userdb.DBUser).GetStudentById).To(func(ctx context.Context, stuId string) (bool, *model.Student, error) {
return true, nil, nil
}).Build()
id, cookie, err := userService.GetLoginData(req)
if tc.expectingError {
assert.Nil(t, cookie)
assert.Contains(t, err.Error(), errno.InternalServiceError.ErrorMsg)
} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedId, id)
assert.Equal(t, utils.ParseCookiesToString(tc.expectedCookie), cookie)
}
})
}
}
*/
89 changes: 89 additions & 0 deletions pkg/db/user/create_student_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package user

import (
"context"
"testing"

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

"github.com/west2-online/fzuhelper-server/pkg/db/model"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

func TestDBUser_CreateStudent(t *testing.T) {
type testCase struct {
name string
inPutStudent *model.Student
mockError error
expectingError bool
ErrorMsg string
}
stu := &model.Student{
StuId: "102301000",
Sex: "男",
College: "计算机与大数据学院",
Grade: 2023,
Major: "计算机科学与技术",
}
testCases := []testCase{
{
name: "success",
inPutStudent: stu,
mockError: nil,
expectingError: false,
},
{
name: "error",
inPutStudent: nil,
mockError: gorm.ErrInvalidValue,
expectingError: true,
ErrorMsg: "dal.CreateStudent error",
},
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockGormDB := new(gorm.DB)
mockSnowflake := new(utils.Snowflake)
mockDBUser := NewDBUser(mockGormDB, mockSnowflake)

mockey.Mock((*gorm.DB).WithContext).To(func(ctx context.Context) *gorm.DB {
return mockGormDB
}).Build()

mockey.Mock((*gorm.DB).Create).To(func(value interface{}) *gorm.DB {
if tc.mockError != nil {
mockGormDB.Error = tc.mockError
return mockGormDB
}
return mockGormDB
}).Build()

err := mockDBUser.CreateStudent(context.Background(), tc.inPutStudent)
if tc.expectingError {
assert.Error(t, err)
assert.Contains(t, err.Error(), tc.ErrorMsg)
} else {
assert.NoError(t, err)
}
})
}
}
111 changes: 111 additions & 0 deletions pkg/db/user/get_student_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
Copyright 2024 The west2-online Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package user

import (
"context"
"testing"

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

"github.com/west2-online/fzuhelper-server/pkg/db/model"
"github.com/west2-online/fzuhelper-server/pkg/utils"
)

func TestDBUser_GetStudentById(t *testing.T) {
type testCase struct {
name string
inPutId string
mockError error
expectingError bool
expectedStudent *model.Student
ErrorMsg string
}
stu := &model.Student{
StuId: "102301000",
Sex: "男",
College: "计算机与大数据学院",
Grade: 2023,
Major: "计算机科学与技术",
}
testCases := []testCase{
{
name: "success",
inPutId: stu.StuId,
mockError: nil,
expectingError: false,
expectedStudent: stu,
},
{
name: "record not found",
inPutId: stu.StuId,
mockError: gorm.ErrRecordNotFound,
expectingError: true,
expectedStudent: nil,
},
{
name: "error",
inPutId: stu.StuId,
mockError: gorm.ErrInvalidValue,
expectingError: true,
expectedStudent: nil,
ErrorMsg: "dal.GetStudentById error",
},
}
defer mockey.UnPatchAll()
for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {
mockGormDB := new(gorm.DB)
mockSnowflake := new(utils.Snowflake)
mockDBUser := NewDBUser(mockGormDB, mockSnowflake)

mockey.Mock((*gorm.DB).WithContext).To(func(ctx context.Context) *gorm.DB {
return mockGormDB
}).Build()
mockey.Mock((*gorm.DB).Where).To(func(query interface{}, args ...interface{}) *gorm.DB {
return mockGormDB
}).Build()
mockey.Mock((*gorm.DB).First).To(func(dest interface{}, conds ...interface{}) *gorm.DB {
if tc.mockError != nil {
mockGormDB.Error = tc.mockError
return mockGormDB
}

if res, ok := dest.(*model.Student); ok && tc.expectedStudent != nil {
*res = *tc.expectedStudent
}
return mockGormDB
}).Build()

_, result, err := mockDBUser.GetStudentById(context.Background(), tc.inPutId)
if tc.expectingError {
if err == nil {
return
}
assert.Error(t, err)
assert.Nil(t, result)
assert.Contains(t, err.Error(), tc.ErrorMsg)
} else {
assert.NoError(t, err)
assert.NotNil(t, result)
assert.Equal(t, tc.expectedStudent, result)
}
})
}
}

0 comments on commit d81b084

Please sign in to comment.