Some small refactorings
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
package tests
|
||||
|
||||
import (
|
||||
"github.com/navidrome/navidrome/model"
|
||||
)
|
||||
|
||||
type MockedGenreRepo struct {
|
||||
Error error
|
||||
data map[string]model.Genre
|
||||
}
|
||||
|
||||
func (r *MockedGenreRepo) init() {
|
||||
if r.data == nil {
|
||||
r.data = make(map[string]model.Genre)
|
||||
}
|
||||
}
|
||||
|
||||
func (r *MockedGenreRepo) GetAll(...model.QueryOptions) (model.Genres, error) {
|
||||
if r.Error != nil {
|
||||
return nil, r.Error
|
||||
}
|
||||
r.init()
|
||||
|
||||
var all model.Genres
|
||||
for _, g := range r.data {
|
||||
all = append(all, g)
|
||||
}
|
||||
return all, nil
|
||||
}
|
||||
|
||||
func (r *MockedGenreRepo) Put(g *model.Genre) error {
|
||||
if r.Error != nil {
|
||||
return r.Error
|
||||
}
|
||||
r.init()
|
||||
r.data[g.ID] = *g
|
||||
return nil
|
||||
}
|
||||
@@ -46,10 +46,10 @@ func (db *MockDataStore) MediaFolder(context.Context) model.MediaFolderRepositor
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Genre(context.Context) model.GenreRepository {
|
||||
if db.MockedGenre != nil {
|
||||
return db.MockedGenre
|
||||
if db.MockedGenre == nil {
|
||||
db.MockedGenre = &MockedGenreRepo{}
|
||||
}
|
||||
return struct{ model.GenreRepository }{}
|
||||
return db.MockedGenre
|
||||
}
|
||||
|
||||
func (db *MockDataStore) Playlist(context.Context) model.PlaylistRepository {
|
||||
|
||||
+10
-10
@@ -4,8 +4,8 @@ import "github.com/navidrome/navidrome/model"
|
||||
|
||||
type MockedPropertyRepo struct {
|
||||
model.PropertyRepository
|
||||
data map[string]string
|
||||
err error
|
||||
Error error
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func (p *MockedPropertyRepo) init() {
|
||||
@@ -15,8 +15,8 @@ func (p *MockedPropertyRepo) init() {
|
||||
}
|
||||
|
||||
func (p *MockedPropertyRepo) Put(id string, value string) error {
|
||||
if p.err != nil {
|
||||
return p.err
|
||||
if p.Error != nil {
|
||||
return p.Error
|
||||
}
|
||||
p.init()
|
||||
p.data[id] = value
|
||||
@@ -24,8 +24,8 @@ func (p *MockedPropertyRepo) Put(id string, value string) error {
|
||||
}
|
||||
|
||||
func (p *MockedPropertyRepo) Get(id string) (string, error) {
|
||||
if p.err != nil {
|
||||
return "", p.err
|
||||
if p.Error != nil {
|
||||
return "", p.Error
|
||||
}
|
||||
p.init()
|
||||
if v, ok := p.data[id]; ok {
|
||||
@@ -35,8 +35,8 @@ func (p *MockedPropertyRepo) Get(id string) (string, error) {
|
||||
}
|
||||
|
||||
func (p *MockedPropertyRepo) Delete(id string) error {
|
||||
if p.err != nil {
|
||||
return p.err
|
||||
if p.Error != nil {
|
||||
return p.Error
|
||||
}
|
||||
p.init()
|
||||
if _, ok := p.data[id]; ok {
|
||||
@@ -47,8 +47,8 @@ func (p *MockedPropertyRepo) Delete(id string) error {
|
||||
}
|
||||
|
||||
func (p *MockedPropertyRepo) DefaultGet(id string, defaultValue string) (string, error) {
|
||||
if p.err != nil {
|
||||
return "", p.err
|
||||
if p.Error != nil {
|
||||
return "", p.Error
|
||||
}
|
||||
p.init()
|
||||
v, err := p.Get(id)
|
||||
|
||||
@@ -12,16 +12,22 @@ type MockShareRepo struct {
|
||||
|
||||
Entity interface{}
|
||||
Cols []string
|
||||
Err error
|
||||
Error error
|
||||
}
|
||||
|
||||
func (m *MockShareRepo) Save(entity interface{}) (string, error) {
|
||||
if m.Error != nil {
|
||||
return "", m.Error
|
||||
}
|
||||
m.Entity = entity
|
||||
return "id", m.Err
|
||||
return "id", nil
|
||||
}
|
||||
|
||||
func (m *MockShareRepo) Update(entity interface{}, cols ...string) error {
|
||||
if m.Error != nil {
|
||||
return m.Error
|
||||
}
|
||||
m.Entity = entity
|
||||
m.Cols = cols
|
||||
return m.Err
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -4,8 +4,8 @@ import "github.com/navidrome/navidrome/model"
|
||||
|
||||
type MockedUserPropsRepo struct {
|
||||
model.UserPropsRepository
|
||||
data map[string]string
|
||||
err error
|
||||
Error error
|
||||
data map[string]string
|
||||
}
|
||||
|
||||
func (p *MockedUserPropsRepo) init() {
|
||||
@@ -15,8 +15,8 @@ func (p *MockedUserPropsRepo) init() {
|
||||
}
|
||||
|
||||
func (p *MockedUserPropsRepo) Put(userId, key string, value string) error {
|
||||
if p.err != nil {
|
||||
return p.err
|
||||
if p.Error != nil {
|
||||
return p.Error
|
||||
}
|
||||
p.init()
|
||||
p.data[userId+key] = value
|
||||
@@ -24,8 +24,8 @@ func (p *MockedUserPropsRepo) Put(userId, key string, value string) error {
|
||||
}
|
||||
|
||||
func (p *MockedUserPropsRepo) Get(userId, key string) (string, error) {
|
||||
if p.err != nil {
|
||||
return "", p.err
|
||||
if p.Error != nil {
|
||||
return "", p.Error
|
||||
}
|
||||
p.init()
|
||||
if v, ok := p.data[userId+key]; ok {
|
||||
@@ -35,8 +35,8 @@ func (p *MockedUserPropsRepo) Get(userId, key string) (string, error) {
|
||||
}
|
||||
|
||||
func (p *MockedUserPropsRepo) Delete(userId, key string) error {
|
||||
if p.err != nil {
|
||||
return p.err
|
||||
if p.Error != nil {
|
||||
return p.Error
|
||||
}
|
||||
p.init()
|
||||
if _, ok := p.data[userId+key]; ok {
|
||||
@@ -47,8 +47,8 @@ func (p *MockedUserPropsRepo) Delete(userId, key string) error {
|
||||
}
|
||||
|
||||
func (p *MockedUserPropsRepo) DefaultGet(userId, key string, defaultValue string) (string, error) {
|
||||
if p.err != nil {
|
||||
return "", p.err
|
||||
if p.Error != nil {
|
||||
return "", p.Error
|
||||
}
|
||||
p.init()
|
||||
v, err := p.Get(userId, key)
|
||||
|
||||
@@ -15,20 +15,20 @@ func CreateMockUserRepo() *MockedUserRepo {
|
||||
|
||||
type MockedUserRepo struct {
|
||||
model.UserRepository
|
||||
Err error
|
||||
Data map[string]*model.User
|
||||
Error error
|
||||
Data map[string]*model.User
|
||||
}
|
||||
|
||||
func (u *MockedUserRepo) CountAll(qo ...model.QueryOptions) (int64, error) {
|
||||
if u.Err != nil {
|
||||
return 0, u.Err
|
||||
if u.Error != nil {
|
||||
return 0, u.Error
|
||||
}
|
||||
return int64(len(u.Data)), nil
|
||||
}
|
||||
|
||||
func (u *MockedUserRepo) Put(usr *model.User) error {
|
||||
if u.Err != nil {
|
||||
return u.Err
|
||||
if u.Error != nil {
|
||||
return u.Error
|
||||
}
|
||||
if usr.ID == "" {
|
||||
usr.ID = base64.StdEncoding.EncodeToString([]byte(usr.UserName))
|
||||
@@ -39,8 +39,8 @@ func (u *MockedUserRepo) Put(usr *model.User) error {
|
||||
}
|
||||
|
||||
func (u *MockedUserRepo) FindByUsername(username string) (*model.User, error) {
|
||||
if u.Err != nil {
|
||||
return nil, u.Err
|
||||
if u.Error != nil {
|
||||
return nil, u.Error
|
||||
}
|
||||
usr, ok := u.Data[strings.ToLower(username)]
|
||||
if !ok {
|
||||
@@ -54,5 +54,5 @@ func (u *MockedUserRepo) FindByUsernameWithPassword(username string) (*model.Use
|
||||
}
|
||||
|
||||
func (u *MockedUserRepo) UpdateLastLoginAt(id string) error {
|
||||
return u.Err
|
||||
return u.Error
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user