Move mock datastore to tests package
This commit is contained in:
@@ -0,0 +1,77 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/deluan/navidrome/model"
|
||||
)
|
||||
|
||||
func CreateMockAlbumRepo() *MockAlbum {
|
||||
return &MockAlbum{}
|
||||
}
|
||||
|
||||
type MockAlbum struct {
|
||||
model.AlbumRepository
|
||||
data map[string]model.Album
|
||||
all model.Albums
|
||||
err bool
|
||||
Options model.QueryOptions
|
||||
}
|
||||
|
||||
func (m *MockAlbum) SetError(err bool) {
|
||||
m.err = err
|
||||
}
|
||||
|
||||
func (m *MockAlbum) SetData(albums model.Albums) {
|
||||
m.data = make(map[string]model.Album)
|
||||
m.all = albums
|
||||
for _, a := range m.all {
|
||||
m.data[a.ID] = a
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockAlbum) Exists(id string) (bool, error) {
|
||||
if m.err {
|
||||
return false, errors.New("Error!")
|
||||
}
|
||||
_, found := m.data[id]
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (m *MockAlbum) Get(id string) (*model.Album, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
if d, ok := m.data[id]; ok {
|
||||
return &d, nil
|
||||
}
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *MockAlbum) 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 *MockAlbum) FindByArtist(artistId string) (model.Albums, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
var res = make(model.Albums, len(m.data))
|
||||
i := 0
|
||||
for _, a := range m.data {
|
||||
if a.AlbumArtistID == artistId {
|
||||
res[i] = a
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var _ model.AlbumRepository = (*MockAlbum)(nil)
|
||||
@@ -0,0 +1,48 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/deluan/navidrome/model"
|
||||
)
|
||||
|
||||
func CreateMockArtistRepo() *MockArtist {
|
||||
return &MockArtist{}
|
||||
}
|
||||
|
||||
type MockArtist struct {
|
||||
model.ArtistRepository
|
||||
data map[string]model.Artist
|
||||
err bool
|
||||
}
|
||||
|
||||
func (m *MockArtist) SetError(err bool) {
|
||||
m.err = err
|
||||
}
|
||||
|
||||
func (m *MockArtist) SetData(artists model.Artists) {
|
||||
m.data = make(map[string]model.Artist)
|
||||
for _, a := range artists {
|
||||
m.data[a.ID] = a
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockArtist) Exists(id string) (bool, error) {
|
||||
if m.err {
|
||||
return false, errors.New("Error!")
|
||||
}
|
||||
_, found := m.data[id]
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (m *MockArtist) Get(id string) (*model.Artist, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
if d, ok := m.data[id]; ok {
|
||||
return &d, nil
|
||||
}
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
|
||||
var _ model.ArtistRepository = (*MockArtist)(nil)
|
||||
@@ -0,0 +1,64 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/deluan/navidrome/model"
|
||||
)
|
||||
|
||||
func CreateMockMediaFileRepo() *MockMediaFile {
|
||||
return &MockMediaFile{}
|
||||
}
|
||||
|
||||
type MockMediaFile struct {
|
||||
model.MediaFileRepository
|
||||
data map[string]model.MediaFile
|
||||
err bool
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) SetError(err bool) {
|
||||
m.err = err
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) SetData(mfs model.MediaFiles) {
|
||||
m.data = make(map[string]model.MediaFile)
|
||||
for _, mf := range mfs {
|
||||
m.data[mf.ID] = mf
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) Exists(id string) (bool, error) {
|
||||
if m.err {
|
||||
return false, errors.New("Error!")
|
||||
}
|
||||
_, found := m.data[id]
|
||||
return found, nil
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) Get(id string) (*model.MediaFile, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
if d, ok := m.data[id]; ok {
|
||||
return &d, nil
|
||||
}
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
|
||||
func (m *MockMediaFile) FindByAlbum(artistId string) (model.MediaFiles, error) {
|
||||
if m.err {
|
||||
return nil, errors.New("Error!")
|
||||
}
|
||||
var res = make(model.MediaFiles, len(m.data))
|
||||
i := 0
|
||||
for _, a := range m.data {
|
||||
if a.AlbumID == artistId {
|
||||
res[i] = a
|
||||
i++
|
||||
}
|
||||
}
|
||||
|
||||
return res, nil
|
||||
}
|
||||
|
||||
var _ model.MediaFileRepository = (*MockMediaFile)(nil)
|
||||
@@ -0,0 +1,109 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/deluan/navidrome/model"
|
||||
)
|
||||
|
||||
type MockDataStore struct {
|
||||
MockedGenre model.GenreRepository
|
||||
MockedAlbum model.AlbumRepository
|
||||
MockedArtist model.ArtistRepository
|
||||
MockedMediaFile model.MediaFileRepository
|
||||
MockedUser model.UserRepository
|
||||
MockedPlayer model.PlayerRepository
|
||||
MockedTranscoding model.TranscodingRepository
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Album(context.Context) model.AlbumRepository {
|
||||
if db.MockedAlbum == nil {
|
||||
db.MockedAlbum = CreateMockAlbumRepo()
|
||||
}
|
||||
return db.MockedAlbum
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Artist(context.Context) model.ArtistRepository {
|
||||
if db.MockedArtist == nil {
|
||||
db.MockedArtist = CreateMockArtistRepo()
|
||||
}
|
||||
return db.MockedArtist
|
||||
}
|
||||
|
||||
func (db *MockDataStore) MediaFile(context.Context) model.MediaFileRepository {
|
||||
if db.MockedMediaFile == nil {
|
||||
db.MockedMediaFile = CreateMockMediaFileRepo()
|
||||
}
|
||||
return db.MockedMediaFile
|
||||
}
|
||||
|
||||
func (db *MockDataStore) MediaFolder(context.Context) model.MediaFolderRepository {
|
||||
return struct{ model.MediaFolderRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Genre(context.Context) model.GenreRepository {
|
||||
if db.MockedGenre != nil {
|
||||
return db.MockedGenre
|
||||
}
|
||||
return struct{ model.GenreRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Playlist(context.Context) model.PlaylistRepository {
|
||||
return struct{ model.PlaylistRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) PlayQueue(context.Context) model.PlayQueueRepository {
|
||||
return struct{ model.PlayQueueRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Property(context.Context) model.PropertyRepository {
|
||||
return struct{ model.PropertyRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) User(context.Context) model.UserRepository {
|
||||
if db.MockedUser == nil {
|
||||
db.MockedUser = &mockedUserRepo{}
|
||||
}
|
||||
return db.MockedUser
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Transcoding(context.Context) model.TranscodingRepository {
|
||||
if db.MockedTranscoding != nil {
|
||||
return db.MockedTranscoding
|
||||
}
|
||||
return struct{ model.TranscodingRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Player(context.Context) model.PlayerRepository {
|
||||
if db.MockedPlayer != nil {
|
||||
return db.MockedPlayer
|
||||
}
|
||||
return struct{ model.PlayerRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) WithTx(block func(db model.DataStore) error) error {
|
||||
return block(db)
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Resource(ctx context.Context, m interface{}) model.ResourceRepository {
|
||||
return struct{ model.ResourceRepository }{}
|
||||
}
|
||||
|
||||
func (db *MockDataStore) GC(ctx context.Context, rootFolder string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type mockedUserRepo struct {
|
||||
model.UserRepository
|
||||
}
|
||||
|
||||
func (u *mockedUserRepo) FindByUsername(username string) (*model.User, error) {
|
||||
if username != "admin" {
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
return &model.User{UserName: "admin", Password: "wordpass"}, nil
|
||||
}
|
||||
|
||||
func (u *mockedUserRepo) UpdateLastAccessAt(id string) error {
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package tests
|
||||
|
||||
import "github.com/deluan/navidrome/model"
|
||||
|
||||
type MockTranscodingRepository struct {
|
||||
model.TranscodingRepository
|
||||
}
|
||||
|
||||
func (m *MockTranscodingRepository) Get(id string) (*model.Transcoding, error) {
|
||||
return &model.Transcoding{ID: id, TargetFormat: "mp3", DefaultBitRate: 160}, nil
|
||||
}
|
||||
|
||||
func (m *MockTranscodingRepository) FindByFormat(format string) (*model.Transcoding, error) {
|
||||
switch format {
|
||||
case "mp3":
|
||||
return &model.Transcoding{ID: "mp31", TargetFormat: "mp3", DefaultBitRate: 160}, nil
|
||||
case "oga":
|
||||
return &model.Transcoding{ID: "oga1", TargetFormat: "oga", DefaultBitRate: 128}, nil
|
||||
default:
|
||||
return nil, model.ErrNotFound
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user