This repository has been archived by the owner on Feb 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory_test.go
194 lines (156 loc) · 3.18 KB
/
memory_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
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package gokv
import (
"github.com/philippgille/gokv/encoding"
"github.com/tdakkota/joe-gokv-memory/test"
"go.uber.org/zap"
"testing"
)
func createMemory() (*MemoryStore, *test.MockStore) {
m := test.NewMockStore(encoding.JSON)
v, _ := NewMemory(m)
return v, m
}
const key = "key"
const invalidKey = ""
const value = "value"
func TestMemoryStore_Set(t *testing.T) {
t.Run("valid key", func(t *testing.T) {
memory, store := createMemory()
err := memory.Set(key, []byte(value))
if err != nil {
t.Error(err)
return
}
if data, ok := store.Map()[key]; ok {
var v []byte
err := store.Codec.Unmarshal(data, &v)
if err != nil {
t.Error(err)
return
}
if string(v) != value {
t.Errorf("expected %s, got %s", value, string(v))
}
} else {
t.Error("key doesn't exists")
}
})
t.Run("invalid key", func(t *testing.T) {
memory, _ := createMemory()
err := memory.Set(invalidKey, []byte(value))
if err == nil {
t.Error("error expected")
return
}
})
}
func TestMemoryStore_Get(t *testing.T) {
t.Run("valid key", func(t *testing.T) {
memory, store := createMemory()
data, err := store.Codec.Marshal([]byte(value))
if err != nil {
t.Error(err)
return
}
store.Map()[key] = data
v, ok, err := memory.Get(key)
if err != nil {
t.Error(err)
return
}
if !ok {
t.Error("expected true")
}
if string(v) != value {
t.Errorf("expected %s, got %s", value, string(v))
}
})
t.Run("invalid key", func(t *testing.T) {
memory, _ := createMemory()
_, ok, err := memory.Get(invalidKey)
if ok || err == nil {
t.Error("error expected")
return
}
})
}
func TestMemoryStore_Delete(t *testing.T) {
t.Run("valid key", func(t *testing.T) {
memory, store := createMemory()
data, err := store.Codec.Marshal([]byte(value))
if err != nil {
t.Error(err)
return
}
store.Map()[key] = data
ok, err := memory.Delete(key)
if err != nil {
t.Error(err)
return
}
if !ok {
t.Error("expected true")
}
})
t.Run("invalid key", func(t *testing.T) {
memory, _ := createMemory()
ok, err := memory.Delete(invalidKey)
if err == nil {
t.Error("error expected")
}
if ok {
t.Error("expected false")
}
})
}
func TestMemoryStore_Keys(t *testing.T) {
memory, _ := createMemory()
err := memory.Set(key, []byte(value))
if err != nil {
t.Error(err)
return
}
keys, err := memory.Keys()
if err != nil {
t.Error(err)
return
}
if len(keys) < 1 || keys[0] != key {
t.Error("expected one key:", key)
}
}
func TestMemoryStore_Close(t *testing.T) {
memory, store := createMemory()
err := memory.Close()
if err != nil {
t.Error(err)
return
}
if !store.Closed() {
t.Error("expected that Store is closed")
}
}
func TestNewMemory(t *testing.T) {
logger := zap.L()
m, err := NewMemory(test.NewMockStore(encoding.JSON), WithLogger(logger))
if err != nil {
t.Error(err)
return
}
if m.logger != logger {
t.Error("expected equal")
return
}
err = m.Set(key, []byte(value))
if err != nil {
t.Error(err)
return
}
}
func TestMemory(t *testing.T) {
m := Memory(test.NewMockStore(encoding.JSON))
if m == nil {
t.Error("expected *joe.Module instance, not nil")
return
}
}