From 5265d0234f1e6011fb07dbe5d2f18c33db7f5820 Mon Sep 17 00:00:00 2001 From: Deluan Date: Thu, 9 Apr 2020 12:13:54 -0400 Subject: [PATCH] Fix tests for Cover service --- engine/cover_test.go | 27 ++++++++++++++++++++++----- engine/media_streamer_test.go | 2 +- persistence/mock_album_repo.go | 17 ++++++++++------- persistence/mock_artist_repo.go | 15 +++++++++------ persistence/mock_mediafile_repo.go | 4 ++-- 5 files changed, 44 insertions(+), 21 deletions(-) diff --git a/engine/cover_test.go b/engine/cover_test.go index 2505a52f5..ecc0f0f7f 100644 --- a/engine/cover_test.go +++ b/engine/cover_test.go @@ -18,8 +18,8 @@ var _ = Describe("Cover", func() { BeforeEach(func() { ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}} - ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "CoverArtId": "222"}, {"id": "333", "CoverArtId": ""}]`, 1) - ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`, 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"}]`) cover = NewCover(ds, testCache) }) @@ -30,7 +30,7 @@ var _ = Describe("Cover", func() { _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) - Expect(format).To(Equal("png")) + Expect(format).To(Equal("jpeg")) }) 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()) - _, _, err := image.Decode(bytes.NewReader(buf.Bytes())) + _, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) + Expect(format).To(Equal("jpeg")) }) 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()) - img, _, err := image.Decode(bytes.NewReader(buf.Bytes())) + img, format, err := image.Decode(bytes.NewReader(buf.Bytes())) Expect(err).To(BeNil()) + Expect(format).To(Equal("jpeg")) Expect(img.Bounds().Size().X).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!")) + }) + }) }) diff --git a/engine/media_streamer_test.go b/engine/media_streamer_test.go index 1cec0bee0..2ed076dee 100644 --- a/engine/media_streamer_test.go +++ b/engine/media_streamer_test.go @@ -20,7 +20,7 @@ var _ = Describe("MediaStreamer", func() { BeforeEach(func() { 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) }) diff --git a/persistence/mock_album_repo.go b/persistence/mock_album_repo.go index 1b014c5e7..c4c82a94f 100644 --- a/persistence/mock_album_repo.go +++ b/persistence/mock_album_repo.go @@ -14,7 +14,7 @@ func CreateMockAlbumRepo() *MockAlbum { type MockAlbum struct { model.AlbumRepository - data map[string]*model.Album + data map[string]model.Album all model.Albums err bool Options model.QueryOptions @@ -24,19 +24,22 @@ func (m *MockAlbum) SetError(err bool) { m.err = err } -func (m *MockAlbum) SetData(j string, size int) { - m.data = make(map[string]*model.Album) - m.all = make(model.Albums, size) +func (m *MockAlbum) SetData(j string) { + m.data = make(map[string]model.Album) + m.all = model.Albums{} err := json.Unmarshal([]byte(j), &m.all) if err != nil { fmt.Println("ERROR: ", err) } for _, a := range m.all { - m.data[a.ID] = &a + m.data[a.ID] = a } } func (m *MockAlbum) Exists(id string) (bool, error) { + if m.err { + return false, errors.New("Error!") + } _, found := m.data[id] return found, nil } @@ -46,7 +49,7 @@ func (m *MockAlbum) Get(id string) (*model.Album, error) { return nil, errors.New("Error!") } if d, ok := m.data[id]; ok { - return d, nil + return &d, nil } return nil, model.ErrNotFound } @@ -69,7 +72,7 @@ func (m *MockAlbum) FindByArtist(artistId string) (model.Albums, error) { i := 0 for _, a := range m.data { if a.AlbumArtistID == artistId { - res[i] = *a + res[i] = a i++ } } diff --git a/persistence/mock_artist_repo.go b/persistence/mock_artist_repo.go index 4c4686142..2a159d617 100644 --- a/persistence/mock_artist_repo.go +++ b/persistence/mock_artist_repo.go @@ -14,7 +14,7 @@ func CreateMockArtistRepo() *MockArtist { type MockArtist struct { model.ArtistRepository - data map[string]*model.Artist + data map[string]model.Artist err bool } @@ -22,19 +22,22 @@ func (m *MockArtist) SetError(err bool) { m.err = err } -func (m *MockArtist) SetData(j string, size int) { - m.data = make(map[string]*model.Artist) - var l = make([]model.Artist, size) +func (m *MockArtist) SetData(j string) { + m.data = make(map[string]model.Artist) + var l = model.Artists{} err := json.Unmarshal([]byte(j), &l) if err != nil { fmt.Println("ERROR: ", err) } for _, a := range l { - m.data[a.ID] = &a + m.data[a.ID] = a } } func (m *MockArtist) Exists(id string) (bool, error) { + if m.err { + return false, errors.New("Error!") + } _, found := m.data[id] return found, nil } @@ -44,7 +47,7 @@ func (m *MockArtist) Get(id string) (*model.Artist, error) { return nil, errors.New("Error!") } if d, ok := m.data[id]; ok { - return d, nil + return &d, nil } return nil, model.ErrNotFound } diff --git a/persistence/mock_mediafile_repo.go b/persistence/mock_mediafile_repo.go index 40a3b66fd..7897f5455 100644 --- a/persistence/mock_mediafile_repo.go +++ b/persistence/mock_mediafile_repo.go @@ -22,9 +22,9 @@ func (m *MockMediaFile) SetError(err bool) { m.err = err } -func (m *MockMediaFile) SetData(j string, size int) { +func (m *MockMediaFile) SetData(j string) { m.data = make(map[string]model.MediaFile) - var l = make(model.MediaFiles, size) + var l = model.MediaFiles{} err := json.Unmarshal([]byte(j), &l) if err != nil { fmt.Println("ERROR: ", err)