2020-01-11 03:45:17 +01:00
package persistence
import (
2020-02-01 03:09:23 +01:00
"context"
2020-02-03 02:33:58 +01:00
"path/filepath"
2020-01-11 03:45:17 +01:00
"testing"
2020-01-28 14:22:17 +01:00
_ "github.com/mattn/go-sqlite3"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/conf"
2020-01-28 14:22:17 +01:00
"github.com/navidrome/navidrome/db"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
2020-05-13 22:49:55 +02:00
"github.com/navidrome/navidrome/model/request"
2020-01-26 23:10:13 +01:00
"github.com/navidrome/navidrome/tests"
2022-07-26 22:47:16 +02:00
. "github.com/onsi/ginkgo/v2"
2020-01-11 03:45:17 +01:00
. "github.com/onsi/gomega"
2023-12-09 19:52:17 +01:00
"github.com/pocketbase/dbx"
2020-01-11 03:45:17 +01:00
)
func TestPersistence ( t * testing . T ) {
2020-01-26 23:10:13 +01:00
tests . Init ( t , true )
2020-01-28 14:22:17 +01:00
2020-01-31 22:03:30 +01:00
//os.Remove("./test-123.db")
2020-05-22 21:23:42 +02:00
//conf.Server.DbPath = "./test-123.db"
2020-02-28 20:16:15 +01:00
conf . Server . DbPath = "file::memory:?cache=shared"
2023-04-04 16:30:28 +02:00
db . Init ( )
2021-07-16 17:03:28 +02:00
log . SetLevel ( log . LevelError )
2020-01-11 03:45:17 +01:00
RegisterFailHandler ( Fail )
2020-01-15 00:23:29 +01:00
RunSpecs ( t , "Persistence Suite" )
2020-01-11 03:45:17 +01:00
}
2020-01-15 00:23:29 +01:00
2023-12-09 19:52:17 +01:00
func getDBXBuilder ( ) * dbx . DB {
return dbx . NewFromDB ( db . Db ( ) , db . Driver )
}
2021-07-16 17:03:28 +02:00
var (
genreElectronic = model . Genre { ID : "gn-1" , Name : "Electronic" }
genreRock = model . Genre { ID : "gn-2" , Name : "Rock" }
testGenres = model . Genres { genreElectronic , genreRock }
)
2020-02-01 03:09:23 +01:00
var (
2020-04-20 04:40:24 +02:00
artistKraftwerk = model . Artist { ID : "2" , Name : "Kraftwerk" , AlbumCount : 1 , FullText : " kraftwerk" }
artistBeatles = model . Artist { ID : "3" , Name : "The Beatles" , AlbumCount : 2 , FullText : " beatles the" }
2020-02-01 03:09:23 +01:00
testArtists = model . Artists {
artistKraftwerk ,
artistBeatles ,
}
)
2020-01-15 23:49:09 +01:00
2020-02-01 03:09:23 +01:00
var (
2023-12-09 01:29:16 +01:00
albumSgtPeppers = model . Album { ID : "101" , Name : "Sgt Peppers" , Artist : "The Beatles" , OrderAlbumName : "sgt peppers" , AlbumArtistID : "3" , Genre : "Rock" , Genres : model . Genres { genreRock } , EmbedArtPath : P ( "/beatles/1/sgt/a day.mp3" ) , SongCount : 1 , MaxYear : 1967 , FullText : " beatles peppers sgt the" , Discs : model . Discs { } }
albumAbbeyRoad = model . Album { ID : "102" , Name : "Abbey Road" , Artist : "The Beatles" , OrderAlbumName : "abbey road" , AlbumArtistID : "3" , Genre : "Rock" , Genres : model . Genres { genreRock } , EmbedArtPath : P ( "/beatles/1/come together.mp3" ) , SongCount : 1 , MaxYear : 1969 , FullText : " abbey beatles road the" , Discs : model . Discs { } }
albumRadioactivity = model . Album { ID : "103" , Name : "Radioactivity" , Artist : "Kraftwerk" , OrderAlbumName : "radioactivity" , AlbumArtistID : "2" , Genre : "Electronic" , Genres : model . Genres { genreElectronic , genreRock } , EmbedArtPath : P ( "/kraft/radio/radio.mp3" ) , SongCount : 2 , FullText : " kraftwerk radioactivity" , Discs : model . Discs { } }
2020-02-01 03:09:23 +01:00
testAlbums = model . Albums {
albumSgtPeppers ,
albumAbbeyRoad ,
albumRadioactivity ,
}
)
2020-01-15 00:23:29 +01:00
2020-02-01 03:09:23 +01:00
var (
2021-07-16 17:03:28 +02:00
songDayInALife = model . MediaFile { ID : "1001" , Title : "A Day In A Life" , ArtistID : "3" , Artist : "The Beatles" , AlbumID : "101" , Album : "Sgt Peppers" , Genre : "Rock" , Genres : model . Genres { genreRock } , Path : P ( "/beatles/1/sgt/a day.mp3" ) , FullText : " a beatles day in life peppers sgt the" }
songComeTogether = model . MediaFile { ID : "1002" , Title : "Come Together" , ArtistID : "3" , Artist : "The Beatles" , AlbumID : "102" , Album : "Abbey Road" , Genre : "Rock" , Genres : model . Genres { genreRock } , Path : P ( "/beatles/1/come together.mp3" ) , FullText : " abbey beatles come road the together" }
songRadioactivity = model . MediaFile { ID : "1003" , Title : "Radioactivity" , ArtistID : "2" , Artist : "Kraftwerk" , AlbumID : "103" , Album : "Radioactivity" , Genre : "Electronic" , Genres : model . Genres { genreElectronic } , Path : P ( "/kraft/radio/radio.mp3" ) , FullText : " kraftwerk radioactivity" }
2023-12-28 03:14:54 +01:00
songAntenna = model . MediaFile { ID : "1004" , Title : "Antenna" , ArtistID : "2" , Artist : "Kraftwerk" ,
AlbumID : "103" , Genre : "Electronic" , Genres : model . Genres { genreElectronic , genreRock } ,
Path : P ( "/kraft/radio/antenna.mp3" ) , FullText : " antenna kraftwerk" ,
RgAlbumGain : 1.0 , RgAlbumPeak : 2.0 , RgTrackGain : 3.0 , RgTrackPeak : 4.0 ,
}
testSongs = model . MediaFiles {
2020-02-01 03:09:23 +01:00
songDayInALife ,
songComeTogether ,
songRadioactivity ,
songAntenna ,
}
)
2020-01-28 14:22:17 +01:00
2023-01-15 21:11:37 +01:00
var (
radioWithoutHomePage = model . Radio { ID : "1235" , StreamUrl : "https://example.com:8000/1/stream.mp3" , HomePageUrl : "" , Name : "No Homepage" }
radioWithHomePage = model . Radio { ID : "5010" , StreamUrl : "https://example.com/stream.mp3" , Name : "Example Radio" , HomePageUrl : "https://example.com" }
testRadios = model . Radios { radioWithoutHomePage , radioWithHomePage }
)
2020-01-28 14:22:17 +01:00
var (
2021-10-17 04:44:44 +02:00
plsBest model . Playlist
plsCool model . Playlist
testPlaylists [ ] * model . Playlist
2020-01-28 14:22:17 +01:00
)
2020-01-18 07:56:17 +01:00
func P ( path string ) string {
2020-02-03 02:33:58 +01:00
return filepath . FromSlash ( path )
2020-01-18 07:56:17 +01:00
}
2022-07-26 22:47:16 +02:00
// Initialize test DB
// TODO Load this data setup from file(s)
var _ = BeforeSuite ( func ( ) {
2023-12-09 19:52:17 +01:00
conn := getDBXBuilder ( )
2022-07-26 22:47:16 +02:00
ctx := log . NewContext ( context . TODO ( ) )
2023-01-15 21:11:37 +01:00
user := model . User { ID : "userid" , UserName : "userid" , IsAdmin : true }
2022-07-26 22:47:16 +02:00
ctx = request . WithUser ( ctx , user )
2023-12-09 19:52:17 +01:00
ur := NewUserRepository ( ctx , conn )
2022-07-26 22:47:16 +02:00
err := ur . Put ( & user )
if err != nil {
panic ( err )
}
2021-10-30 04:55:28 +02:00
2023-12-09 19:52:17 +01:00
gr := NewGenreRepository ( ctx , conn )
2022-07-26 22:47:16 +02:00
for i := range testGenres {
g := testGenres [ i ]
err := gr . Put ( & g )
2021-10-30 04:55:28 +02:00
if err != nil {
panic ( err )
}
2022-07-26 22:47:16 +02:00
}
2021-07-16 17:03:28 +02:00
2023-12-09 19:52:17 +01:00
mr := NewMediaFileRepository ( ctx , conn )
2022-07-26 22:47:16 +02:00
for i := range testSongs {
s := testSongs [ i ]
err := mr . Put ( & s )
if err != nil {
panic ( err )
2020-01-28 14:22:17 +01:00
}
2022-07-26 22:47:16 +02:00
}
2020-02-01 03:09:23 +01:00
2023-12-09 19:52:17 +01:00
alr := NewAlbumRepository ( ctx , conn ) . ( * albumRepository )
2022-07-26 22:47:16 +02:00
for i := range testAlbums {
a := testAlbums [ i ]
err := alr . Put ( & a )
if err != nil {
2020-02-01 03:09:23 +01:00
panic ( err )
}
2022-07-26 22:47:16 +02:00
}
2020-02-01 03:09:23 +01:00
2023-12-09 19:52:17 +01:00
arr := NewArtistRepository ( ctx , conn )
2022-07-26 22:47:16 +02:00
for i := range testArtists {
a := testArtists [ i ]
err := arr . Put ( & a )
if err != nil {
2020-02-01 03:09:23 +01:00
panic ( err )
}
2022-07-26 22:47:16 +02:00
}
2020-02-01 03:09:23 +01:00
2023-12-09 19:52:17 +01:00
rar := NewRadioRepository ( ctx , conn )
2023-01-15 21:11:37 +01:00
for i := range testRadios {
r := testRadios [ i ]
err := rar . Put ( & r )
if err != nil {
panic ( err )
}
}
2022-07-26 22:47:16 +02:00
plsBest = model . Playlist {
Name : "Best" ,
Comment : "No Comments" ,
OwnerID : "userid" ,
OwnerName : "userid" ,
Public : true ,
SongCount : 2 ,
}
plsBest . AddTracks ( [ ] string { "1001" , "1003" } )
plsCool = model . Playlist { Name : "Cool" , OwnerID : "userid" , OwnerName : "userid" }
plsCool . AddTracks ( [ ] string { "1004" } )
testPlaylists = [ ] * model . Playlist { & plsBest , & plsCool }
2023-12-09 19:52:17 +01:00
pr := NewPlaylistRepository ( ctx , conn )
2022-07-26 22:47:16 +02:00
for i := range testPlaylists {
err := pr . Put ( testPlaylists [ i ] )
if err != nil {
2020-02-01 03:09:23 +01:00
panic ( err )
}
2022-07-26 22:47:16 +02:00
}
// Prepare annotations
if err := arr . SetStar ( true , artistBeatles . ID ) ; err != nil {
panic ( err )
}
ar , _ := arr . Get ( artistBeatles . ID )
artistBeatles . Starred = true
artistBeatles . StarredAt = ar . StarredAt
testArtists [ 1 ] = artistBeatles
if err := alr . SetStar ( true , albumRadioactivity . ID ) ; err != nil {
panic ( err )
}
al , _ := alr . Get ( albumRadioactivity . ID )
albumRadioactivity . Starred = true
albumRadioactivity . StarredAt = al . StarredAt
testAlbums [ 2 ] = albumRadioactivity
if err := mr . SetStar ( true , songComeTogether . ID ) ; err != nil {
panic ( err )
}
mf , _ := mr . Get ( songComeTogether . ID )
songComeTogether . Starred = true
songComeTogether . StarredAt = mf . StarredAt
testSongs [ 1 ] = songComeTogether
2020-01-15 00:23:29 +01:00
} )