package oltp
import "testing"
// A delivery read back by id carries the repository it was recorded
// against — every field RecordDelivery writes, read back.
//
// The redeliver path refuses a delivery whose repo is not the one in
// the URL, because the id names a row the whole forge shares. This
// query used to leave Repo empty, so that guard refused every delivery
// and redeliver never worked at all: "no delivery 291" for an id the
// deliveries list had just returned (b82291eb).
func TestADeliveryReadByIDCarriesItsRepository(t *testing.T) {
db := testDB(t)
db.RecordDelivery(Delivery{Sub: 3, Repo: "dantest-sandbox", Event: "e1", Kind: "ping",
Status: 0, Ms: 12, Err: "dial tcp: no such host", Attempt: 1, Body: `{"id":"e1"}`})
rows, err := db.Deliveries(3, 10)
if err != nil || len(rows) != 1 {
t.Fatalf("fixture: %v %d", err, len(rows))
}
// Both readers, so the list and the by-id lookup cannot disagree
// about a column again.
if rows[0].Repo != "dantest-sandbox" {
t.Fatalf("FAIL: the deliveries list left Repo = %q", rows[0].Repo)
}
got, ok := db.DeliveryByID(rows[0].ID)
if !ok {
t.Fatal("FAIL: the id the list returned is not found by id")
}
if got.Repo != "dantest-sandbox" {
t.Fatalf("FAIL: DeliveryByID left Repo = %q; the redeliver guard will refuse every delivery", got.Repo)
}
if got.Sub != 3 || got.Kind != "ping" || got.Body != `{"id":"e1"}` || got.Err == "" {
t.Fatalf("FAIL: a field was lost on the way back: %+v", got)
}
t.Logf("ok: delivery %d reads back with repo %q", got.ID, got.Repo)
}