2020-01-10 21:45:17 -05:00
package persistence
import (
2020-01-31 21:09:23 -05:00
"context"
2020-02-02 20:33:58 -05:00
"path/filepath"
2020-01-10 21:45:17 -05:00
"testing"
2020-01-28 08:22:17 -05:00
_ "github.com/mattn/go-sqlite3"
2020-01-23 19:44:08 -05:00
"github.com/navidrome/navidrome/conf"
2020-01-28 08:22:17 -05:00
"github.com/navidrome/navidrome/db"
2020-01-23 19:44:08 -05:00
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/model"
2020-05-13 16:49:55 -04:00
"github.com/navidrome/navidrome/model/request"
2020-01-26 17:10:13 -05:00
"github.com/navidrome/navidrome/tests"
2022-07-26 16:47:16 -04:00
. "github.com/onsi/ginkgo/v2"
2020-01-10 21:45:17 -05:00
. "github.com/onsi/gomega"
)
func TestPersistence ( t * testing . T ) {
2020-01-26 17:10:13 -05:00
tests . Init ( t , true )
2020-01-28 08:22:17 -05:00
2020-01-31 16:03:30 -05:00
//os.Remove("./test-123.db")
2020-05-22 15:23:42 -04:00
//conf.Server.DbPath = "./test-123.db"
2024-08-03 17:37:21 +00:00
conf . Server . DbPath = "file::memory:?cache=shared&_foreign_keys=on"
2024-05-09 07:42:40 -04:00
defer db . Init ( ) ( )
2024-09-20 11:22:37 -04:00
log . SetLevel ( log . LevelFatal )
2020-01-10 21:45:17 -05:00
RegisterFailHandler ( Fail )
2020-01-14 18:23:29 -05:00
RunSpecs ( t , "Persistence Suite" )
2020-01-10 21:45:17 -05:00
}
2020-01-14 18:23:29 -05:00
2021-07-16 11:03:28 -04:00
var (
genreElectronic = model . Genre { ID : "gn-1" , Name : "Electronic" }
genreRock = model . Genre { ID : "gn-2" , Name : "Rock" }
testGenres = model . Genres { genreElectronic , genreRock }
)
2020-01-31 21:09:23 -05:00
var (
2024-09-20 02:44:29 +09:00
artistKraftwerk = model . Artist { ID : "2" , Name : "Kraftwerk" , OrderArtistName : "kraftwerk" , AlbumCount : 1 , FullText : " kraftwerk" }
artistBeatles = model . Artist { ID : "3" , Name : "The Beatles" , OrderArtistName : "beatles" , AlbumCount : 2 , FullText : " beatles the" }
2020-01-31 21:09:23 -05:00
testArtists = model . Artists {
artistKraftwerk ,
artistBeatles ,
}
)
2020-01-15 17:49:09 -05:00
2020-01-31 21:09:23 -05:00
var (
2024-05-07 17:52:13 +02:00
albumSgtPeppers = model . Album { LibraryID : 1 , 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 { LibraryID : 1 , 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 { LibraryID : 1 , 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-01-31 21:09:23 -05:00
testAlbums = model . Albums {
albumSgtPeppers ,
albumAbbeyRoad ,
albumRadioactivity ,
}
)
2020-01-14 18:23:29 -05:00
2020-01-31 21:09:23 -05:00
var (
2024-05-07 17:52:13 +02:00
songDayInALife = model . MediaFile { LibraryID : 1 , 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 { LibraryID : 1 , 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 { LibraryID : 1 , 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" }
songAntenna = model . MediaFile { LibraryID : 1 , ID : "1004" , Title : "Antenna" , ArtistID : "2" , Artist : "Kraftwerk" ,
2023-12-27 21:14:54 -05:00
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-01-31 21:09:23 -05:00
songDayInALife ,
songComeTogether ,
songRadioactivity ,
songAntenna ,
}
)
2020-01-28 08:22:17 -05:00
2023-01-15 20:11:37 +00: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 08:22:17 -05:00
var (
2021-10-16 22:44:44 -04:00
plsBest model . Playlist
plsCool model . Playlist
testPlaylists [ ] * model . Playlist
2020-01-28 08:22:17 -05:00
)
2024-08-03 17:37:21 +00:00
var (
adminUser = model . User { ID : "userid" , UserName : "userid" , Name : "admin" , Email : "admin@email.com" , IsAdmin : true }
regularUser = model . User { ID : "2222" , UserName : "regular-user" , Name : "Regular User" , Email : "regular@example.com" }
testUsers = model . Users { adminUser , regularUser }
)
2020-01-18 01:56:17 -05:00
func P ( path string ) string {
2020-02-02 20:33:58 -05:00
return filepath . FromSlash ( path )
2020-01-18 01:56:17 -05:00
}
2022-07-26 16:47:16 -04:00
// Initialize test DB
// TODO Load this data setup from file(s)
var _ = BeforeSuite ( func ( ) {
2024-05-21 17:19:41 -04:00
conn := NewDBXBuilder ( db . Db ( ) )
2022-07-26 16:47:16 -04:00
ctx := log . NewContext ( context . TODO ( ) )
2024-08-03 17:37:21 +00:00
ctx = request . WithUser ( ctx , adminUser )
2022-07-26 16:47:16 -04:00
2023-12-09 13:52:17 -05:00
ur := NewUserRepository ( ctx , conn )
2024-08-03 17:37:21 +00:00
for i := range testUsers {
err := ur . Put ( & testUsers [ i ] )
if err != nil {
panic ( err )
}
2022-07-26 16:47:16 -04:00
}
2021-10-29 22:55:28 -04:00
2023-12-09 13:52:17 -05:00
gr := NewGenreRepository ( ctx , conn )
2022-07-26 16:47:16 -04:00
for i := range testGenres {
g := testGenres [ i ]
err := gr . Put ( & g )
2021-10-29 22:55:28 -04:00
if err != nil {
panic ( err )
}
2022-07-26 16:47:16 -04:00
}
2021-07-16 11:03:28 -04:00
2023-12-09 13:52:17 -05:00
mr := NewMediaFileRepository ( ctx , conn )
2022-07-26 16:47:16 -04:00
for i := range testSongs {
s := testSongs [ i ]
err := mr . Put ( & s )
if err != nil {
panic ( err )
2020-01-28 08:22:17 -05:00
}
2022-07-26 16:47:16 -04:00
}
2020-01-31 21:09:23 -05:00
2023-12-09 13:52:17 -05:00
alr := NewAlbumRepository ( ctx , conn ) . ( * albumRepository )
2022-07-26 16:47:16 -04:00
for i := range testAlbums {
a := testAlbums [ i ]
err := alr . Put ( & a )
if err != nil {
2020-01-31 21:09:23 -05:00
panic ( err )
}
2022-07-26 16:47:16 -04:00
}
2020-01-31 21:09:23 -05:00
2023-12-09 13:52:17 -05:00
arr := NewArtistRepository ( ctx , conn )
2022-07-26 16:47:16 -04:00
for i := range testArtists {
a := testArtists [ i ]
err := arr . Put ( & a )
if err != nil {
2020-01-31 21:09:23 -05:00
panic ( err )
}
2022-07-26 16:47:16 -04:00
}
2020-01-31 21:09:23 -05:00
2023-12-09 13:52:17 -05:00
rar := NewRadioRepository ( ctx , conn )
2023-01-15 20:11:37 +00:00
for i := range testRadios {
r := testRadios [ i ]
err := rar . Put ( & r )
if err != nil {
panic ( err )
}
}
2022-07-26 16:47:16 -04: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 13:52:17 -05:00
pr := NewPlaylistRepository ( ctx , conn )
2022-07-26 16:47:16 -04:00
for i := range testPlaylists {
err := pr . Put ( testPlaylists [ i ] )
if err != nil {
2020-01-31 21:09:23 -05:00
panic ( err )
}
2022-07-26 16:47:16 -04: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-14 18:23:29 -05:00
} )