diff --git a/internal/course/service/get_course_list.go b/internal/course/service/get_course_list.go index 1bd24966..f3c41df4 100644 --- a/internal/course/service/get_course_list.go +++ b/internal/course/service/get_course_list.go @@ -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) } @@ -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 } diff --git a/internal/course/service/get_course_list_test.go b/internal/course/service/get_course_list_test.go index a9c623d9..ab86d80b 100644 --- a/internal/course/service/get_course_list_test.go +++ b/internal/course/service/get_course_list_test.go @@ -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 @@ -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, }, @@ -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, @@ -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) } }) } diff --git a/pkg/db/course/create_course_test.go b/pkg/db/course/create_course_test.go index af5ff1bc..19d75372 100644 --- a/pkg/db/course/create_course_test.go +++ b/pkg/db/course/create_course_test.go @@ -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{ diff --git a/pkg/db/course/get_course_test.go b/pkg/db/course/get_course_test.go index 076c090d..561a12e6 100644 --- a/pkg/db/course/get_course_test.go +++ b/pkg/db/course/get_course_test.go @@ -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 @@ -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, }, @@ -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 @@ -148,7 +153,7 @@ func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) { expectedResult: &model.UserCourse{ Id: 1001, TermCoursesSha256: "abc123def456", - ExamInfoSHA256: "exam-sha256", + ExamInfoSHA256: &examInfoSHA256, }, expectingError: false, }, diff --git a/pkg/db/course/update_course_test.go b/pkg/db/course/update_course_test.go index e0db0d90..6378e0b3 100644 --- a/pkg/db/course/update_course_test.go +++ b/pkg/db/course/update_course_test.go @@ -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 @@ -48,8 +51,8 @@ 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, @@ -57,8 +60,8 @@ 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, }, expectingError: false, }, diff --git a/pkg/db/model/course.go b/pkg/db/model/course.go index 963a530b..7c3953a0 100644 --- a/pkg/db/model/course.go +++ b/pkg/db/model/course.go @@ -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"`