navidrome/tests/mock_album_repo.go

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

78 lines
1.4 KiB
Go
Raw Normal View History

2020-10-27 16:01:40 +01:00
package tests
import (
2016-03-03 06:46:23 +01:00
"errors"
2020-01-24 01:44:08 +01:00
"github.com/navidrome/navidrome/model"
)
2021-06-08 22:30:19 +02:00
func CreateMockAlbumRepo() *MockAlbumRepo {
return &MockAlbumRepo{}
}
2021-06-08 22:30:19 +02:00
type MockAlbumRepo struct {
2020-01-15 04:22:34 +01:00
model.AlbumRepository
2020-04-09 18:13:54 +02:00
data map[string]model.Album
2020-01-15 04:22:34 +01:00
all model.Albums
err bool
2020-01-15 04:22:34 +01:00
Options model.QueryOptions
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) SetError(err bool) {
m.err = err
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) SetData(albums model.Albums) {
2020-04-09 18:13:54 +02:00
m.data = make(map[string]model.Album)
2020-10-27 15:48:37 +01:00
m.all = albums
for _, a := range m.all {
2020-04-09 18:13:54 +02:00
m.data[a.ID] = a
}
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) Exists(id string) (bool, error) {
2020-04-09 18:13:54 +02:00
if m.err {
return false, errors.New("Error!")
}
2016-03-03 06:46:23 +01:00
_, found := m.data[id]
return found, nil
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) Get(id string) (*model.Album, error) {
if m.err {
return nil, errors.New("Error!")
}
if d, ok := m.data[id]; ok {
2020-04-09 18:13:54 +02:00
return &d, nil
}
2020-01-15 04:22:34 +01:00
return nil, model.ErrNotFound
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) GetAll(qo ...model.QueryOptions) (model.Albums, error) {
if len(qo) > 0 {
m.Options = qo[0]
}
if m.err {
return nil, errors.New("Error!")
}
return m.all, nil
}
2021-06-08 22:30:19 +02:00
func (m *MockAlbumRepo) FindByArtist(artistId string) (model.Albums, error) {
if m.err {
return nil, errors.New("Error!")
}
2020-01-15 04:22:34 +01:00
var res = make(model.Albums, len(m.data))
i := 0
for _, a := range m.data {
2020-03-25 23:51:13 +01:00
if a.AlbumArtistID == artistId {
2020-04-09 18:13:54 +02:00
res[i] = a
i++
}
}
return res, nil
2016-03-03 06:46:23 +01:00
}
2021-06-08 22:30:19 +02:00
var _ model.AlbumRepository = (*MockAlbumRepo)(nil)