Skip to content

Commit 807afe5

Browse files
committed
fix :fix exam_sha258 null field problem
1 parent 67e0b54 commit 807afe5

6 files changed

Lines changed: 42 additions & 24 deletions

File tree

internal/course/service/get_course_list.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,18 +196,18 @@ func (s *CourseService) putExamToDatabase(stuId string, term string, rawCourses
196196
if old == nil {
197197
return nil
198198
}
199-
if old.ExamInfoSHA256 == examInfoSHA256 {
199+
if old.ExamInfoSHA256 != nil && *old.ExamInfoSHA256 == examInfoSHA256 {
200200
return nil
201201
}
202202

203203
var oldExams []CourseExamInfo
204-
if old.ExamInfo != "" {
205-
if err = sonic.Unmarshal([]byte(old.ExamInfo), &oldExams); err != nil {
204+
if old.ExamInfo != nil {
205+
if err = sonic.Unmarshal([]byte(*old.ExamInfo), &oldExams); err != nil {
206206
return errno.Errorf(errno.InternalJSONErrorCode,
207207
"service.putExamToDatabase: decode exam info failed: %v", err)
208208
}
209209
}
210-
if old.ExamInfoSHA256 == "" {
210+
if old.ExamInfoSHA256 == nil || *old.ExamInfoSHA256 == "" {
211211
// 历史数据没有考试快照时只建立基线,不把已有考试信息当作新增变化通知。
212212
return s.updateExamSnapshot(old.Id, examInfo, examInfoSHA256)
213213
}
@@ -254,8 +254,8 @@ func (s *CourseService) updateExamSnapshot(id int64, examInfo, examInfoSHA256 st
254254
// 快照更新是本次考试变化处理的提交步骤;成功后下一次刷新不会再次识别同一变化。
255255
_, err := s.db.Course.UpdateUserTermCourse(s.ctx, &model.UserCourse{
256256
Id: id,
257-
ExamInfo: examInfo,
258-
ExamInfoSHA256: examInfoSHA256,
257+
ExamInfo: &examInfo,
258+
ExamInfoSHA256: &examInfoSHA256,
259259
})
260260
return err
261261
}

internal/course/service/get_course_list_test.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -636,6 +636,9 @@ func TestPutExamToDatabase(t *testing.T) {
636636
examInfoSHA256, err := courseExamInfoHash(exams)
637637
assert.NoError(t, err)
638638

639+
oldExamInfo := `[{"name":"数据结构","teacher":"张老师","credit":"4.0","exam_time":"旧时间"},{"name":"高等数学","teacher":"李老师","credit":"5.0","exam_time":"旧时间2"}]`
640+
oldExamInfoSHA256 := "old-sha256"
641+
639642
type testCase struct {
640643
name string
641644
rawCourses []*jwch.Course
@@ -658,7 +661,7 @@ func TestPutExamToDatabase(t *testing.T) {
658661
},
659662
{
660663
name: "unchanged exam snapshot is not updated",
661-
oldCourse: &dbmodel.UserCourse{Id: 1, ExamInfoSHA256: examInfoSHA256},
664+
oldCourse: &dbmodel.UserCourse{Id: 1, ExamInfoSHA256: &examInfoSHA256},
662665
expectUpdate: false,
663666
expectEnqueue: 0,
664667
},
@@ -675,8 +678,8 @@ func TestPutExamToDatabase(t *testing.T) {
675678
},
676679
oldCourse: &dbmodel.UserCourse{
677680
Id: 1,
678-
ExamInfo: `[{"name":"数据结构","teacher":"张老师","credit":"4.0","exam_time":"旧时间"},{"name":"高等数学","teacher":"李老师","credit":"5.0","exam_time":"旧时间2"}]`,
679-
ExamInfoSHA256: "old-sha256",
681+
ExamInfo: &oldExamInfo,
682+
ExamInfoSHA256: &oldExamInfoSHA256,
680683
},
681684
expectUpdate: true,
682685
expectEnqueue: 2,
@@ -731,11 +734,15 @@ func TestPutExamToDatabase(t *testing.T) {
731734
assert.NotNil(t, updatedCourse)
732735
assert.Equal(t, int64(1), updatedCourse.Id)
733736
if tc.expectExamInfo != "" {
734-
assert.Equal(t, tc.expectExamInfo, updatedCourse.ExamInfo)
735-
assert.Equal(t, tc.expectExamHash, updatedCourse.ExamInfoSHA256)
737+
assert.NotNil(t, updatedCourse.ExamInfo)
738+
assert.Equal(t, tc.expectExamInfo, *updatedCourse.ExamInfo)
739+
assert.NotNil(t, updatedCourse.ExamInfoSHA256)
740+
assert.Equal(t, tc.expectExamHash, *updatedCourse.ExamInfoSHA256)
736741
} else {
737-
assert.NotEmpty(t, updatedCourse.ExamInfo)
738-
assert.NotEmpty(t, updatedCourse.ExamInfoSHA256)
742+
assert.NotNil(t, updatedCourse.ExamInfo)
743+
assert.NotEmpty(t, *updatedCourse.ExamInfo)
744+
assert.NotNil(t, updatedCourse.ExamInfoSHA256)
745+
assert.NotEmpty(t, *updatedCourse.ExamInfoSHA256)
739746
}
740747
})
741748
}

pkg/db/course/create_course_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,17 @@ func TestDBCourse_CreateUserTermCourse(t *testing.T) {
3838
expectingError bool
3939
}
4040

41+
examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
42+
examInfoSHA256 := "exam-sha256"
43+
4144
expectedResult := &model.UserCourse{
4245
Id: 1001,
4346
StuId: "222200311",
4447
Term: "202401",
4548
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
4649
TermCoursesSha256: "abc123def456",
47-
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
48-
ExamInfoSHA256: "exam-sha256",
50+
ExamInfo: &examInfo,
51+
ExamInfoSHA256: &examInfoSHA256,
4952
}
5053

5154
testCases := []testCase{

pkg/db/course/get_course_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import (
3131
)
3232

3333
func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
34+
examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
35+
examInfoSHA256 := "exam-sha256"
36+
3437
type testCase struct {
3538
name string
3639
mockError error
@@ -53,8 +56,8 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
5356
Term: "202401",
5457
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
5558
TermCoursesSha256: "abc123def456",
56-
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
57-
ExamInfoSHA256: "exam-sha256",
59+
ExamInfo: &examInfo,
60+
ExamInfoSHA256: &examInfoSHA256,
5861
},
5962
expectingError: false,
6063
},
@@ -128,6 +131,8 @@ func TestDBCourse_GetUserTermCourseByStuIdAndTerm(t *testing.T) {
128131
}
129132

130133
func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
134+
examInfoSHA256 := "exam-sha256"
135+
131136
type testCase struct {
132137
name string
133138
mockError error
@@ -148,7 +153,7 @@ func TestDBCourse_GetUserTermCourseSha256ByStuIdAndTerm(t *testing.T) {
148153
expectedResult: &model.UserCourse{
149154
Id: 1001,
150155
TermCoursesSha256: "abc123def456",
151-
ExamInfoSHA256: "exam-sha256",
156+
ExamInfoSHA256: &examInfoSHA256,
152157
},
153158
expectingError: false,
154159
},

pkg/db/course/update_course_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ import (
3030
)
3131

3232
func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
33+
examInfo := `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`
34+
examInfoSHA256 := "exam-sha256"
35+
3336
type testCase struct {
3437
name string
3538
mockError error
@@ -48,17 +51,17 @@ func TestDBCourse_UpdateUserTermCourse(t *testing.T) {
4851
Term: "202401",
4952
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
5053
TermCoursesSha256: "abc123def456",
51-
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
52-
ExamInfoSHA256: "exam-sha256",
54+
ExamInfo: &examInfo,
55+
ExamInfoSHA256: &examInfoSHA256,
5356
},
5457
expectedResult: &model.UserCourse{
5558
Id: 1001,
5659
StuId: "222200311",
5760
Term: "202401",
5861
TermCourses: `[{"courseId":"C123","courseName":"Math"}]`,
5962
TermCoursesSha256: "abc123def456",
60-
ExamInfo: `[{"name":"Math","exam_time":"2026-06-20 09:00"}]`,
61-
ExamInfoSHA256: "exam-sha256",
63+
ExamInfo: &examInfo,
64+
ExamInfoSHA256: &examInfoSHA256,
6265
},
6366
expectingError: false,
6467
},

pkg/db/model/course.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ type UserCourse struct {
2828
Term string
2929
TermCourses string
3030
TermCoursesSha256 string
31-
ExamInfo string
32-
ExamInfoSHA256 string
31+
ExamInfo *string
32+
ExamInfoSHA256 *string
3333
CreatedAt time.Time
3434
UpdatedAt time.Time
3535
DeletedAt gorm.DeletedAt `sql:"index"`

0 commit comments

Comments
 (0)