package oltp
import (
"strings"
"testing"
)
// The newest pair for a tip is the one its checks are read from,
// whichever target it merged into, and another tip's pairs never
// answer for it.
func TestLatestPairForATip(t *testing.T) {
db := testDB(t)
tip, other := strings.Repeat("a", 64), strings.Repeat("a", 63)+"b"
t1, t2 := strings.Repeat("1", 64), strings.Repeat("2", 64)
if got, err := db.LatestPairFor("r", tip); err != nil || got != "" {
t.Fatalf("FAIL: no runs answered %q, %v", got, err)
}
add := func(id, pair string) {
t.Helper()
if err := db.AddCIRun(CIRun{RunID: id, Repo: "r", Checkin: pair, Pair: pair, Workflow: "w", Pipeline: "p", Status: "running"}); err != nil {
t.Fatal(err)
}
}
add("1", "pair:"+tip+":"+t1)
add("2", "pair:"+other+":"+t2)
if got, _ := db.LatestPairFor("r", tip); got != "pair:"+tip+":"+t1 {
t.Fatalf("FAIL: latest pair = %q, want the tip's own", got)
}
if _, err := db.w.Exec(`UPDATE ci_run SET started = '2000-01-01T00:00:00Z' WHERE run_id = '1'`); err != nil {
t.Fatal(err)
}
add("3", "pair:"+tip+":"+t2)
if got, _ := db.LatestPairFor("r", tip); got != "pair:"+tip+":"+t2 {
t.Fatalf("FAIL: latest pair = %q, want the newer target's", got)
}
t.Log("ok: the newest pair for the tip, never another tip's")
}