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-22 05:01:43 +01:00
"github.com/astaxie/beego/orm"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/conf"
2020-01-28 14:22:17 +01:00
"github.com/deluan/navidrome/db"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
2020-05-13 22:49:55 +02:00
"github.com/deluan/navidrome/model/request"
2020-01-26 23:10:13 +01:00
"github.com/deluan/navidrome/tests"
2020-01-28 14:22:17 +01:00
_ "github.com/mattn/go-sqlite3"
2020-01-11 03:45:17 +01:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
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"
2020-04-26 18:35:26 +02:00
_ = orm . RegisterDataBase ( "default" , db . Driver , conf . Server . DbPath )
2020-02-01 19:46:03 +01:00
db . EnsureLatestVersion ( )
2020-01-15 00:23:29 +01:00
log . SetLevel ( log . LevelCritical )
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
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 (
2020-05-29 02:39:26 +02:00
albumSgtPeppers = model . Album { ID : "101" , Name : "Sgt Peppers" , Artist : "The Beatles" , OrderAlbumName : "sgt peppers" , AlbumArtistID : "3" , Genre : "Rock" , CoverArtId : "1" , CoverArtPath : P ( "/beatles/1/sgt/a day.mp3" ) , SongCount : 1 , MaxYear : 1967 , FullText : " beatles peppers sgt the" }
albumAbbeyRoad = model . Album { ID : "102" , Name : "Abbey Road" , Artist : "The Beatles" , OrderAlbumName : "abbey road" , AlbumArtistID : "3" , Genre : "Rock" , CoverArtId : "2" , CoverArtPath : P ( "/beatles/1/come together.mp3" ) , SongCount : 1 , MaxYear : 1969 , FullText : " abbey beatles road the" }
albumRadioactivity = model . Album { ID : "103" , Name : "Radioactivity" , Artist : "Kraftwerk" , OrderAlbumName : "radioactivity" , AlbumArtistID : "2" , Genre : "Electronic" , CoverArtId : "3" , CoverArtPath : P ( "/kraft/radio/radio.mp3" ) , SongCount : 2 , FullText : " kraftwerk radioactivity" }
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 (
2020-04-20 04:40:24 +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" , 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" , 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" , Path : P ( "/kraft/radio/radio.mp3" ) , FullText : " kraftwerk radioactivity" }
songAntenna = model . MediaFile { ID : "1004" , Title : "Antenna" , ArtistID : "2" , Artist : "Kraftwerk" , AlbumID : "103" , Genre : "Electronic" , Path : P ( "/kraft/radio/antenna.mp3" ) , FullText : " antenna kraftwerk" }
2020-02-01 03:09:23 +01:00
testSongs = model . MediaFiles {
songDayInALife ,
songComeTogether ,
songRadioactivity ,
songAntenna ,
}
)
2020-01-28 14:22:17 +01:00
var (
plsBest = model . Playlist {
2020-05-08 19:57:32 +02:00
Name : "Best" ,
Comment : "No Comments" ,
Owner : "userid" ,
Public : true ,
SongCount : 2 ,
Tracks : model . MediaFiles { { ID : "1001" } , { ID : "1003" } } ,
2020-01-28 14:22:17 +01:00
}
2020-05-17 03:42:52 +02:00
plsCool = model . Playlist { Name : "Cool" , Owner : "userid" , Tracks : model . MediaFiles { { ID : "1004" } } }
testPlaylists = [ ] * model . Playlist { & plsBest , & plsCool }
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
}
2020-01-15 00:23:29 +01:00
var _ = Describe ( "Initialize test DB" , func ( ) {
2020-02-01 03:09:23 +01:00
// TODO Load this data setup from file(s)
2020-01-15 00:23:29 +01:00
BeforeSuite ( func ( ) {
2020-01-28 14:22:17 +01:00
o := orm . NewOrm ( )
2020-05-13 22:49:55 +02:00
ctx := log . NewContext ( context . TODO ( ) )
2020-06-05 16:59:23 +02:00
ctx = request . WithUser ( ctx , model . User { ID : "userid" , UserName : "userid" } )
2020-01-31 15:53:19 +01:00
mr := NewMediaFileRepository ( ctx , o )
2020-05-13 21:32:42 +02:00
for i := range testSongs {
s := testSongs [ i ]
2020-01-28 14:22:17 +01:00
err := mr . Put ( & s )
2020-01-15 23:49:09 +01:00
if err != nil {
panic ( err )
}
}
2020-01-22 05:01:43 +01:00
2020-04-09 05:54:54 +02:00
alr := NewAlbumRepository ( ctx , o ) . ( * albumRepository )
2020-05-13 21:32:42 +02:00
for i := range testAlbums {
a := testAlbums [ i ]
2020-04-09 05:54:54 +02:00
_ , err := alr . put ( a . ID , & a )
2020-01-31 15:53:19 +01:00
if err != nil {
panic ( err )
}
}
arr := NewArtistRepository ( ctx , o )
2020-05-13 21:32:42 +02:00
for i := range testArtists {
a := testArtists [ i ]
2020-01-31 15:53:19 +01:00
err := arr . Put ( & a )
if err != nil {
panic ( err )
}
}
pr := NewPlaylistRepository ( ctx , o )
2020-05-13 21:32:42 +02:00
for i := range testPlaylists {
2020-05-17 03:42:52 +02:00
err := pr . Put ( testPlaylists [ i ] )
2020-01-28 14:22:17 +01:00
if err != nil {
panic ( err )
}
}
2020-02-01 03:09:23 +01: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
} )
} )