navidrome/tests/mock_album_repo.go

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

97 lines
1.8 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"
"time"
"github.com/google/uuid"
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{
data: make(map[string]*model.Album),
}
}
2021-06-08 22:30:19 +02:00
type MockAlbumRepo struct {
2020-01-15 04:22:34 +01:00
model.AlbumRepository
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) {
m.data = make(map[string]*model.Album)
2020-10-27 15:48:37 +01:00
m.all = albums
for i, a := range m.all {
m.data[a.ID] = &m.all[i]
}
}
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 {
return d, nil
}
2020-01-15 04:22:34 +01:00
return nil, model.ErrNotFound
}
func (m *MockAlbumRepo) Put(al *model.Album) error {
if m.err {
return errors.New("error")
}
if al.ID == "" {
al.ID = uuid.NewString()
}
m.data[al.ID] = al
return nil
}
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
}
func (m *MockAlbumRepo) GetAllWithoutGenres(qo ...model.QueryOptions) (model.Albums, error) {
return m.GetAll(qo...)
}
func (m *MockAlbumRepo) IncPlayCount(id string, timestamp time.Time) error {
if m.err {
return errors.New("error")
}
if d, ok := m.data[id]; ok {
d.PlayCount++
d.PlayDate = timestamp
return nil
}
return model.ErrNotFound
}
func (m *MockAlbumRepo) CountAll(...model.QueryOptions) (int64, error) {
return int64(len(m.all)), nil
2016-03-03 06:46:23 +01:00
}
2021-06-08 22:30:19 +02:00
var _ model.AlbumRepository = (*MockAlbumRepo)(nil)