navidrome/persistence/mock_mediafile_repo.go

70 lines
1.2 KiB
Go
Raw Normal View History

package persistence
import (
"encoding/json"
2016-03-03 06:46:23 +01:00
"errors"
"fmt"
2017-04-01 15:47:14 +02:00
"github.com/cloudsonic/sonic-server/domain"
)
func CreateMockMediaFileRepo() *MockMediaFile {
return &MockMediaFile{}
}
type MockMediaFile struct {
domain.MediaFileRepository
data map[string]domain.MediaFile
err bool
}
func (m *MockMediaFile) SetError(err bool) {
m.err = err
}
func (m *MockMediaFile) SetData(j string, size int) {
m.data = make(map[string]domain.MediaFile)
2016-03-04 03:01:55 +01:00
var l = make(domain.MediaFiles, size)
err := json.Unmarshal([]byte(j), &l)
if err != nil {
fmt.Println("ERROR: ", err)
}
for _, a := range l {
m.data[a.Id] = a
}
}
func (m *MockMediaFile) Exists(id string) (bool, error) {
if m.err {
return false, errors.New("Error!")
}
2016-03-03 06:46:23 +01:00
_, found := m.data[id]
return found, nil
}
func (m *MockMediaFile) Get(id string) (*domain.MediaFile, error) {
if m.err {
return nil, errors.New("Error!")
}
if d, ok := m.data[id]; ok {
return &d, nil
}
return nil, domain.ErrNotFound
}
func (m *MockMediaFile) FindByAlbum(artistId string) (domain.MediaFiles, error) {
if m.err {
return nil, errors.New("Error!")
}
2016-03-04 03:01:55 +01:00
var res = make(domain.MediaFiles, len(m.data))
i := 0
for _, a := range m.data {
if a.AlbumId == artistId {
res[i] = a
i++
}
}
return res, nil
2016-03-03 06:46:23 +01:00
}