2020-01-10 17:45:17 -09:00
package persistence
import (
2020-01-31 17:09:23 -09:00
"context"
2020-01-17 21:56:17 -09:00
"os"
"strings"
2020-01-10 17:45:17 -09:00
"testing"
2020-01-21 19:01:43 -09:00
"github.com/astaxie/beego/orm"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/conf"
2020-01-28 04:22:17 -09:00
"github.com/deluan/navidrome/db"
2020-01-23 15:44:08 -09:00
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
2020-01-26 13:10:13 -09:00
"github.com/deluan/navidrome/tests"
2020-01-28 04:22:17 -09:00
_ "github.com/mattn/go-sqlite3"
2020-01-10 17:45:17 -09:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
func TestPersistence ( t * testing . T ) {
2020-01-26 13:10:13 -09:00
tests . Init ( t , true )
2020-01-28 04:22:17 -09:00
2020-01-31 12:03:30 -09:00
//os.Remove("./test-123.db")
2020-02-01 09:46:03 -09:00
//conf.Server.Path = "./test-123.db"
conf . Server . DbPath = ":memory:"
db . Init ( )
2020-01-28 04:22:17 -09:00
New ( )
2020-02-01 09:46:03 -09:00
db . EnsureLatestVersion ( )
2020-01-14 14:23:29 -09:00
log . SetLevel ( log . LevelCritical )
2020-01-10 17:45:17 -09:00
RegisterFailHandler ( Fail )
2020-01-14 14:23:29 -09:00
RunSpecs ( t , "Persistence Suite" )
2020-01-10 17:45:17 -09:00
}
2020-01-14 14:23:29 -09:00
2020-01-31 17:09:23 -09:00
var (
artistKraftwerk = model . Artist { ID : "2" , Name : "Kraftwerk" , AlbumCount : 1 }
artistBeatles = model . Artist { ID : "3" , Name : "The Beatles" , AlbumCount : 2 }
testArtists = model . Artists {
artistKraftwerk ,
artistBeatles ,
}
)
2020-01-15 13:49:09 -09:00
2020-01-31 17:09:23 -09:00
var (
albumSgtPeppers = model . Album { ID : "1" , Name : "Sgt Peppers" , Artist : "The Beatles" , ArtistID : "3" , Genre : "Rock" , CoverArtId : "1" , CoverArtPath : P ( "/beatles/1/sgt/a day.mp3" ) , SongCount : 1 , Year : 1967 }
albumAbbeyRoad = model . Album { ID : "2" , Name : "Abbey Road" , Artist : "The Beatles" , ArtistID : "3" , Genre : "Rock" , CoverArtId : "2" , CoverArtPath : P ( "/beatles/1/come together.mp3" ) , SongCount : 1 , Year : 1969 }
albumRadioactivity = model . Album { ID : "3" , Name : "Radioactivity" , Artist : "Kraftwerk" , ArtistID : "2" , Genre : "Electronic" , CoverArtId : "3" , CoverArtPath : P ( "/kraft/radio/radio.mp3" ) , SongCount : 2 }
testAlbums = model . Albums {
albumSgtPeppers ,
albumAbbeyRoad ,
albumRadioactivity ,
}
)
2020-01-14 14:23:29 -09:00
2020-01-31 17:09:23 -09:00
var (
songDayInALife = model . MediaFile { ID : "1" , Title : "A Day In A Life" , ArtistID : "3" , Artist : "The Beatles" , AlbumID : "1" , Album : "Sgt Peppers" , Genre : "Rock" , Path : P ( "/beatles/1/sgt/a day.mp3" ) }
songComeTogether = model . MediaFile { ID : "2" , Title : "Come Together" , ArtistID : "3" , Artist : "The Beatles" , AlbumID : "2" , Album : "Abbey Road" , Genre : "Rock" , Path : P ( "/beatles/1/come together.mp3" ) }
songRadioactivity = model . MediaFile { ID : "3" , Title : "Radioactivity" , ArtistID : "2" , Artist : "Kraftwerk" , AlbumID : "3" , Album : "Radioactivity" , Genre : "Electronic" , Path : P ( "/kraft/radio/radio.mp3" ) }
songAntenna = model . MediaFile { ID : "4" , Title : "Antenna" , ArtistID : "2" , Artist : "Kraftwerk" , AlbumID : "3" , Genre : "Electronic" , Path : P ( "/kraft/radio/antenna.mp3" ) }
testSongs = model . MediaFiles {
songDayInALife ,
songComeTogether ,
songRadioactivity ,
songAntenna ,
}
)
2020-01-28 04:22:17 -09:00
var (
plsBest = model . Playlist {
ID : "10" ,
Name : "Best" ,
Comment : "No Comments" ,
Duration : 10 ,
Owner : "userid" ,
Public : true ,
Tracks : model . MediaFiles { { ID : "1" } , { ID : "3" } } ,
}
plsCool = model . Playlist { ID : "11" , Name : "Cool" , Tracks : model . MediaFiles { { ID : "4" } } }
testPlaylists = model . Playlists { plsBest , plsCool }
)
2020-01-17 21:56:17 -09:00
func P ( path string ) string {
return strings . ReplaceAll ( path , "/" , string ( os . PathSeparator ) )
}
2020-01-14 14:23:29 -09:00
var _ = Describe ( "Initialize test DB" , func ( ) {
2020-01-31 17:09:23 -09:00
// TODO Load this data setup from file(s)
2020-01-14 14:23:29 -09:00
BeforeSuite ( func ( ) {
2020-01-28 04:22:17 -09:00
o := orm . NewOrm ( )
2020-01-31 17:09:23 -09:00
ctx := context . WithValue ( log . NewContext ( nil ) , "user" , & model . User { ID : "userid" } )
2020-01-31 05:53:19 -09:00
mr := NewMediaFileRepository ( ctx , o )
2020-01-15 13:49:09 -09:00
for _ , s := range testSongs {
2020-01-28 04:22:17 -09:00
err := mr . Put ( & s )
2020-01-15 13:49:09 -09:00
if err != nil {
panic ( err )
}
}
2020-01-21 19:01:43 -09:00
2020-01-31 05:53:19 -09:00
alr := NewAlbumRepository ( ctx , o )
for _ , a := range testAlbums {
err := alr . Put ( & a )
if err != nil {
panic ( err )
}
}
arr := NewArtistRepository ( ctx , o )
for _ , a := range testArtists {
err := arr . Put ( & a )
if err != nil {
panic ( err )
}
}
pr := NewPlaylistRepository ( ctx , o )
2020-01-28 04:22:17 -09:00
for _ , pls := range testPlaylists {
err := pr . Put ( & pls )
if err != nil {
panic ( err )
}
}
2020-01-31 17:09:23 -09: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 14:23:29 -09:00
} )
} )