feat: 建立统一检测任务、样本链与发病事件

This commit is contained in:
weijuesen
2026-08-14 03:31:02 +08:00
parent a25bc7abc6
commit cc93c9a053
26 changed files with 1716 additions and 32 deletions
@@ -0,0 +1,44 @@
package service
import (
"context"
"database/sql/driver"
"encoding/json"
"testing"
"github.com/DATA-DOG/go-sqlmock"
"silk-server-go/internal/model"
)
func TestDetectionTaskCreateOutboxIsIdempotent(t *testing.T) {
o, mock := newOutboxMock(t)
handler := NewDetectionTaskOutboxHandler(o.db)
payload, _ := json.Marshal(DetectionTaskCreatePayload{
SourceKey: "inspection-inspection-1",
SourceType: "inspection",
SourceID: "inspection-1",
InspectionID: "inspection-1",
Disease: "待确认",
})
event := model.OutboxEvent{
EventType: OutboxEventDetectionTaskCreate,
Payload: payload,
}
args := make([]driver.Value, 0, 20)
for i := 0; i < 20; i++ {
args = append(args, sqlmock.AnyArg())
}
mock.ExpectQuery(`INSERT INTO "detection_tasks".*ON CONFLICT \("source_key"\) DO NOTHING`).
WithArgs(args...).
WillReturnRows(sqlmock.NewRows([]string{"id"}).AddRow("task-1"))
if err := handler(context.Background(), event); err != nil {
t.Fatalf("first create failed: %v", err)
}
mock.ExpectQuery(`INSERT INTO "detection_tasks".*ON CONFLICT \("source_key"\) DO NOTHING`).
WithArgs(args...).
WillReturnRows(sqlmock.NewRows([]string{"id"}))
if err := handler(context.Background(), event); err != nil {
t.Fatalf("duplicate create failed: %v", err)
}
}