navidrome/persistence/album_repository_test.go

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

142 lines
4.1 KiB
Go
Raw Normal View History

package persistence
2020-01-13 00:55:55 +01:00
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"github.com/astaxie/beego/orm"
"github.com/deluan/navidrome/conf"
"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"
2020-01-13 00:55:55 +01:00
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("AlbumRepository", func() {
2020-01-15 04:22:34 +01:00
var repo model.AlbumRepository
2020-01-13 00:55:55 +01:00
BeforeEach(func() {
2020-05-13 22:49:55 +02:00
ctx := request.WithUser(log.NewContext(context.TODO()), model.User{ID: "userid", UserName: "johndoe"})
repo = NewAlbumRepository(ctx, orm.NewOrm())
})
Describe("Get", func() {
It("returns an existent album", func() {
2020-04-20 04:40:24 +02:00
Expect(repo.Get("103")).To(Equal(&albumRadioactivity))
})
It("returns ErrNotFound when the album does not exist", func() {
_, err := repo.Get("666")
Expect(err).To(MatchError(model.ErrNotFound))
})
2020-01-13 00:55:55 +01:00
})
Describe("GetAll", func() {
It("returns all records", func() {
Expect(repo.GetAll()).To(Equal(testAlbums))
})
It("returns all records sorted", func() {
Expect(repo.GetAll(model.QueryOptions{Sort: "name"})).To(Equal(model.Albums{
2020-01-15 23:49:09 +01:00
albumAbbeyRoad,
albumRadioactivity,
albumSgtPeppers,
2020-01-13 00:55:55 +01:00
}))
})
It("returns all records sorted desc", func() {
Expect(repo.GetAll(model.QueryOptions{Sort: "name", Order: "desc"})).To(Equal(model.Albums{
2020-01-15 23:49:09 +01:00
albumSgtPeppers,
albumRadioactivity,
albumAbbeyRoad,
2020-01-13 00:55:55 +01:00
}))
})
It("paginates the result", func() {
Expect(repo.GetAll(model.QueryOptions{Offset: 1, Max: 1})).To(Equal(model.Albums{
2020-01-15 23:49:09 +01:00
albumAbbeyRoad,
2020-01-13 00:55:55 +01:00
}))
})
})
Describe("GetStarred", func() {
It("returns all starred records", func() {
Expect(repo.GetStarred(model.QueryOptions{})).To(Equal(model.Albums{
2020-01-15 23:49:09 +01:00
albumRadioactivity,
2020-01-13 00:55:55 +01:00
}))
})
})
Describe("FindByArtist", func() {
It("returns all records from a given ArtistID", func() {
Expect(repo.FindByArtist("3")).To(Equal(model.Albums{
2020-01-15 23:49:09 +01:00
albumSgtPeppers,
albumAbbeyRoad,
2020-01-13 00:55:55 +01:00
}))
})
})
Describe("getMinYear", func() {
It("returns 0 when there's no valid year", func() {
Expect(getMinYear("a b c")).To(Equal(0))
Expect(getMinYear("")).To(Equal(0))
})
It("returns 0 when all values are 0", func() {
Expect(getMinYear("0 0 0 ")).To(Equal(0))
})
It("returns the smallest value from the list", func() {
Expect(getMinYear("2000 0 1800")).To(Equal(1800))
})
})
Describe("getCoverFromPath", func() {
testFolder, _ := ioutil.TempDir("", "album_persistence_tests")
if err := os.MkdirAll(testFolder, 0777); err != nil {
panic(err)
}
if _, err := os.Create(filepath.Join(testFolder, "Cover.jpeg")); err != nil {
panic(err)
}
if _, err := os.Create(filepath.Join(testFolder, "FRONT.PNG")); err != nil {
panic(err)
}
testPath := filepath.Join(testFolder, "somefile.test")
2020-06-29 16:50:38 +02:00
embeddedPath := filepath.Join(testFolder, "somefile.mp3")
It("returns audio file for embedded cover", func() {
conf.Server.CoverArtPriority = "embedded, cover.*, front.*"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, embeddedPath)).To(Equal(""))
})
It("returns external file when no embedded cover exists", func() {
conf.Server.CoverArtPriority = "embedded, cover.*, front.*"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))
})
It("returns embedded cover even if not first choice", func() {
conf.Server.CoverArtPriority = "something.png, embedded, cover.*, front.*"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, embeddedPath)).To(Equal(""))
})
It("returns first correct match case-insensitively", func() {
conf.Server.CoverArtPriority = "embedded, cover.jpg, front.svg, front.png"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "FRONT.PNG")))
})
It("returns match for embedded pattern", func() {
conf.Server.CoverArtPriority = "embedded, cover.jp?g, front.png"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, "")).To(Equal(filepath.Join(testFolder, "Cover.jpeg")))
})
It("returns empty string if no match was found", func() {
conf.Server.CoverArtPriority = "embedded, cover.jpg, front.apng"
2020-06-29 16:50:38 +02:00
Expect(getCoverFromPath(testPath, "")).To(Equal(""))
})
// Reset configuration to default.
conf.Server.CoverArtPriority = "embedded, cover.*, front.*"
})
2020-01-13 00:55:55 +01:00
})