Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions internal/course/service/get_course_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,18 +196,18 @@ func (s *CourseService) putExamToDatabase(stuId string, term string, rawCourses
if old == nil {
return nil
}
if old.ExamInfoSHA256 == examInfoSHA256 {
if old.ExamInfoSHA256 != nil && *old.ExamInfoSHA256 == examInfoSHA256 {
return nil
}

var oldExams []CourseExamInfo
if old.ExamInfo != "" {
if err = sonic.Unmarshal([]byte(old.ExamInfo), &oldExams); err != nil {
if old.ExamInfo != nil {
if err = sonic.Unmarshal([]byte(*old.ExamInfo), &oldExams); err != nil {
return errno.Errorf(errno.InternalJSONErrorCode,
"service.putExamToDatabase: decode exam info failed: %v", err)
}
}
if old.ExamInfoSHA256 == "" {
if old.ExamInfoSHA256 == nil || *old.ExamInfoSHA256 == "" {
// 历史数据没有考试快照时只建立基线,不把已有考试信息当作新增变化通知。
return s.updateExamSnapshot(old.Id, examInfo, examInfoSHA256)
}
Expand Down Expand Up @@ -254,8 +254,8 @@ func (s *CourseService) updateExamSnapshot(id int64, examInfo, examInfoSHA256 st
// 快照更新是本次考试变化处理的提交步骤;成功后下一次刷新不会再次识别同一变化。
_, err := s.db.Course.UpdateUserTermCourse(s.ctx, &model.UserCourse{
Id: id,
ExamInfo: examInfo,
ExamInfoSHA256: examInfoSHA256,
ExamInfo: &examInfo,
ExamInfoSHA256: &examInfoSHA256,
})
return err
}
Expand Down
21 changes: 14 additions & 7 deletions internal/course/service/get_course_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,9 @@ func TestPutExamToDatabase(t *testing.T) {
examInfoSHA256, err := courseExamInfoHash(exams)
assert.NoError(t, err)

oldExamInfo := `[{"name":"数据结构","teacher":"张老师","credit":"4.0","exam_time":"旧时间"},{"name":"高等数学","teacher":"李老师","credit":"5.0","exam_time":"旧时间2"}]`
oldExamInfoSHA256 := "old-sha256"

type testCase struct {
name string
rawCourses []*jwch.Course
Expand All @@ -658,7 +661,7 @@ func TestPutExamToDatabase(t *testing.T) {
},
{
name: "unchanged exam snapshot is not updated",
oldCourse: &dbmodel.UserCourse{Id: 1, ExamInfoSHA256: examInfoSHA256},
oldCourse: &dbmodel.UserCourse{Id: 1, ExamInfoSHA256: &examInfoSHA256},
expectUpdate: false,
expectEnqueue: 0,
},
Expand All @@ -675,8 +678,8 @@ func TestPutExamToDatabase(t *testing.T) {
},
oldCourse: &dbmodel.UserCourse{
Id: 1,
ExamInfo: `[{"name":"数据结构","teacher":"张老师","credit":"4.0","exam_time":"旧时间"},{"name":"高等数学","teacher":"李老师","credit":"5.0","exam_time":"旧时间2"}]`,
ExamInfoSHA256: "old-sha256",
ExamInfo: &oldExamInfo,
ExamInfoSHA256: &oldExamInfoSHA256,
},
expectUpdate: true,
expectEnqueue: 2,
Expand Down Expand Up @@ -731,11 +734,15 @@ func TestPutExamToDatabase(t *testing.T) {
assert.NotNil(t, updatedCourse)
assert.Equal(t, int64(1), updatedCourse.Id)
if tc.expectExamInfo != "" {
assert.Equal(t, tc.expectExamInfo, updatedCourse.ExamInfo)
assert.Equal(t, tc.expectExamHash, updatedCourse.ExamInfoSHA256)
assert.NotNil(t, updatedCourse.ExamInfo)
assert.Equal(t, tc.expectExamInfo, *updatedCourse.ExamInfo)
assert.NotNil(t, updatedCourse.ExamInfoSHA256)
assert.Equal(t, tc.expectExamHash, *updatedCourse.ExamInfoSHA256)
} else {
assert.NotEmpty(t, updatedCourse.ExamInfo)
assert.NotEmpty(t, updatedCourse.ExamInfoSHA256)
assert.NotNil(t, updatedCourse.ExamInfo)
assert.NotEmpty(t, *updatedCourse.ExamInfo)
assert.NotNil(t, updatedCourse.ExamInfoSHA256)
assert.NotEmpty(t, *updatedCourse.ExamInfoSHA256)
}
})
}
Expand Down
7 changes: 5 additions & 2 deletions pkg/db/course/create_course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,17 @@ func TestDBCourse_CreateUserTermCourse(t *testing.T) {
expectingError bool
}

examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
examInfoSHA256 := "exam-sha256"

expectedResult := &model.UserCourse{
Id: 1001,
StuId: "222200311",
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456",
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
ExamInfoSHA256: "exam-sha256",
ExamInfo: &examInfo,
ExamInfoSHA256: &examInfoSHA256,
}

testCases := []testCase{
Expand Down
11 changes: 8 additions & 3 deletions pkg/db/course/get_course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
)

func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
examInfoSHA256 := "exam-sha256"

type testCase struct {
name string
mockError error
Expand All @@ -53,8 +56,8 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456",
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
ExamInfoSHA256: "exam-sha256",
ExamInfo: &examInfo,
ExamInfoSHA256: &examInfoSHA256,
},
expectingError: false,
},
Expand Down Expand Up @@ -128,6 +131,8 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
}

func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
examInfoSHA256 := "exam-sha256"

type testCase struct {
name string
mockError error
Expand All @@ -148,7 +153,7 @@ func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
expectedResult: &model.UserCourse{
Id: 1001,
TermCoursesSha256: "abc123def456",
ExamInfoSHA256: "exam-sha256",
ExamInfoSHA256: &examInfoSHA256,
},
expectingError: false,
},
Expand Down
11 changes: 7 additions & 4 deletions pkg/db/course/update_course_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ import (
)

func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
examInfoSHA256 := "exam-sha256"

type testCase struct {
name string
mockError error
Expand All @@ -48,17 +51,17 @@ func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456",
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
ExamInfoSHA256: "exam-sha256",
ExamInfo: &examInfo,
ExamInfoSHA256: &examInfoSHA256,
},
expectedResult: &model.UserCourse{
Id: 1001,
StuId: "222200311",
Term: "202401",
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
TermCoursesSha256: "abc123def456",
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
ExamInfoSHA256: "exam-sha256",
ExamInfo: &examInfo,
ExamInfoSHA256: &examInfoSHA256,
},
expectingError: false,
},
Expand Down
4 changes: 2 additions & 2 deletions pkg/db/model/course.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type UserCourse struct {
Term string
TermCourses string
TermCoursesSha256 string
ExamInfo string
ExamInfoSHA256 string
ExamInfo *string
ExamInfoSHA256 *string
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt `sql:"index"`
Expand Down