45 lines
1.3 KiB
Go
45 lines
1.3 KiB
Go
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)
|
|
}
|
|
}
|