Make mocks strongly typed

This commit is contained in:
Deluan 2020-10-27 10:48:37 -04:00
parent 6152fadd92
commit 313a088f86
6 changed files with 25 additions and 32 deletions

View File

@ -22,8 +22,15 @@ var _ = Describe("Artwork", func() {
BeforeEach(func() {
ds = &persistence.MockDataStore{MockedTranscoding: &mockTranscodingRepository{}}
ds.Album(ctx).(*persistence.MockAlbum).SetData(`[{"id": "222", "coverArtId": "123", "coverArtPath":"tests/fixtures/test.mp3"}, {"id": "333", "coverArtId": ""}, {"id": "444", "coverArtId": "444", "coverArtPath": "tests/fixtures/cover.jpg"}]`)
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(`[{"id": "123", "albumId": "222", "path": "tests/fixtures/test.mp3", "hasCoverArt": true, "updatedAt":"2020-04-02T21:29:31.6377Z"},{"id": "456", "albumId": "222", "path": "tests/fixtures/test.ogg", "hasCoverArt": false, "updatedAt":"2020-04-02T21:29:31.6377Z"}]`)
ds.Album(ctx).(*persistence.MockAlbum).SetData(model.Albums{
{ID: "222", CoverArtId: "123", CoverArtPath: "tests/fixtures/test.mp3"},
{ID: "333", CoverArtId: ""},
{ID: "444", CoverArtId: "444", CoverArtPath: "tests/fixtures/cover.jpg"},
})
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(model.MediaFiles{
{ID: "123", AlbumID: "222", Path: "tests/fixtures/test.mp3", HasCoverArt: true},
{ID: "456", AlbumID: "222", Path: "tests/fixtures/test.ogg", HasCoverArt: false},
})
})
Context("Cache is configured", func() {

View File

@ -26,7 +26,9 @@ var _ = Describe("MediaStreamer", func() {
conf.Server.DataFolder, _ = ioutil.TempDir("", "file_caches")
conf.Server.TranscodingCacheSize = "100MB"
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}]`)
ds.MediaFile(ctx).(*persistence.MockMediaFile).SetData(model.MediaFiles{
{ID: "123", Path: "tests/fixtures/test.mp3", Suffix: "mp3", BitRate: 128, Duration: 257.0},
})
testCache := NewTranscodingCache()
Eventually(func() bool { return testCache.Ready() }).Should(BeTrue())
streamer = NewMediaStreamer(ds, ffmpeg, testCache)

View File

@ -1,9 +1,7 @@
package persistence
import (
"encoding/json"
"errors"
"fmt"
"github.com/deluan/navidrome/model"
)
@ -24,13 +22,9 @@ func (m *MockAlbum) SetError(err bool) {
m.err = err
}
func (m *MockAlbum) SetData(j string) {
func (m *MockAlbum) SetData(albums model.Albums) {
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)
}
m.all = albums
for _, a := range m.all {
m.data[a.ID] = a
}

View File

@ -1,9 +1,7 @@
package persistence
import (
"encoding/json"
"errors"
"fmt"
"github.com/deluan/navidrome/model"
)
@ -22,14 +20,9 @@ func (m *MockArtist) SetError(err bool) {
m.err = err
}
func (m *MockArtist) SetData(j string) {
func (m *MockArtist) SetData(artists model.Artists) {
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 {
for _, a := range artists {
m.data[a.ID] = a
}
}

View File

@ -1,9 +1,7 @@
package persistence
import (
"encoding/json"
"errors"
"fmt"
"github.com/deluan/navidrome/model"
)
@ -22,15 +20,10 @@ func (m *MockMediaFile) SetError(err bool) {
m.err = err
}
func (m *MockMediaFile) SetData(j string) {
func (m *MockMediaFile) SetData(mfs model.MediaFiles) {
m.data = make(map[string]model.MediaFile)
var l = model.MediaFiles{}
err := json.Unmarshal([]byte(j), &l)
if err != nil {
fmt.Println("ERROR: ", err)
}
for _, a := range l {
m.data[a.ID] = a
for _, mf := range mfs {
m.data[mf.ID] = mf
}
}

View File

@ -28,7 +28,9 @@ var _ = Describe("AlbumListController", func() {
Describe("GetAlbumList", func() {
It("should return list of the type specified", func() {
r := newGetRequest("type=newest", "offset=10", "size=20")
mockRepo.SetData(`[{"id": "1"},{"id": "2"}]`)
mockRepo.SetData(model.Albums{
{ID: "1"}, {ID: "2"},
})
resp, err := controller.GetAlbumList(w, r)
Expect(err).To(BeNil())
@ -58,7 +60,9 @@ var _ = Describe("AlbumListController", func() {
Describe("GetAlbumList2", func() {
It("should return list of the type specified", func() {
r := newGetRequest("type=newest", "offset=10", "size=20")
mockRepo.SetData(`[{"id": "1"},{"id": "2"}]`)
mockRepo.SetData(model.Albums{
{ID: "1"}, {ID: "2"},
})
resp, err := controller.GetAlbumList2(w, r)
Expect(err).To(BeNil())