forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_utils.go
77 lines (64 loc) · 1.94 KB
/
test_utils.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
package kvstore
import (
"context"
"errors"
"strings"
)
// In memory kv store used for testing
type FakeKVStore struct {
store map[Key]string
delError bool
}
func NewFakeKVStore() *FakeKVStore {
return &FakeKVStore{store: make(map[Key]string)}
}
func (f *FakeKVStore) DeletionError(shouldErr bool) {
f.delError = shouldErr
}
func (f *FakeKVStore) Get(ctx context.Context, orgId int64, namespace string, key string) (string, bool, error) {
value := f.store[buildKey(orgId, namespace, key)]
found := value != ""
return value, found, nil
}
func (f *FakeKVStore) Set(ctx context.Context, orgId int64, namespace string, key string, value string) error {
f.store[buildKey(orgId, namespace, key)] = value
return nil
}
func (f *FakeKVStore) Del(ctx context.Context, orgId int64, namespace string, key string) error {
if f.delError {
return errors.New("mocked del error")
}
delete(f.store, buildKey(orgId, namespace, key))
return nil
}
// List all keys with an optional filter. If default values are provided, filter is not applied.
func (f *FakeKVStore) Keys(ctx context.Context, orgId int64, namespace string, keyPrefix string) ([]Key, error) {
res := make([]Key, 0)
for k := range f.store {
if orgId == AllOrganizations && namespace == "" && keyPrefix == "" {
res = append(res, k)
} else if k.OrgId == orgId && k.Namespace == namespace && strings.HasPrefix(k.Key, keyPrefix) {
res = append(res, k)
}
}
return res, nil
}
func (f *FakeKVStore) GetAll(ctx context.Context, orgId int64, namespace string) (map[int64]map[string]string, error) {
items := make(map[int64]map[string]string)
for k := range f.store {
orgId := k.OrgId
namespace := k.Namespace
if _, ok := items[orgId]; !ok {
items[orgId] = make(map[string]string)
}
items[orgId][namespace] = f.store[k]
}
return items, nil
}
func buildKey(orgId int64, namespace string, key string) Key {
return Key{
OrgId: orgId,
Namespace: namespace,
Key: key,
}
}