Fix tests for Cover service

This commit is contained in:
Deluan 2020-04-09 12:13:54 -04:00
parent 4fc88f23e9
commit 5265d0234f
5 changed files with 44 additions and 21 deletions

View File

@ -18,8 +18,8 @@ var _ = Describe("Cover", func() {
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}}
ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "CoverArtId": "222"}, {"id": "333", "CoverArtId": ""}]`, 1) ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "coverArtId": "123"}, {"id": "333", "coverArtId": ""}]`)
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`, 1) ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`)
cover = NewCover(ds, testCache) cover = NewCover(ds, testCache)
}) })
@ -30,7 +30,7 @@ var _ = Describe("Cover", func() {
_, format, err := image.Decode(bytes.NewReader(buf.Bytes())) _, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(format).To(Equal("png")) Expect(format).To(Equal("jpeg"))
}) })
It("accepts albumIds with 'al-' prefix", func() { It("accepts albumIds with 'al-' prefix", func() {
@ -38,8 +38,9 @@ var _ = Describe("Cover", func() {
Expect(cover.Get(ctx, "al-222", 0, buf)).To(BeNil()) Expect(cover.Get(ctx, "al-222", 0, buf)).To(BeNil())
_, _, err := image.Decode(bytes.NewReader(buf.Bytes())) _, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(format).To(Equal("jpeg"))
}) })
It("returns the default cover if album does not have cover", func() { It("returns the default cover if album does not have cover", func() {
@ -79,10 +80,26 @@ var _ = Describe("Cover", func() {
Expect(cover.Get(ctx, "123", 200, buf)).To(BeNil()) Expect(cover.Get(ctx, "123", 200, buf)).To(BeNil())
img, _, err := image.Decode(bytes.NewReader(buf.Bytes())) img, format, err := image.Decode(bytes.NewReader(buf.Bytes()))
Expect(err).To(BeNil()) Expect(err).To(BeNil())
Expect(format).To(Equal("jpeg"))
Expect(img.Bounds().Size().X).To(Equal(200)) Expect(img.Bounds().Size().X).To(Equal(200))
Expect(img.Bounds().Size().Y).To(Equal(200)) Expect(img.Bounds().Size().Y).To(Equal(200))
}) })
Context("Errors", func() {
It("returns err if gets error from album table", func() {
ds.Album(ctx).(*persistence.MockAlbum).SetError(true)
buf := new(bytes.Buffer)
Expect(cover.Get(ctx, "222", 0, buf)).To(MatchError("Error!"))
})
It("returns err if gets error from media_file table", func() {
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetError(true)
buf := new(bytes.Buffer)
Expect(cover.Get(ctx, "123", 0, buf)).To(MatchError("Error!"))
})
})
}) })

View File

@ -20,7 +20,7 @@ var _ = Describe("MediaStreamer", func() {
BeforeEach(func() { BeforeEach(func() {
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}}
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "suffix": "mp3", "bitRate": 128, "duration": 257.0}]`, 1) ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "suffix": "mp3", "bitRate": 128, "duration": 257.0}]`)
streamer = NewMediaStreamer(ds, ffmpeg, testCache) streamer = NewMediaStreamer(ds, ffmpeg, testCache)
}) })

View File

@ -14,7 +14,7 @@ func CreateMockAlbumRepo() *MockAlbum {
type MockAlbum struct { type MockAlbum struct {
model.AlbumRepository model.AlbumRepository
data map[string]*model.Album data map[string]model.Album
all model.Albums all model.Albums
err bool err bool
Options model.QueryOptions Options model.QueryOptions
@ -24,19 +24,22 @@ func (m *MockAlbum) SetError(err bool) {
m.err = err m.err = err
} }
func (m *MockAlbum) SetData(j string, size int) { func (m *MockAlbum) SetData(j string) {
m.data = make(map[string]*model.Album) m.data = make(map[string]model.Album)
m.all = make(model.Albums, size) m.all = model.Albums{}
err := json.Unmarshal([]byte(j), &m.all) err := json.Unmarshal([]byte(j), &m.all)
if err != nil { if err != nil {
fmt.Println("ERROR: ", err) fmt.Println("ERROR: ", err)
} }
for _, a := range m.all { for _, a := range m.all {
m.data[a.ID] = &a m.data[a.ID] = a
} }
} }
func (m *MockAlbum) Exists(id string) (bool, error) { func (m *MockAlbum) Exists(id string) (bool, error) {
if m.err {
return false, errors.New("Error!")
}
_, found := m.data[id] _, found := m.data[id]
return found, nil return found, nil
} }
@ -46,7 +49,7 @@ func (m *MockAlbum) Get(id string) (*model.Album, error) {
return nil, errors.New("Error!") return nil, errors.New("Error!")
} }
if d, ok := m.data[id]; ok { if d, ok := m.data[id]; ok {
return d, nil return &d, nil
} }
return nil, model.ErrNotFound return nil, model.ErrNotFound
} }
@ -69,7 +72,7 @@ func (m *MockAlbum) FindByArtist(artistId string) (model.Albums, error) {
i := 0 i := 0
for _, a := range m.data { for _, a := range m.data {
if a.AlbumArtistID == artistId { if a.AlbumArtistID == artistId {
res[i] = *a res[i] = a
i++ i++
} }
} }

View File

@ -14,7 +14,7 @@ func CreateMockArtistRepo() *MockArtist {
type MockArtist struct { type MockArtist struct {
model.ArtistRepository model.ArtistRepository
data map[string]*model.Artist data map[string]model.Artist
err bool err bool
} }
@ -22,19 +22,22 @@ func (m *MockArtist) SetError(err bool) {
m.err = err m.err = err
} }
func (m *MockArtist) SetData(j string, size int) { func (m *MockArtist) SetData(j string) {
m.data = make(map[string]*model.Artist) m.data = make(map[string]model.Artist)
var l = make([]model.Artist, size) var l = model.Artists{}
err := json.Unmarshal([]byte(j), &l) err := json.Unmarshal([]byte(j), &l)
if err != nil { if err != nil {
fmt.Println("ERROR: ", err) fmt.Println("ERROR: ", err)
} }
for _, a := range l { for _, a := range l {
m.data[a.ID] = &a m.data[a.ID] = a
} }
} }
func (m *MockArtist) Exists(id string) (bool, error) { func (m *MockArtist) Exists(id string) (bool, error) {
if m.err {
return false, errors.New("Error!")
}
_, found := m.data[id] _, found := m.data[id]
return found, nil return found, nil
} }
@ -44,7 +47,7 @@ func (m *MockArtist) Get(id string) (*model.Artist, error) {
return nil, errors.New("Error!") return nil, errors.New("Error!")
} }
if d, ok := m.data[id]; ok { if d, ok := m.data[id]; ok {
return d, nil return &d, nil
} }
return nil, model.ErrNotFound return nil, model.ErrNotFound
} }

View File

@ -22,9 +22,9 @@ func (m *MockMediaFile) SetError(err bool) {
m.err = err m.err = err
} }
func (m *MockMediaFile) SetData(j string, size int) { func (m *MockMediaFile) SetData(j string) {
m.data = make(map[string]model.MediaFile) m.data = make(map[string]model.MediaFile)
var l = make(model.MediaFiles, size) var l = model.MediaFiles{}
err := json.Unmarshal([]byte(j), &l) err := json.Unmarshal([]byte(j), &l)
if err != nil { if err != nil {
fmt.Println("ERROR: ", err) fmt.Println("ERROR: ", err)