Migrating hosting from Codeberg to git.kmalloc.org. Renames the module path in go.mod, every internal import, and the OCI image source label in the Dockerfile. Build and full test suite verified green after the rename.
228 lines
8.4 KiB
Go
228 lines
8.4 KiB
Go
package storage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"reflect"
|
|
"sort"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/uptrace/bun"
|
|
|
|
"git.kmalloc.org/squid/sonda/internal/library"
|
|
"git.kmalloc.org/squid/sonda/internal/listening"
|
|
"git.kmalloc.org/squid/sonda/internal/spotifyimport"
|
|
)
|
|
|
|
func TestRecordManyStoresTheListensOnceAndCountsWhatIsNew(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
u, err := NewUserRepo(db).CreateUser(ctx, "alice", "h", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var track int64
|
|
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('t') RETURNING id").Scan(&track); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewListeningRepo(db)
|
|
ev := func(key string, at int64) listening.Event {
|
|
return listening.Event{UserID: u.ID, TrackID: track, Type: listening.Complete, OccurredAt: time.Unix(at, 0), DurationPlayedMs: 1000, IdempotencyKey: key}
|
|
}
|
|
batch := []listening.Event{ev("a", 100), ev("b", 200), ev("a", 100)} // the same key twice in one batch
|
|
n, err := repo.RecordMany(ctx, u.ID, batch)
|
|
if err != nil || n != 2 {
|
|
t.Fatalf("RecordMany = %d, %v, want 2 new", n, err)
|
|
}
|
|
n, err = repo.RecordMany(ctx, u.ID, []listening.Event{ev("a", 100), ev("c", 300)})
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("second RecordMany = %d, %v, want only the new one", n, err)
|
|
}
|
|
if got := count(t, db, "SELECT COUNT(*) FROM listening_events WHERE user_id = ?", u.ID); got != 3 {
|
|
t.Errorf("%d events stored, want 3", got)
|
|
}
|
|
if n, err := repo.RecordMany(ctx, u.ID, nil); err != nil || n != 0 {
|
|
t.Errorf("an empty batch = %d, %v", n, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestRecordManyHandlesMoreThanOneStatementHolds(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
u, err := NewUserRepo(db).CreateUser(ctx, "alice", "h", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var track int64
|
|
if err := db.QueryRow("INSERT INTO tracks (title) VALUES ('t') RETURNING id").Scan(&track); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var events []listening.Event
|
|
for i := 0; i < 5000; i++ {
|
|
events = append(events, listening.Event{UserID: u.ID, TrackID: track, Type: listening.Play, OccurredAt: time.Unix(int64(1000+i), 0), IdempotencyKey: fmt.Sprintf("k%d", i)})
|
|
}
|
|
n, err := NewListeningRepo(db).RecordMany(ctx, u.ID, events)
|
|
if err != nil || n != 5000 {
|
|
t.Fatalf("RecordMany = %d, %v, want 5000", n, err)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestCandidatesAreTheSongsThatCanBePlayed(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
repo := NewLibraryRepo(db)
|
|
lib := mkLib(t, repo, "music")
|
|
a := parsed("a/1.flac", "Under Pressure", "Hot Space", "Queen", "David Bowie")
|
|
a.DurationMs = 248000
|
|
b := parsed("a/2.flac", "Gone", "Album", "Radiohead")
|
|
c := parsed("a/3.flac", "Loose", "", "Björk")
|
|
c.Album, c.AlbumArtist = "", ""
|
|
for _, f := range []library.ParsedFile{a, b, c} {
|
|
if _, err := repo.SaveFile(ctx, lib, f, 0); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if _, err := db.Exec("UPDATE audio_files SET missing_at = 5 WHERE rel_path = 'a/2.flac'"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := NewCatalogRepo(db).Candidates(ctx)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(got) != 2 {
|
|
t.Fatalf("%d candidates, want 2 (the one with no file left is not there): %+v", len(got), got)
|
|
}
|
|
by := map[string]int{}
|
|
for i, g := range got {
|
|
by[g.Title] = i
|
|
}
|
|
up := got[by["Under Pressure"]]
|
|
artists := append([]string(nil), up.Artists...)
|
|
sort.Strings(artists)
|
|
if up.Album != "Hot Space" || up.DurationMs != 248000 || len(artists) != 2 || artists[0] != "David Bowie" || artists[1] != "Queen" || up.TrackID == 0 {
|
|
t.Errorf("Under Pressure = %+v", up)
|
|
}
|
|
if l := got[by["Loose"]]; l.Album != "" || len(l.Artists) != 1 || l.Artists[0] != "Björk" {
|
|
t.Errorf("a song with no album = %+v", l)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestSearchKeyIsExported(t *testing.T) {
|
|
if SearchKey("Björk") != "bjork" {
|
|
t.Errorf("SearchKey(Björk) = %q", SearchKey("Björk"))
|
|
}
|
|
}
|
|
|
|
func TestSpotifyImportsAreKeptPerUserNewestFirst(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
users := NewUserRepo(db)
|
|
alice, err := users.CreateUser(ctx, "alice", "h", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
bob, err := users.CreateUser(ctx, "bob", "h", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewSpotifyImportRepo(db)
|
|
rep := spotifyimport.Report{
|
|
History: spotifyimport.History{Entries: 10, Matched: 4, Imported: 3, AlreadyThere: 1, NotFound: 6, Ignored: 2},
|
|
Playlists: []spotifyimport.PlaylistResult{{Name: "Road trip", Tracks: 3, Matched: 1, Status: "created"}},
|
|
Missing: []spotifyimport.Missing{{Artist: "Blur", Count: 4}},
|
|
MissingTotal: 7,
|
|
}
|
|
first, err := repo.Save(ctx, alice.ID, time.Unix(1000, 0), rep)
|
|
if err != nil || first == 0 {
|
|
t.Fatalf("Save = %d, %v", first, err)
|
|
}
|
|
later := rep
|
|
later.History.Imported = 9
|
|
second, err := repo.Save(ctx, alice.ID, time.Unix(2000, 0), later)
|
|
if err != nil || second == first {
|
|
t.Fatalf("second Save = %d, %v", second, err)
|
|
}
|
|
if _, err := repo.Save(ctx, bob.ID, time.Unix(3000, 0), rep); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
runs, total, err := repo.List(ctx, alice.ID, 10, 0)
|
|
if err != nil || total != 2 || len(runs) != 2 {
|
|
t.Fatalf("List = %d runs, total %d, %v, want her two", len(runs), total, err)
|
|
}
|
|
if runs[0].ID != second || runs[0].Report.History.Imported != 9 || runs[0].At.Unix() != 2000 {
|
|
t.Errorf("newest = %+v, want the second import", runs[0])
|
|
}
|
|
if !reflect.DeepEqual(runs[1].Report, rep) || runs[1].At.Unix() != 1000 {
|
|
t.Errorf("oldest = %+v, want the report as it was saved", runs[1])
|
|
}
|
|
page, total, err := repo.List(ctx, alice.ID, 1, 1)
|
|
if err != nil || total != 2 || len(page) != 1 || page[0].ID != first {
|
|
t.Errorf("second page = %+v, total %d, %v, want the first import", page, total, err)
|
|
}
|
|
none, total, err := repo.List(ctx, alice.ID, 10, 5)
|
|
if err != nil || total != 2 || len(none) != 0 {
|
|
t.Errorf("past the end = %d runs, total %d, %v", len(none), total, err)
|
|
}
|
|
|
|
if _, err := db.ExecContext(ctx, "DELETE FROM users WHERE id = ?", alice.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if runs, total, _ := repo.List(ctx, alice.ID, 10, 0); total != 0 || len(runs) != 0 {
|
|
t.Errorf("after the account is gone: %d runs, total %d, want none", len(runs), total)
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestOutsidePlaysKeepOnlyTheArtistAndTheTimeAndAreKeptOnce(t *testing.T) {
|
|
forEachBackend(t, func(t *testing.T, db *bun.DB, _ string) {
|
|
ctx := context.Background()
|
|
u, err := NewUserRepo(db).CreateUser(ctx, "alice", "h", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
repo := NewOutsidePlayRepo(db)
|
|
play := func(key, artist string, at int64) spotifyimport.OutsidePlay {
|
|
return spotifyimport.OutsidePlay{Key: key, Artist: artist, PlayedAt: time.Unix(at, 0)}
|
|
}
|
|
n, err := repo.RecordOutside(ctx, u.ID, []spotifyimport.OutsidePlay{play("a", "Björk", 100), play("b", "Blur", 200), play("a", "Björk", 100)})
|
|
if err != nil || n != 2 {
|
|
t.Fatalf("RecordOutside = %d, %v, want 2 new", n, err)
|
|
}
|
|
n, err = repo.RecordOutside(ctx, u.ID, []spotifyimport.OutsidePlay{play("a", "Björk", 100), play("c", "Oasis", 300)})
|
|
if err != nil || n != 1 {
|
|
t.Fatalf("second RecordOutside = %d, %v, want only the new one", n, err)
|
|
}
|
|
var artist, key string
|
|
if err := db.QueryRowContext(ctx, "SELECT artist, artist_key FROM outside_plays WHERE user_id = ? AND idempotency_key = 'a'", u.ID).Scan(&artist, &key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if artist != "Björk" || key != SearchKey("Björk") || key == "" {
|
|
t.Errorf("stored %q, %q, want the name and its search key", artist, key)
|
|
}
|
|
if got := count(t, db, "SELECT COUNT(*) FROM outside_plays WHERE user_id = ?", u.ID); got != 3 {
|
|
t.Errorf("%d plays stored, want 3", got)
|
|
}
|
|
if n, err := repo.RecordOutside(ctx, u.ID, nil); err != nil || n != 0 {
|
|
t.Errorf("an empty batch = %d, %v", n, err)
|
|
}
|
|
// More than one statement holds.
|
|
many := make([]spotifyimport.OutsidePlay, 1200)
|
|
for i := range many {
|
|
many[i] = play(fmt.Sprintf("k%d", i), "Many", int64(1000+i))
|
|
}
|
|
if n, err := repo.RecordOutside(ctx, u.ID, many); err != nil || n != 1200 {
|
|
t.Errorf("many = %d, %v, want 1200", n, err)
|
|
}
|
|
if _, err := db.ExecContext(ctx, "DELETE FROM users WHERE id = ?", u.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := count(t, db, "SELECT COUNT(*) FROM outside_plays"); got != 0 {
|
|
t.Errorf("%d plays left after the account is gone", got)
|
|
}
|
|
})
|
|
}
|