2021-06-23 22:47:32 +02:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import "github.com/navidrome/navidrome/model"
|
|
|
|
|
|
|
|
type MockedUserPropsRepo struct {
|
|
|
|
model.UserPropsRepository
|
2021-08-22 17:46:52 +02:00
|
|
|
Error error
|
|
|
|
data map[string]string
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (p *MockedUserPropsRepo) init() {
|
|
|
|
if p.data == nil {
|
|
|
|
p.data = make(map[string]string)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-26 04:21:37 +02:00
|
|
|
func (p *MockedUserPropsRepo) Put(userId, key string, value string) error {
|
2021-08-22 17:46:52 +02:00
|
|
|
if p.Error != nil {
|
|
|
|
return p.Error
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
p.init()
|
2021-06-26 04:21:37 +02:00
|
|
|
p.data[userId+key] = value
|
2021-06-23 22:47:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-26 04:21:37 +02:00
|
|
|
func (p *MockedUserPropsRepo) Get(userId, key string) (string, error) {
|
2021-08-22 17:46:52 +02:00
|
|
|
if p.Error != nil {
|
|
|
|
return "", p.Error
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
p.init()
|
2021-06-26 04:21:37 +02:00
|
|
|
if v, ok := p.data[userId+key]; ok {
|
2021-06-23 22:47:32 +02:00
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
return "", model.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-06-26 04:21:37 +02:00
|
|
|
func (p *MockedUserPropsRepo) Delete(userId, key string) error {
|
2021-08-22 17:46:52 +02:00
|
|
|
if p.Error != nil {
|
|
|
|
return p.Error
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
p.init()
|
2021-06-26 04:21:37 +02:00
|
|
|
if _, ok := p.data[userId+key]; ok {
|
|
|
|
delete(p.data, userId+key)
|
2021-06-23 22:47:32 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return model.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2021-06-26 04:21:37 +02:00
|
|
|
func (p *MockedUserPropsRepo) DefaultGet(userId, key string, defaultValue string) (string, error) {
|
2021-08-22 17:46:52 +02:00
|
|
|
if p.Error != nil {
|
|
|
|
return "", p.Error
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
p.init()
|
2021-06-26 04:21:37 +02:00
|
|
|
v, err := p.Get(userId, key)
|
2021-06-23 22:47:32 +02:00
|
|
|
if err != nil {
|
2022-10-01 02:18:14 +02:00
|
|
|
return defaultValue, nil //nolint:nilerr
|
2021-06-23 22:47:32 +02:00
|
|
|
}
|
|
|
|
return v, nil
|
|
|
|
}
|