Migration round trip (family idea "Migration CI lane"): - TestMigrationsRoundTrip, in the integration lane, migrates a scratch database to the baseline, then all the way up, then every migration after the baseline down, then up again. - The schema after the down pass must match a database migrated to the baseline alone: columns, indexes, constraints, sequences, enums, functions and triggers. So a down file that leaves something behind fails, as well as one that errors. - The baseline's own down file refuses to run by design, so the round trip stops there. - The test uses golang-migrate as a library, with no migrator binary. - The scratch database is dropped afterwards, and the shared test database is never migrated down. dbtest (family idea "dbtest Pool and Fixtures"): - dbtest.URL skips without a database. It fails instead when MINSTREL_TEST_DATABASE_REQUIRED is set, and the integration lane sets it. - dbtest.On(t, db) gives One/Exec/Count fixtures. The local copies are repointed to them: exec in api, mustExec/mustID in backup, countQueueRows in scrobble, and countTrackSim/countArtistSim in similarity. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
100 lines
3.6 KiB
Go
100 lines
3.6 KiB
Go
package api
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"testing"
|
|
|
|
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
|
)
|
|
|
|
// getArt fetches path and returns the image body, or "" with the status
|
|
// when the answer is not an image.
|
|
func getArt(t *testing.T, h *handlers, path string) (string, int) {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if w.Code != http.StatusOK {
|
|
return "", w.Code
|
|
}
|
|
return w.Body.String(), w.Code
|
|
}
|
|
|
|
// coverURLs returns the cover_url of every artist in the alphabetical
|
|
// listing, by name.
|
|
func coverURLs(t *testing.T, h *handlers) map[string]string {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/artists", nil))
|
|
var got Page[ArtistRef]
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("decode artists: %v body=%s", err, w.Body.String())
|
|
}
|
|
out := map[string]string{}
|
|
for _, a := range got.Items {
|
|
out[a.Name] = a.CoverURL
|
|
}
|
|
return out
|
|
}
|
|
|
|
func detailCoverURL(t *testing.T, h *handlers, id string) string {
|
|
t.Helper()
|
|
w := httptest.NewRecorder()
|
|
newLibraryRouter(h).ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/api/artists/"+id, nil))
|
|
var got ArtistDetail
|
|
if err := json.Unmarshal(w.Body.Bytes(), &got); err != nil {
|
|
t.Fatalf("decode artist: %v body=%s", err, w.Body.String())
|
|
}
|
|
return got.CoverURL
|
|
}
|
|
|
|
// An artist's image is the picture fetched for them, or else their newest
|
|
// album cover; artist listings point at it when there is a picture, and the
|
|
// artist page whenever the artist has albums.
|
|
func TestArtistImage_PictureOrNewestAlbumCover(t *testing.T) {
|
|
h, pool := testHandlers(t)
|
|
truncateLibrary(t, pool)
|
|
dir := t.TempDir()
|
|
write := func(name, body string) string {
|
|
path := filepath.Join(dir, name)
|
|
if err := os.WriteFile(path, []byte(body), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return path
|
|
}
|
|
orchard := seedArtist(t, pool, "Tin Orchard")
|
|
album := seedAlbum(t, pool, orchard.ID, "Paper Boats", 2012)
|
|
dbtest.On(t, pool).Exec(`UPDATE albums SET cover_art_path = $2 WHERE id = $1`, album.ID, write("boats.jpg", "boats"))
|
|
quiet := seedArtist(t, pool, "Quiet Field")
|
|
orchardImage := "/api/artists/" + uuidToString(orchard.ID) + "/image"
|
|
albumCover := "/api/albums/" + uuidToString(album.ID) + "/cover"
|
|
|
|
if got, code := getArt(t, h, orchardImage); got != "boats" {
|
|
t.Errorf("no picture: got %q (status %d), want the album cover", got, code)
|
|
}
|
|
if got := coverURLs(t, h); got["Tin Orchard"] != albumCover || got["Quiet Field"] != "" {
|
|
t.Errorf("no picture: listing cover_url = %v, want the album cover for Tin Orchard and none for Quiet Field", got)
|
|
}
|
|
|
|
dbtest.On(t, pool).Exec(`UPDATE artists SET artist_thumb_path = $2 WHERE id = $1`, orchard.ID, write("thumb.jpg", "thumb"))
|
|
if got, code := getArt(t, h, orchardImage); got != "thumb" {
|
|
t.Errorf("with a picture: got %q (status %d), want the picture", got, code)
|
|
}
|
|
if got := coverURLs(t, h); got["Tin Orchard"] != orchardImage {
|
|
t.Errorf("with a picture: listing cover_url = %q, want %q", got["Tin Orchard"], orchardImage)
|
|
}
|
|
|
|
if got := detailCoverURL(t, h, uuidToString(orchard.ID)); got != orchardImage {
|
|
t.Errorf("artist page cover_url = %q, want %q", got, orchardImage)
|
|
}
|
|
if got := detailCoverURL(t, h, uuidToString(quiet.ID)); got != "" {
|
|
t.Errorf("artist page with no albums: cover_url = %q, want none", got)
|
|
}
|
|
if _, code := getArt(t, h, "/api/artists/"+uuidToString(quiet.ID)+"/image"); code != http.StatusNotFound {
|
|
t.Errorf("no picture and no albums: status %d, want 404", code)
|
|
}
|
|
}
|