Refactor: Consolidate scrobbling logic in play_tracker

This commit is contained in:
Deluan
2021-06-22 23:56:29 -04:00
committed by Deluan Quintão
parent 76acd7da89
commit 056f0b944f
11 changed files with 243 additions and 120 deletions
+35 -7
View File
@@ -2,17 +2,22 @@ package tests
import (
"errors"
"time"
"github.com/google/uuid"
"github.com/navidrome/navidrome/model"
)
func CreateMockAlbumRepo() *MockAlbumRepo {
return &MockAlbumRepo{}
return &MockAlbumRepo{
data: make(map[string]*model.Album),
}
}
type MockAlbumRepo struct {
model.AlbumRepository
data map[string]model.Album
data map[string]*model.Album
all model.Albums
err bool
Options model.QueryOptions
@@ -23,10 +28,10 @@ func (m *MockAlbumRepo) SetError(err bool) {
}
func (m *MockAlbumRepo) SetData(albums model.Albums) {
m.data = make(map[string]model.Album)
m.data = make(map[string]*model.Album)
m.all = albums
for _, a := range m.all {
m.data[a.ID] = a
for i, a := range m.all {
m.data[a.ID] = &m.all[i]
}
}
@@ -43,11 +48,22 @@ func (m *MockAlbumRepo) 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
}
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
}
func (m *MockAlbumRepo) GetAll(qo ...model.QueryOptions) (model.Albums, error) {
if len(qo) > 0 {
m.Options = qo[0]
@@ -58,6 +74,18 @@ func (m *MockAlbumRepo) GetAll(qo ...model.QueryOptions) (model.Albums, error) {
return m.all, nil
}
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) FindByArtist(artistId string) (model.Albums, error) {
if m.err {
return nil, errors.New("Error!")
@@ -66,7 +94,7 @@ func (m *MockAlbumRepo) FindByArtist(artistId string) (model.Albums, error) {
i := 0
for _, a := range m.data {
if a.AlbumArtistID == artistId {
res[i] = a
res[i] = *a
i++
}
}