2020-01-28 14:22:17 +01:00
|
|
|
package persistence
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego/orm"
|
2020-01-31 15:53:19 +01:00
|
|
|
"github.com/deluan/navidrome/log"
|
2020-01-28 14:22:17 +01:00
|
|
|
"github.com/deluan/navidrome/model"
|
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
)
|
|
|
|
|
|
|
|
var _ = Describe("PlaylistRepository", func() {
|
|
|
|
var repo model.PlaylistRepository
|
|
|
|
|
|
|
|
BeforeEach(func() {
|
2020-01-31 15:53:19 +01:00
|
|
|
repo = NewPlaylistRepository(log.NewContext(nil), orm.NewOrm())
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Count", func() {
|
|
|
|
It("returns the number of playlists in the DB", func() {
|
|
|
|
Expect(repo.CountAll()).To(Equal(int64(2)))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-03-26 01:33:32 +01:00
|
|
|
Describe("Exists", func() {
|
2020-01-28 14:22:17 +01:00
|
|
|
It("returns true for an existing playlist", func() {
|
|
|
|
Expect(repo.Exists("11")).To(BeTrue())
|
|
|
|
})
|
|
|
|
It("returns false for a non-existing playlist", func() {
|
|
|
|
Expect(repo.Exists("666")).To(BeFalse())
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("Get", func() {
|
|
|
|
It("returns an existing playlist", func() {
|
2020-04-11 22:45:21 +02:00
|
|
|
p, err := repo.Get("10")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
// Compare all but Tracks and timestamps
|
|
|
|
p2 := *p
|
|
|
|
p2.Tracks = plsBest.Tracks
|
|
|
|
p2.UpdatedAt = plsBest.UpdatedAt
|
|
|
|
p2.CreatedAt = plsBest.CreatedAt
|
|
|
|
Expect(p2).To(Equal(plsBest))
|
|
|
|
// Compare tracks
|
|
|
|
for i := range p.Tracks {
|
|
|
|
Expect(p.Tracks[i].ID).To(Equal(plsBest.Tracks[i].ID))
|
|
|
|
}
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
It("returns ErrNotFound for a non-existing playlist", func() {
|
|
|
|
_, err := repo.Get("666")
|
|
|
|
Expect(err).To(MatchError(model.ErrNotFound))
|
|
|
|
})
|
2020-04-12 01:05:51 +02:00
|
|
|
It("returns all tracks", func() {
|
|
|
|
pls, err := repo.Get("10")
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(pls.Name).To(Equal(plsBest.Name))
|
|
|
|
Expect(pls.Tracks).To(Equal(model.MediaFiles{
|
|
|
|
songDayInALife,
|
|
|
|
songRadioactivity,
|
|
|
|
}))
|
|
|
|
})
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
|
2020-04-11 22:45:21 +02:00
|
|
|
Describe("Put/Exists/Delete", func() {
|
|
|
|
var newPls model.Playlist
|
|
|
|
BeforeEach(func() {
|
2020-04-12 03:11:49 +02:00
|
|
|
newPls = model.Playlist{ID: "22", Name: "Great!", Tracks: model.MediaFiles{{ID: "4"}, {ID: "3"}}}
|
2020-04-11 22:45:21 +02:00
|
|
|
})
|
2020-01-28 14:22:17 +01:00
|
|
|
It("saves the playlist to the DB", func() {
|
|
|
|
Expect(repo.Put(&newPls)).To(BeNil())
|
|
|
|
})
|
2020-04-12 03:11:49 +02:00
|
|
|
It("adds repeated songs to a playlist and keeps the order", func() {
|
|
|
|
newPls.Tracks = append(newPls.Tracks, model.MediaFile{ID: "4"})
|
|
|
|
Expect(repo.Put(&newPls)).To(BeNil())
|
|
|
|
saved, _ := repo.Get("22")
|
|
|
|
Expect(saved.Tracks).To(HaveLen(3))
|
|
|
|
Expect(saved.Tracks[0].ID).To(Equal("4"))
|
|
|
|
Expect(saved.Tracks[1].ID).To(Equal("3"))
|
|
|
|
Expect(saved.Tracks[2].ID).To(Equal("4"))
|
|
|
|
})
|
2020-01-28 14:22:17 +01:00
|
|
|
It("returns the newly created playlist", func() {
|
2020-04-11 22:45:21 +02:00
|
|
|
Expect(repo.Exists("22")).To(BeTrue())
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
It("returns deletes the playlist", func() {
|
|
|
|
Expect(repo.Delete("22")).To(BeNil())
|
|
|
|
})
|
|
|
|
It("returns error if tries to retrieve the deleted playlist", func() {
|
2020-04-11 22:45:21 +02:00
|
|
|
Expect(repo.Exists("22")).To(BeFalse())
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Describe("GetAll", func() {
|
|
|
|
It("returns all playlists from DB", func() {
|
2020-04-11 22:45:21 +02:00
|
|
|
all, err := repo.GetAll()
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
Expect(all[0].ID).To(Equal(plsBest.ID))
|
|
|
|
Expect(all[1].ID).To(Equal(plsCool.ID))
|
2020-01-28 14:22:17 +01:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|