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++
}
}
+34 -6
View File
@@ -2,17 +2,22 @@ package tests
import (
"errors"
"time"
"github.com/google/uuid"
"github.com/navidrome/navidrome/model"
)
func CreateMockArtistRepo() *MockArtistRepo {
return &MockArtistRepo{}
return &MockArtistRepo{
data: make(map[string]*model.Artist),
}
}
type MockArtistRepo struct {
model.ArtistRepository
data map[string]model.Artist
data map[string]*model.Artist
err bool
}
@@ -21,9 +26,9 @@ func (m *MockArtistRepo) SetError(err bool) {
}
func (m *MockArtistRepo) SetData(artists model.Artists) {
m.data = make(map[string]model.Artist)
for _, a := range artists {
m.data[a.ID] = a
m.data = make(map[string]*model.Artist)
for i, a := range artists {
m.data[a.ID] = &artists[i]
}
}
@@ -40,9 +45,32 @@ func (m *MockArtistRepo) 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
}
func (m *MockArtistRepo) Put(ar *model.Artist) error {
if m.err {
return errors.New("error")
}
if ar.ID == "" {
ar.ID = uuid.NewString()
}
m.data[ar.ID] = ar
return nil
}
func (m *MockArtistRepo) 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
}
var _ model.ArtistRepository = (*MockArtistRepo)(nil)
+22 -9
View File
@@ -2,6 +2,7 @@ package tests
import (
"errors"
"time"
"github.com/google/uuid"
@@ -10,13 +11,13 @@ import (
func CreateMockMediaFileRepo() *MockMediaFileRepo {
return &MockMediaFileRepo{
data: make(map[string]model.MediaFile),
data: make(map[string]*model.MediaFile),
}
}
type MockMediaFileRepo struct {
model.MediaFileRepository
data map[string]model.MediaFile
data map[string]*model.MediaFile
err bool
}
@@ -25,9 +26,9 @@ func (m *MockMediaFileRepo) SetError(err bool) {
}
func (m *MockMediaFileRepo) SetData(mfs model.MediaFiles) {
m.data = make(map[string]model.MediaFile)
for _, mf := range mfs {
m.data[mf.ID] = mf
m.data = make(map[string]*model.MediaFile)
for i, mf := range mfs {
m.data[mf.ID] = &mfs[i]
}
}
@@ -44,22 +45,34 @@ func (m *MockMediaFileRepo) Get(id string) (*model.MediaFile, 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 *MockMediaFileRepo) Put(mf *model.MediaFile) error {
if m.err {
return errors.New("error!")
return errors.New("error")
}
if mf.ID == "" {
mf.ID = uuid.NewString()
}
m.data[mf.ID] = *mf
m.data[mf.ID] = mf
return nil
}
func (m *MockMediaFileRepo) 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 *MockMediaFileRepo) FindByAlbum(artistId string) (model.MediaFiles, error) {
if m.err {
return nil, errors.New("Error!")
@@ -68,7 +81,7 @@ func (m *MockMediaFileRepo) FindByAlbum(artistId string) (model.MediaFiles, erro
i := 0
for _, a := range m.data {
if a.AlbumID == artistId {
res[i] = a
res[i] = *a
i++
}
}