Skip to content

Commit

Permalink
make verify
Browse files Browse the repository at this point in the history
  • Loading branch information
jiuxia211 committed Dec 3, 2024
1 parent 4197ea3 commit 9810fc4
Show file tree
Hide file tree
Showing 9 changed files with 224 additions and 41 deletions.
22 changes: 20 additions & 2 deletions pkg/db/course/create_course_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 course

import (
Expand Down Expand Up @@ -51,7 +67,6 @@ func TestDBCourse_CreateUserTermCourse(t *testing.T) {

for _, tc := range testCases {
mockey.PatchConvey(tc.name, t, func() {

mockGormDB := new(gorm.DB)
mockSnowflake := new(utils.Snowflake)
mockDBCourse := NewDBCourse(mockGormDB, mockSnowflake)
Expand All @@ -65,7 +80,10 @@ func TestDBCourse_CreateUserTermCourse(t *testing.T) {
mockGormDB.Error = tc.mockError
return mockGormDB
}
*value.(*model.UserCourse) = *tc.input
userCourse, ok := value.(*model.UserCourse)
if ok {
*userCourse = *tc.input
}
return mockGormDB
}).Build()

Expand Down
74 changes: 52 additions & 22 deletions pkg/db/course/get_course_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
/*
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 course

import (
"context"
"errors"
"fmt"
"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"
"gorm.io/gorm"
)

func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
Expand All @@ -34,7 +52,8 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
StuId: "222200311",
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456"},
TermCoursesSha256: "abc123def456",
},
expectingError: false,
},
{
Expand Down Expand Up @@ -72,28 +91,32 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
}).Build()

mockey.Mock((*gorm.DB).First).To(func(dest interface{}, conds ...interface{}) *gorm.DB {
if tc.mockError != nil {
switch {
case tc.mockError != nil:
mockGormDB.Error = tc.mockError
return mockGormDB
} else if tc.mockRows != nil && tc.mockRows.Error == gorm.ErrRecordNotFound {
case tc.mockRows != nil && errors.Is(tc.mockRows.Error, gorm.ErrRecordNotFound):
mockGormDB.Error = gorm.ErrRecordNotFound
return mockGormDB
} else {
*dest.(*model.UserCourse) = *tc.expectedResult
default:
userCourse, ok := dest.(*model.UserCourse)
if ok {
*userCourse = *tc.expectedResult
}
return mockGormDB
}

}).Build()

result, err := mockDBCourse.GetUserTermCourseByStuIdAndTerm(context.Background(), tc.stuId, tc.term)

if tc.mockRows != nil && tc.mockRows.Error == gorm.ErrRecordNotFound {
switch {
case tc.mockRows != nil && errors.Is(tc.mockRows.Error, gorm.ErrRecordNotFound):
assert.NoError(t, err)
assert.Nil(t, result)
} else if tc.expectingError {
case tc.expectingError:
assert.Error(t, err)
assert.Nil(t, result)
} else {
default:
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
Expand Down Expand Up @@ -124,7 +147,8 @@ func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
StuId: "222200311",
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456"},
TermCoursesSha256: "abc123def456",
},
expectingError: false,
},
{
Expand Down Expand Up @@ -166,29 +190,35 @@ func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
}).Build()

mockey.Mock((*gorm.DB).First).To(func(dest interface{}, conds ...interface{}) *gorm.DB {
if tc.mockError != nil {
return &gorm.DB{Error: tc.mockError}
} else if tc.mockRows != nil && tc.mockRows.Error == gorm.ErrRecordNotFound {
return &gorm.DB{Error: gorm.ErrRecordNotFound}
} else {
*dest.(*model.UserCourse) = *tc.expectedResult
return &gorm.DB{Error: nil}
switch {
case tc.mockError != nil:
mockGormDB.Error = tc.mockError
return mockGormDB
case tc.mockRows != nil && errors.Is(tc.mockRows.Error, gorm.ErrRecordNotFound):
mockGormDB.Error = gorm.ErrRecordNotFound
return mockGormDB
default:
userCourse, ok := dest.(*model.UserCourse)
if ok {
*userCourse = *tc.expectedResult
}
return mockGormDB
}
}).Build()

result, err := mockDBCourse.GetUserTermCourseSha256ByStuIdAndTerm(context.Background(), tc.stuId, tc.term)

if tc.mockRows != nil && tc.mockRows.Error == gorm.ErrRecordNotFound {
switch {
case tc.mockRows != nil && errors.Is(tc.mockRows.Error, gorm.ErrRecordNotFound):
assert.NoError(t, err)
assert.Nil(t, result)
} else if tc.expectingError {
case tc.expectingError:
assert.Error(t, err)
assert.Nil(t, result)
} else {
default:
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
}
})

}
}
20 changes: 18 additions & 2 deletions pkg/db/course/update_course_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 course

import (
Expand All @@ -7,9 +23,10 @@ import (

"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"
"gorm.io/gorm"
)

func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
Expand Down Expand Up @@ -78,7 +95,6 @@ func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
assert.Error(t, err)
assert.Nil(t, result)
assert.Contains(t, err.Error(), "dal.UpdateUserTermCourse error")

} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedResult, result)
Expand Down
20 changes: 18 additions & 2 deletions pkg/db/launch_screen/add_image_list_showtime_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 launch_screen

import (
Expand All @@ -7,9 +23,10 @@ import (

"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"
"gorm.io/gorm"
)

func TestDBLaunchScreen_AddImageListShowTime(t *testing.T) {
Expand Down Expand Up @@ -76,7 +93,6 @@ func TestDBLaunchScreen_AddImageListShowTime(t *testing.T) {
if tc.expectingError {
assert.Error(t, err)
assert.Contains(t, err.Error(), "dal.AddImageListShowTime error")

} else {
assert.NoError(t, err)
assert.Equal(t, tc.expectedPictureList, tc.inputPictureList)
Expand Down
24 changes: 22 additions & 2 deletions pkg/db/launch_screen/add_point_time_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 launch_screen

import (
Expand All @@ -7,9 +23,10 @@ import (

"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"
"gorm.io/gorm"
)

func TestDBLaunchScreen_AddPointTime(t *testing.T) {
Expand Down Expand Up @@ -73,7 +90,10 @@ func TestDBLaunchScreen_AddPointTime(t *testing.T) {
return mockGormDB
}
if tc.initialPicture != nil {
*dest.(*model.Picture) = *tc.initialPicture
initialPicture, ok := dest.(*model.Picture)
if ok {
*initialPicture = *tc.initialPicture
}
}
return mockGormDB
}).Build()
Expand Down
24 changes: 22 additions & 2 deletions pkg/db/launch_screen/create_image_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 launch_screen

import (
Expand All @@ -8,9 +24,10 @@ import (

"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"
"gorm.io/gorm"
)

func TestDBLaunchScreen_CreateImage(t *testing.T) {
Expand Down Expand Up @@ -71,7 +88,10 @@ func TestDBLaunchScreen_CreateImage(t *testing.T) {
mockGormDB.Error = tc.mockError
return mockGormDB
}
*value.(*model.Picture) = *tc.inputPicture
inputPicture, ok := value.(*model.Picture)
if ok {
*inputPicture = *tc.inputPicture
}
return mockGormDB
}).Build()

Expand Down
24 changes: 22 additions & 2 deletions pkg/db/launch_screen/delete_image_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
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 launch_screen

import (
Expand All @@ -8,9 +24,10 @@ import (

"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"
"gorm.io/gorm"
)

func TestDBLaunchScreen_DeleteImage(t *testing.T) {
Expand Down Expand Up @@ -81,7 +98,10 @@ func TestDBLaunchScreen_DeleteImage(t *testing.T) {
mockGormDB.Error = tc.mockErrorTake
return mockGormDB
}
*dest.(*model.Picture) = *tc.expectedResult
expectedPicture, ok := dest.(*model.Picture)
if ok {
*expectedPicture = *tc.expectedResult
}
return mockGormDB
}).Build()

Expand Down
Loading

0 comments on commit 9810fc4

Please sign in to comment.