package backup import ( "slices" "strings" "testing" ) func everyListedTable() []string { var names []string for _, t := range Tables { names = append(names, t.Name) } for name := range Excluded { names = append(names, name) } return names } // Coverage holds the schema against the two lists: every table is backed up // or excluded with a reason, and every listed table exists. A migration that // adds or drops a table without updating the lists fails here. func TestCoverage(t *testing.T) { without := func(drop ...string) []string { return slices.DeleteFunc(everyListedTable(), func(n string) bool { return slices.Contains(drop, n) }) } for _, tc := range []struct { name string schema []string want []string // a prefix of each problem, in order }{ {"the schema matches the lists", everyListedTable(), nil}, {"a table in neither list", append(everyListedTable(), "new_table"), []string{"new_table: neither"}}, {"listed tables the schema lacks", without("playlists", "scan_runs"), []string{"playlists", "scan_runs"}}, } { problems := Coverage(tc.schema) slices.Sort(problems) ok := len(problems) == len(tc.want) for i := 0; ok && i < len(problems); i++ { ok = strings.HasPrefix(problems[i], tc.want[i]) } if !ok { t.Errorf("%s: problems = %q, want ones starting %q", tc.name, problems, tc.want) } } } func TestEveryExclusionHasAReason(t *testing.T) { for name, why := range Excluded { if strings.TrimSpace(why) == "" { t.Errorf("%s is excluded without a reason", name) } if carried(name) { t.Errorf("%s is both backed up and excluded", name) } } }