quintodrome/persistence/artist_repository_test.go

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

240 lines
6.7 KiB
Go
Raw Normal View History

package persistence
2020-01-12 13:32:06 -09:00
import (
2020-01-31 11:58:17 -09:00
"context"
"github.com/fatih/structs"
"github.com/navidrome/navidrome/conf"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log"
2020-01-23 15:44:08 -09:00
"github.com/navidrome/navidrome/model"
2020-05-13 12:49:55 -08:00
"github.com/navidrome/navidrome/model/request"
"github.com/navidrome/navidrome/utils"
2022-07-26 12:47:16 -08:00
. "github.com/onsi/ginkgo/v2"
2020-01-12 13:32:06 -09:00
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gstruct"
2020-01-12 13:32:06 -09:00
)
var _ = Describe("ArtistRepository", func() {
2020-01-14 18:22:34 -09:00
var repo model.ArtistRepository
2020-01-12 13:32:06 -09:00
BeforeEach(func() {
2020-05-13 12:49:55 -08:00
ctx := log.NewContext(context.TODO())
ctx = request.WithUser(ctx, model.User{ID: "userid"})
repo = NewArtistRepository(ctx, NewDBXBuilder(db.Db()))
2020-01-12 13:32:06 -09:00
})
Describe("Count", func() {
It("returns the number of artists in the DB", func() {
Expect(repo.CountAll()).To(Equal(int64(2)))
})
})
2020-03-25 16:33:32 -08:00
Describe("Exists", func() {
It("returns true for an artist that is in the DB", func() {
Expect(repo.Exists("3")).To(BeTrue())
})
It("returns false for an artist that is in the DB", func() {
Expect(repo.Exists("666")).To(BeFalse())
})
})
2020-01-12 13:32:06 -09:00
Describe("Get", func() {
It("saves and retrieves data", func() {
Expect(repo.Get("2")).To(Equal(&artistKraftwerk))
})
2020-01-12 13:32:06 -09:00
})
Describe("GetIndexKey", func() {
r := artistRepository{indexGroups: utils.ParseIndexGroups(conf.Server.IndexGroups)}
It("returns the index key when PreferSortTags is true and SortArtistName is not empty", func() {
conf.Server.PreferSortTags = true
a := model.Artist{SortArtistName: "Foo", OrderArtistName: "Bar", Name: "Qux"}
idx := GetIndexKey(&r, &a) // defines export_test.go
Expect(idx).To(Equal("F"))
a = model.Artist{SortArtistName: "foo", OrderArtistName: "Bar", Name: "Qux"}
idx = GetIndexKey(&r, &a)
Expect(idx).To(Equal("F"))
})
It("returns the index key when PreferSortTags is true, SortArtistName is empty and OrderArtistName is not empty", func() {
conf.Server.PreferSortTags = true
a := model.Artist{SortArtistName: "", OrderArtistName: "Bar", Name: "Qux"}
idx := GetIndexKey(&r, &a)
Expect(idx).To(Equal("B"))
a = model.Artist{SortArtistName: "", OrderArtistName: "bar", Name: "Qux"}
idx = GetIndexKey(&r, &a)
Expect(idx).To(Equal("B"))
})
It("returns the index key when PreferSortTags is true, both SortArtistName, OrderArtistName are empty", func() {
conf.Server.PreferSortTags = true
a := model.Artist{SortArtistName: "", OrderArtistName: "", Name: "Qux"}
idx := GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
a = model.Artist{SortArtistName: "", OrderArtistName: "", Name: "qux"}
idx = GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
})
It("returns the index key when PreferSortTags is false and SortArtistName is not empty", func() {
conf.Server.PreferSortTags = false
a := model.Artist{SortArtistName: "Foo", OrderArtistName: "Bar", Name: "Qux"}
idx := GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
})
It("returns the index key when PreferSortTags is true, SortArtistName is empty and OrderArtistName is not empty", func() {
conf.Server.PreferSortTags = false
a := model.Artist{SortArtistName: "", OrderArtistName: "Bar", Name: "Qux"}
idx := GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
})
It("returns the index key when PreferSortTags is true, both sort_artist_name, order_artist_name are empty", func() {
conf.Server.PreferSortTags = false
a := model.Artist{SortArtistName: "", OrderArtistName: "", Name: "Qux"}
idx := GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
a = model.Artist{SortArtistName: "", OrderArtistName: "", Name: "qux"}
idx = GetIndexKey(&r, &a)
Expect(idx).To(Equal("Q"))
})
})
Describe("GetIndex", func() {
It("returns the index when PreferSortTags is true and SortArtistName is not empty", func() {
conf.Server.PreferSortTags = true
artistBeatles.SortArtistName = "Foo"
er := repo.Put(&artistBeatles)
Expect(er).To(BeNil())
idx, err := repo.GetIndex()
Expect(err).To(BeNil())
Expect(idx).To(Equal(model.ArtistIndexes{
{
ID: "F",
Artists: model.Artists{
artistBeatles,
},
},
{
ID: "K",
Artists: model.Artists{
artistKraftwerk,
},
},
}))
artistBeatles.SortArtistName = ""
er = repo.Put(&artistBeatles)
Expect(er).To(BeNil())
})
It("returns the index when PreferSortTags is true and SortArtistName is empty", func() {
conf.Server.PreferSortTags = true
idx, err := repo.GetIndex()
Expect(err).To(BeNil())
Expect(idx).To(Equal(model.ArtistIndexes{
{
ID: "B",
Artists: model.Artists{
artistBeatles,
},
},
{
ID: "K",
Artists: model.Artists{
artistKraftwerk,
},
},
}))
})
It("returns the index when PreferSortTags is false and SortArtistName is not empty", func() {
conf.Server.PreferSortTags = false
artistBeatles.SortArtistName = "Foo"
er := repo.Put(&artistBeatles)
Expect(er).To(BeNil())
idx, err := repo.GetIndex()
Expect(err).To(BeNil())
Expect(idx).To(Equal(model.ArtistIndexes{
{
ID: "B",
Artists: model.Artists{
artistBeatles,
},
},
{
ID: "K",
Artists: model.Artists{
artistKraftwerk,
},
},
}))
artistBeatles.SortArtistName = ""
er = repo.Put(&artistBeatles)
Expect(er).To(BeNil())
})
It("returns the index when PreferSortTags is false and SortArtistName is empty", func() {
conf.Server.PreferSortTags = false
idx, err := repo.GetIndex()
Expect(err).To(BeNil())
Expect(idx).To(Equal(model.ArtistIndexes{
{
ID: "B",
Artists: model.Artists{
artistBeatles,
},
},
{
ID: "K",
Artists: model.Artists{
artistKraftwerk,
},
},
}))
})
2020-01-12 13:32:06 -09:00
})
Describe("dbArtist mapping", func() {
var a *model.Artist
BeforeEach(func() {
a = &model.Artist{ID: "1", Name: "Van Halen", SimilarArtists: []model.Artist{
{ID: "2", Name: "AC/DC"}, {ID: "-1", Name: "Test;With:Sep,Chars"},
}}
})
It("maps fields", func() {
dba := &dbArtist{Artist: a}
m := structs.Map(dba)
Expect(dba.PostMapArgs(m)).To(Succeed())
Expect(m).To(HaveKeyWithValue("similar_artists", "2:AC%2FDC;-1:Test%3BWith%3ASep%2CChars"))
other := dbArtist{SimilarArtists: m["similar_artists"].(string), Artist: &model.Artist{
ID: "1", Name: "Van Halen",
}}
Expect(other.PostScan()).To(Succeed())
actual := other.Artist
Expect(*actual).To(MatchFields(IgnoreExtras, Fields{
"ID": Equal(a.ID),
"Name": Equal(a.Name),
}))
Expect(actual.SimilarArtists).To(HaveLen(2))
Expect(actual.SimilarArtists[0].ID).To(Equal("2"))
Expect(actual.SimilarArtists[0].Name).To(Equal("AC/DC"))
Expect(actual.SimilarArtists[1].ID).To(Equal("-1"))
Expect(actual.SimilarArtists[1].Name).To(Equal("Test;With:Sep,Chars"))
})
})
2020-01-12 13:32:06 -09:00
})