This repository has been archived by the owner on Apr 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
kv_mock_test.go
101 lines (81 loc) · 2.41 KB
/
kv_mock_test.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
package staert
import (
"errors"
"strings"
"github.com/abronan/valkeyrie/store"
)
// Extremely limited mock store so we can test initialization
type Mock struct {
Error bool
KVPairs []*store.KVPair
WatchTreeMethod func() <-chan []*store.KVPair
ListError error
GetError error
}
func (s *Mock) Put(key string, value []byte, opts *store.WriteOptions) error {
s.KVPairs = append(s.KVPairs, &store.KVPair{Key: key, Value: value, LastIndex: 0})
return nil
}
func (s *Mock) Get(key string, options *store.ReadOptions) (*store.KVPair, error) {
if s.Error {
return nil, errors.New("error")
}
if s.GetError != nil {
return nil, s.GetError
}
for _, kvPair := range s.KVPairs {
if kvPair.Key == key {
return kvPair, nil
}
}
return nil, nil
}
func (s *Mock) Delete(key string) error {
return errors.New("delete not supported")
}
// Exists mock
func (s *Mock) Exists(key string, options *store.ReadOptions) (bool, error) {
return false, errors.New("exists not supported")
}
// Watch mock
func (s *Mock) Watch(key string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan *store.KVPair, error) {
return nil, errors.New("watch not supported")
}
// WatchTree mock
func (s *Mock) WatchTree(prefix string, stopCh <-chan struct{}, options *store.ReadOptions) (<-chan []*store.KVPair, error) {
return s.WatchTreeMethod(), nil
}
// NewLock mock
func (s *Mock) NewLock(key string, options *store.LockOptions) (store.Locker, error) {
return nil, errors.New("NewLock not supported")
}
// List mock
func (s *Mock) List(prefix string, options *store.ReadOptions) ([]*store.KVPair, error) {
if s.Error {
return nil, errors.New("error")
}
var kv []*store.KVPair
for _, kvPair := range s.KVPairs {
if s.ListError != nil {
return nil, s.ListError
}
if strings.HasPrefix(kvPair.Key, prefix) && kvPair.Key != prefix {
kv = append(kv, kvPair)
}
}
return kv, nil
}
// DeleteTree mock
func (s *Mock) DeleteTree(prefix string) error {
return errors.New("DeleteTree not supported")
}
// AtomicPut mock
func (s *Mock) AtomicPut(key string, value []byte, previous *store.KVPair, opts *store.WriteOptions) (bool, *store.KVPair, error) {
return false, nil, errors.New("AtomicPut not supported")
}
// AtomicDelete mock
func (s *Mock) AtomicDelete(key string, previous *store.KVPair) (bool, error) {
return false, errors.New("AtomicDelete not supported")
}
// Close mock
func (s *Mock) Close() {}