navidrome/persistence/mediafile_repository_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

147 lines
4.3 KiB
Go
Raw Normal View History

2020-01-16 21:56:24 +01:00
package persistence
import (
"context"
"time"
2020-01-18 07:56:17 +01:00
"github.com/astaxie/beego/orm"
"github.com/deluan/navidrome/log"
2020-01-24 01:44:08 +01:00
"github.com/deluan/navidrome/model"
2020-05-13 22:49:55 +02:00
"github.com/deluan/navidrome/model/request"
"github.com/google/uuid"
2020-01-16 21:56:24 +01:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("MediaRepository", func() {
var mr model.MediaFileRepository
2020-01-16 21:56:24 +01:00
BeforeEach(func() {
2020-05-13 22:49:55 +02:00
ctx := log.NewContext(context.TODO())
ctx = request.WithUser(ctx, model.User{ID: "userid"})
mr = NewMediaFileRepository(ctx, orm.NewOrm())
2020-01-16 21:56:24 +01:00
})
It("gets mediafile from the DB", func() {
2020-04-20 04:40:24 +02:00
Expect(mr.Get("1004")).To(Equal(&songAntenna))
2020-01-16 21:56:24 +01:00
})
It("returns ErrNotFound", func() {
_, err := mr.Get("56")
Expect(err).To(MatchError(model.ErrNotFound))
})
It("counts the number of mediafiles in the DB", func() {
Expect(mr.CountAll()).To(Equal(int64(4)))
})
It("checks existence of mediafiles in the DB", func() {
Expect(mr.Exists(songAntenna.ID)).To(BeTrue())
Expect(mr.Exists("666")).To(BeFalse())
})
It("find mediafiles by album", func() {
2020-04-20 04:40:24 +02:00
Expect(mr.FindByAlbum("103")).To(Equal(model.MediaFiles{
songRadioactivity,
songAntenna,
}))
})
It("returns empty array when no tracks are found", func() {
2020-01-31 22:47:13 +01:00
Expect(mr.FindByAlbum("67")).To(Equal(model.MediaFiles{}))
})
2020-07-14 21:27:27 +02:00
It("finds tracks by path when using wildcards chars", func() {
2020-07-14 00:37:48 +02:00
Expect(mr.Put(&model.MediaFile{ID: "7001", Path: P("/Find:By'Path/_/123.mp3")})).To(BeNil())
Expect(mr.Put(&model.MediaFile{ID: "7002", Path: P("/Find:By'Path/1/123.mp3")})).To(BeNil())
found, err := mr.FindAllByPath(P("/Find:By'Path/_/"))
2020-07-14 00:37:48 +02:00
Expect(err).To(BeNil())
Expect(found).To(HaveLen(1))
Expect(found[0].ID).To(Equal("7001"))
})
2020-07-14 21:27:27 +02:00
It("finds tracks by path case sensitively", func() {
Expect(mr.Put(&model.MediaFile{ID: "7003", Path: P("/Casesensitive/file1.mp3")})).To(BeNil())
Expect(mr.Put(&model.MediaFile{ID: "7004", Path: P("/casesensitive/file2.mp3")})).To(BeNil())
found, err := mr.FindAllByPath(P("/Casesensitive"))
2020-07-14 21:27:27 +02:00
Expect(err).To(BeNil())
Expect(found).To(HaveLen(1))
Expect(found[0].ID).To(Equal("7003"))
found, err = mr.FindAllByPath(P("/casesensitive/"))
2020-07-14 21:27:27 +02:00
Expect(err).To(BeNil())
Expect(found).To(HaveLen(1))
Expect(found[0].ID).To(Equal("7004"))
})
It("returns starred tracks", func() {
Expect(mr.GetStarred()).To(Equal(model.MediaFiles{
songComeTogether,
}))
})
It("delete tracks by id", func() {
random, _ := uuid.NewRandom()
id := random.String()
Expect(mr.Put(&model.MediaFile{ID: id})).To(BeNil())
Expect(mr.Delete(id)).To(BeNil())
_, err := mr.Get(id)
Expect(err).To(MatchError(model.ErrNotFound))
})
It("delete tracks by path", func() {
id1 := "1111"
Expect(mr.Put(&model.MediaFile{ID: id1, Path: P("/abc/123/" + id1 + ".mp3")})).To(BeNil())
id2 := "2222"
Expect(mr.Put(&model.MediaFile{ID: id2, Path: P("/abc/123/" + id2 + ".mp3")})).To(BeNil())
2020-01-31 23:56:02 +01:00
id3 := "3333"
2020-07-14 00:37:48 +02:00
Expect(mr.Put(&model.MediaFile{ID: id3, Path: P("/ab_/" + id3 + ".mp3")})).To(BeNil())
id4 := "4444"
Expect(mr.Put(&model.MediaFile{ID: id4, Path: P("/abc/" + id4 + ".mp3")})).To(BeNil())
id5 := "5555"
Expect(mr.Put(&model.MediaFile{ID: id5, Path: P("/Ab_/" + id5 + ".mp3")})).To(BeNil())
2020-07-14 00:37:48 +02:00
Expect(mr.DeleteByPath(P("/ab_"))).To(Equal(int64(1)))
2020-01-31 23:56:02 +01:00
Expect(mr.Get(id1)).ToNot(BeNil())
Expect(mr.Get(id2)).ToNot(BeNil())
2020-07-14 00:37:48 +02:00
Expect(mr.Get(id4)).ToNot(BeNil())
Expect(mr.Get(id5)).ToNot(BeNil())
2020-01-31 23:56:02 +01:00
_, err := mr.Get(id3)
Expect(err).To(MatchError(model.ErrNotFound))
})
Context("Annotations", func() {
It("increments play count when the tracks does not have annotations", func() {
id := "incplay.firsttime"
Expect(mr.Put(&model.MediaFile{ID: id})).To(BeNil())
playDate := time.Now()
Expect(mr.IncPlayCount(id, playDate)).To(BeNil())
mf, err := mr.Get(id)
Expect(err).To(BeNil())
Expect(mf.PlayDate.Unix()).To(Equal(playDate.Unix()))
Expect(mf.PlayCount).To(Equal(int64(1)))
})
It("increments play count on newly starred items", func() {
id := "star.incplay"
Expect(mr.Put(&model.MediaFile{ID: id})).To(BeNil())
Expect(mr.SetStar(true, id)).To(BeNil())
playDate := time.Now()
Expect(mr.IncPlayCount(id, playDate)).To(BeNil())
mf, err := mr.Get(id)
Expect(err).To(BeNil())
Expect(mf.PlayDate.Unix()).To(Equal(playDate.Unix()))
Expect(mf.PlayCount).To(Equal(int64(1)))
})
})
2020-01-16 21:56:24 +01:00
})