forked from navidrome/navidrome
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmock_mediafile_repo.go
104 lines (89 loc) · 2 KB
/
mock_mediafile_repo.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package tests
import (
"errors"
"time"
"github.com/google/uuid"
"github.com/navidrome/navidrome/utils/slice"
"golang.org/x/exp/maps"
"github.com/navidrome/navidrome/model"
)
func CreateMockMediaFileRepo() *MockMediaFileRepo {
return &MockMediaFileRepo{
data: make(map[string]*model.MediaFile),
}
}
type MockMediaFileRepo struct {
model.MediaFileRepository
data map[string]*model.MediaFile
err bool
}
func (m *MockMediaFileRepo) SetError(err bool) {
m.err = err
}
func (m *MockMediaFileRepo) SetData(mfs model.MediaFiles) {
m.data = make(map[string]*model.MediaFile)
for i, mf := range mfs {
m.data[mf.ID] = &mfs[i]
}
}
func (m *MockMediaFileRepo) Exists(id string) (bool, error) {
if m.err {
return false, errors.New("error")
}
_, found := m.data[id]
return found, nil
}
func (m *MockMediaFileRepo) 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 *MockMediaFileRepo) GetAll(...model.QueryOptions) (model.MediaFiles, error) {
if m.err {
return nil, errors.New("error")
}
values := maps.Values(m.data)
return slice.Map(values, func(p *model.MediaFile) model.MediaFile {
return *p
}), nil
}
func (m *MockMediaFileRepo) Put(mf *model.MediaFile) error {
if m.err {
return errors.New("error")
}
if mf.ID == "" {
mf.ID = uuid.NewString()
}
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")
}
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 = (*MockMediaFileRepo)(nil)