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>
This commit is contained in:
@@ -14,15 +14,17 @@ import (
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"github.com/Fabled-Sword/minstrel/internal/backup"
|
||||
"github.com/Fabled-Sword/minstrel/internal/dbtest"
|
||||
)
|
||||
|
||||
// library writes music files under a new library folder and adds a track for
|
||||
// each. It returns the folder.
|
||||
func library(t *testing.T, pool *pgxpool.Pool, files map[string]string) string {
|
||||
t.Helper()
|
||||
fx := dbtest.On(t, pool)
|
||||
root := t.TempDir()
|
||||
artist := mustID(t, pool, `INSERT INTO artists (name, sort_name) VALUES ('Artist', 'artist') RETURNING id::text`)
|
||||
album := mustID(t, pool, `INSERT INTO albums (title, sort_title, artist_id) VALUES ('Album', 'album', $1) RETURNING id::text`, artist)
|
||||
artist := fx.One(`INSERT INTO artists (name, sort_name) VALUES ('Artist', 'artist') RETURNING id`)
|
||||
album := fx.One(`INSERT INTO albums (title, sort_title, artist_id) VALUES ('Album', 'album', $1) RETURNING id`, artist)
|
||||
for rel, body := range files {
|
||||
path := filepath.Join(root, filepath.FromSlash(rel))
|
||||
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
|
||||
@@ -31,7 +33,7 @@ func library(t *testing.T, pool *pgxpool.Pool, files map[string]string) string {
|
||||
if err := os.WriteFile(path, []byte(body), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustExec(t, pool, `INSERT INTO tracks (title, album_id, artist_id, duration_ms, file_path, file_size, file_format)
|
||||
fx.Exec(`INSERT INTO tracks (title, album_id, artist_id, duration_ms, file_path, file_size, file_format)
|
||||
VALUES ($1, $2, $3, 1000, $4, $5, 'flac')`, rel, album, artist, path, len(body))
|
||||
}
|
||||
return root
|
||||
@@ -52,6 +54,7 @@ func runOnce(t *testing.T, svc *backup.Service) backup.Run {
|
||||
|
||||
func TestTheMusicGoesToTheFolderOnceAndAgainOnlyWhenItChanges_Integration(t *testing.T) {
|
||||
svc, pool := newService(t)
|
||||
fx := dbtest.On(t, pool)
|
||||
ctx := context.Background()
|
||||
lib := library(t, pool, map[string]string{"A/one.flac": "one", "B/two.flac": "second"})
|
||||
svc.Roots = func() []string { return []string{lib} }
|
||||
@@ -83,7 +86,7 @@ func TestTheMusicGoesToTheFolderOnceAndAgainOnlyWhenItChanges_Integration(t *tes
|
||||
}
|
||||
|
||||
// A scan that touched the track but found the same file sends nothing.
|
||||
mustExec(t, pool, `UPDATE tracks SET updated_at = now()`)
|
||||
fx.Exec(`UPDATE tracks SET updated_at = now()`)
|
||||
if touched := runOnce(t, svc); touched.MusicFiles != 0 {
|
||||
t.Errorf("after a scan touched the tracks the run sent %d files, want none", touched.MusicFiles)
|
||||
}
|
||||
@@ -100,7 +103,7 @@ func TestTheMusicGoesToTheFolderOnceAndAgainOnlyWhenItChanges_Integration(t *tes
|
||||
if err := os.Chtimes(path, later, later); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustExec(t, pool, `UPDATE tracks SET file_size = $1 WHERE file_path = $2`, len("one, retagged"), path)
|
||||
fx.Exec(`UPDATE tracks SET file_size = $1 WHERE file_path = $2`, len("one, retagged"), path)
|
||||
if changed := runOnce(t, svc); changed.MusicFiles != 1 {
|
||||
t.Errorf("after one file changed the run sent %d files, want 1", changed.MusicFiles)
|
||||
}
|
||||
@@ -136,6 +139,7 @@ func (b lockedBucket) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func TestTheMusicGoesToTheBucketAndARefusedOverwriteIsKept_Integration(t *testing.T) {
|
||||
svc, pool := newService(t)
|
||||
fx := dbtest.On(t, pool)
|
||||
ctx := context.Background()
|
||||
fake := &fakeBucket{puts: map[string][]byte{}}
|
||||
srv := httptest.NewServer(lockedBucket{fakeBucket: fake})
|
||||
@@ -162,7 +166,7 @@ func TestTheMusicGoesToTheBucketAndARefusedOverwriteIsKept_Integration(t *testin
|
||||
if err := os.WriteFile(path, []byte("one, retagged"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
mustExec(t, pool, `UPDATE tracks SET file_size = $1, updated_at = now() WHERE file_path = $2`, len("one, retagged"), path)
|
||||
fx.Exec(`UPDATE tracks SET file_size = $1, updated_at = now() WHERE file_path = $2`, len("one, retagged"), path)
|
||||
run := runOnce(t, svc)
|
||||
if run.MusicFiles != 0 || run.MusicNote != "" {
|
||||
t.Errorf("a refused overwrite: sent %d, note %q; want nothing sent and no note", run.MusicFiles, run.MusicNote)
|
||||
|
||||
Reference in New Issue
Block a user