feat(api): add dbq→ref projection helpers
This commit is contained in:
@@ -4,6 +4,8 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
|
||||
"git.fabledsword.com/bvandeusen/minstrel/internal/db/dbq"
|
||||
)
|
||||
|
||||
func TestUUIDToString(t *testing.T) {
|
||||
@@ -73,3 +75,77 @@ func TestCoverURLAndStreamURL(t *testing.T) {
|
||||
t.Errorf("streamURL = %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestArtistRefFrom(t *testing.T) {
|
||||
var id pgtype.UUID
|
||||
_ = id.Scan("00112233-4455-6677-8899-aabbccddeeff")
|
||||
a := dbq.Artist{ID: id, Name: "Beatles", SortName: "Beatles"}
|
||||
got := artistRefFrom(a, 7)
|
||||
if got.ID != "00112233-4455-6677-8899-aabbccddeeff" {
|
||||
t.Errorf("ID = %q", got.ID)
|
||||
}
|
||||
if got.Name != "Beatles" || got.AlbumCount != 7 {
|
||||
t.Errorf("ref = %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlbumRefFrom(t *testing.T) {
|
||||
var aid, artID pgtype.UUID
|
||||
_ = aid.Scan("00000000-0000-0000-0000-000000000001")
|
||||
_ = artID.Scan("00000000-0000-0000-0000-000000000002")
|
||||
var rel pgtype.Date
|
||||
_ = rel.Scan("1969-09-26")
|
||||
a := dbq.Album{ID: aid, Title: "Abbey Road", ArtistID: artID, ReleaseDate: rel}
|
||||
got := albumRefFrom(a, "Beatles", 17, 2820)
|
||||
if got.Title != "Abbey Road" || got.ArtistName != "Beatles" {
|
||||
t.Errorf("ref = %+v", got)
|
||||
}
|
||||
if got.TrackCount != 17 || got.DurationSec != 2820 {
|
||||
t.Errorf("counts = %+v", got)
|
||||
}
|
||||
if got.Year != 1969 {
|
||||
t.Errorf("year = %d", got.Year)
|
||||
}
|
||||
if got.CoverURL != "/api/albums/00000000-0000-0000-0000-000000000001/cover" {
|
||||
t.Errorf("cover_url = %q", got.CoverURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackRefFrom(t *testing.T) {
|
||||
var tid, aid, artID pgtype.UUID
|
||||
_ = tid.Scan("00000000-0000-0000-0000-000000000010")
|
||||
_ = aid.Scan("00000000-0000-0000-0000-000000000001")
|
||||
_ = artID.Scan("00000000-0000-0000-0000-000000000002")
|
||||
trackNum := int32(3)
|
||||
discNum := int32(1)
|
||||
t2 := dbq.Track{
|
||||
ID: tid, Title: "Something", AlbumID: aid, ArtistID: artID,
|
||||
TrackNumber: &trackNum, DiscNumber: &discNum,
|
||||
DurationMs: 183_000,
|
||||
}
|
||||
got := trackRefFrom(t2, "Abbey Road", "Beatles")
|
||||
if got.Title != "Something" || got.AlbumTitle != "Abbey Road" || got.ArtistName != "Beatles" {
|
||||
t.Errorf("ref = %+v", got)
|
||||
}
|
||||
if got.TrackNumber != 3 || got.DiscNumber != 1 {
|
||||
t.Errorf("positions = %+v", got)
|
||||
}
|
||||
if got.DurationSec != 183 {
|
||||
t.Errorf("duration = %d, want 183", got.DurationSec)
|
||||
}
|
||||
if got.StreamURL != "/api/tracks/00000000-0000-0000-0000-000000000010/stream" {
|
||||
t.Errorf("stream_url = %q", got.StreamURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTrackRefFromNilPositions(t *testing.T) {
|
||||
var tid, aid, artID pgtype.UUID
|
||||
_ = tid.Scan("00000000-0000-0000-0000-000000000010")
|
||||
_ = aid.Scan("00000000-0000-0000-0000-000000000001")
|
||||
_ = artID.Scan("00000000-0000-0000-0000-000000000002")
|
||||
t2 := dbq.Track{ID: tid, AlbumID: aid, ArtistID: artID, DurationMs: 1000}
|
||||
got := trackRefFrom(t2, "A", "B")
|
||||
if got.TrackNumber != 0 || got.DiscNumber != 0 {
|
||||
t.Errorf("nil positions should be 0, got %+v", got)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user