package oltp
import "testing"
// A run is found by any prefix of its id that names it alone, however
// many runs came after it, and never across repositories.
func TestARunIsFoundByItsPrefix(t *testing.T) {
db := testDB(t)
add := func(id, repo string) {
t.Helper()
if err := db.AddCIRun(CIRun{RunID: id, Repo: repo, Checkin: "c", Workflow: "w",
Pipeline: "ci/a.json", Status: "completed"}); err != nil {
t.Fatal(err)
}
}
add("1b82c372a6e6aaaa", "r")
add("1b82c372ffff0000", "r")
add("9f00aa00", "other")
// A check-in inheriting a pair run's verdict: its own row, named
// after the run it inherits.
add("1b82c372a6e6aaaa@c0ffee", "r")
add("77aa00@beef", "r")
for _, tc := range []struct {
prefix, want string
ambiguous bool
}{
{"1b82c372a6e6", "1b82c372a6e6aaaa", false},
{"1b82c372a6e6aaaa", "1b82c372a6e6aaaa", false},
{"1b82c372", "", true},
{"9f00", "", false}, // another repository's run
{"", "", false},
{"zz", "", false},
{"1b82c372a6e6aaaa@c0ffee", "1b82c372a6e6aaaa@c0ffee", false}, // the inheritance row by its whole id
{"77aa", "77aa00@beef", false}, // no run that ran matches
{"1b82c372a6e6aaaa@", "1b82c372a6e6aaaa@c0ffee", false},
} {
run, ok, amb, err := db.CIRunByPrefix("r", tc.prefix)
if err != nil {
t.Fatal(err)
}
if got := map[bool]string{true: run.RunID}[ok]; got != tc.want || amb != tc.ambiguous {
t.Errorf("FAIL: prefix %q found %q (ambiguous %v), want %q (%v)", tc.prefix, got, amb, tc.want, tc.ambiguous)
}
}
}