feat(smoke): support reason tags on smoke logs

This commit is contained in:
nepiedg
2026-04-16 11:06:14 +08:00
parent 411ded8a0c
commit 6e0a06cfcf
8 changed files with 158 additions and 65 deletions
@@ -29,6 +29,7 @@ CREATE TABLE fa_smoke_log (
smoke_time DATE NULL,
smoke_at DATETIME NULL,
remark TEXT,
reason_tags TEXT,
createtime INTEGER,
updatetime INTEGER,
deletetime INTEGER,
@@ -68,3 +69,60 @@ func TestSmokeLogServiceCreateKeepsZeroForResisted(t *testing.T) {
}
}
func TestSmokeLogServiceCreatePersistsReasonTags(t *testing.T) {
t.Parallel()
db := setupSmokeLogServiceTestDB(t)
svc := NewSmokeLogService(db)
smokeAt := time.Date(2026, 3, 4, 8, 30, 0, 0, time.Local)
_, err := svc.Create(context.Background(), 1002, CreateSmokeLogRequest{
SmokeAt: &smokeAt,
Remark: "压力大;会后补了一根",
ReasonTags: smokemodel.StringSlice{"stress", "social"},
Level: 3,
Num: 1,
})
if err != nil {
t.Fatalf("create log with reason tags: %v", err)
}
var got smokemodel.SmokeLog
if err := db.Where("uid = ?", 1002).Order("id DESC").First(&got).Error; err != nil {
t.Fatalf("load created record: %v", err)
}
if len(got.ReasonTags) != 2 || got.ReasonTags[0] != "stress" || got.ReasonTags[1] != "social" {
t.Fatalf("created reason_tags=%v, want=[stress social]", got.ReasonTags)
}
}
func TestSmokeLogServiceUpdatePersistsReasonTags(t *testing.T) {
t.Parallel()
db := setupSmokeLogServiceTestDB(t)
svc := NewSmokeLogService(db)
smokeAt := time.Date(2026, 3, 4, 9, 0, 0, 0, time.Local)
record, err := svc.Create(context.Background(), 1003, CreateSmokeLogRequest{
SmokeAt: &smokeAt,
Remark: "old",
Level: 2,
Num: 1,
})
if err != nil {
t.Fatalf("create seed log: %v", err)
}
reasonTags := smokemodel.StringSlice{"after_meal", "other"}
updated, err := svc.Update(context.Background(), 1003, record.ID, UpdateSmokeLogRequest{
ReasonTags: &reasonTags,
})
if err != nil {
t.Fatalf("update reason_tags: %v", err)
}
if len(updated.ReasonTags) != 2 || updated.ReasonTags[0] != "after_meal" || updated.ReasonTags[1] != "other" {
t.Fatalf("updated reason_tags=%v, want=[after_meal other]", updated.ReasonTags)
}
}