forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtesting.go
130 lines (112 loc) · 2.99 KB
/
testing.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package store
import (
"context"
"strings"
"sync"
"testing"
"github.com/grafana/grafana/pkg/services/ngalert/models"
)
func NewFakeImageStore(t *testing.T, images ...*models.Image) *FakeImageStore {
imageMap := make(map[string]*models.Image)
for _, image := range images {
imageMap[image.Token] = image
}
return &FakeImageStore{
t: t,
images: imageMap,
}
}
type FakeImageStore struct {
t *testing.T
mtx sync.Mutex
images map[string]*models.Image
}
func (s *FakeImageStore) GetImage(_ context.Context, token string) (*models.Image, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
if image, ok := s.images[token]; ok {
return image, nil
}
return nil, models.ErrImageNotFound
}
func (s *FakeImageStore) GetImageByURL(_ context.Context, url string) (*models.Image, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, image := range s.images {
if image.URL == url {
return image, nil
}
}
return nil, models.ErrImageNotFound
}
func (s *FakeImageStore) URLExists(_ context.Context, url string) (bool, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
for _, image := range s.images {
if image.URL == url {
return true, nil
}
}
return false, nil
}
func (s *FakeImageStore) GetImages(_ context.Context, tokens []string) ([]models.Image, []string, error) {
s.mtx.Lock()
defer s.mtx.Unlock()
images := make([]models.Image, 0, len(tokens))
for _, token := range tokens {
if image, ok := s.images[token]; ok {
images = append(images, *image)
}
}
if len(images) < len(tokens) {
return images, unmatchedTokens(tokens, images), models.ErrImageNotFound
}
return images, nil, nil
}
func (s *FakeImageStore) SaveImage(_ context.Context, image *models.Image) error {
s.mtx.Lock()
defer s.mtx.Unlock()
if image.ID == 0 {
image.ID = int64(len(s.images)) + 1
}
if image.Token == "" {
tmp := strings.Split(image.Path, ".")
image.Token = strings.Join(tmp[:len(tmp)-1], ".")
}
s.images[image.Token] = image
return nil
}
func NewFakeAdminConfigStore(t *testing.T) *FakeAdminConfigStore {
t.Helper()
return &FakeAdminConfigStore{Configs: map[int64]*models.AdminConfiguration{}}
}
type FakeAdminConfigStore struct {
mtx sync.Mutex
Configs map[int64]*models.AdminConfiguration
}
func (f *FakeAdminConfigStore) GetAdminConfiguration(orgID int64) (*models.AdminConfiguration, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
return f.Configs[orgID], nil
}
func (f *FakeAdminConfigStore) GetAdminConfigurations() ([]*models.AdminConfiguration, error) {
f.mtx.Lock()
defer f.mtx.Unlock()
acs := make([]*models.AdminConfiguration, 0, len(f.Configs))
for _, ac := range f.Configs {
acs = append(acs, ac)
}
return acs, nil
}
func (f *FakeAdminConfigStore) DeleteAdminConfiguration(orgID int64) error {
f.mtx.Lock()
defer f.mtx.Unlock()
delete(f.Configs, orgID)
return nil
}
func (f *FakeAdminConfigStore) UpdateAdminConfiguration(cmd UpdateAdminConfigurationCmd) error {
f.mtx.Lock()
defer f.mtx.Unlock()
f.Configs[cmd.AdminConfiguration.OrgID] = cmd.AdminConfiguration
return nil
}