Skip to content
quarry Artifact [21ac669cfa]
Login

Artifact [21ac669cfa]

Artifact 21ac669cfa05b2345129a0e74178ef4db2b388c14ae35b90197b41a8583fe2ce:


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")
	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},
	} {
		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)
		}
	}
}