-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmemgodb_test.go
318 lines (272 loc) · 6.92 KB
/
memgodb_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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package fscache
import (
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
var MemgodbTestCases = []any{
struct {
Name string `json:"name"`
Age int `json:"age"`
}{
Name: "Jane Doe",
Age: 25,
},
map[string]any{
"name": "John Doe",
"age": 35,
"colName": "users",
},
map[string]any{
"name": "Jane Dice",
"age": 35,
"colName": "users",
},
}
func Test_Collection(t *testing.T) {
fs := New()
col := fs.DataStore().Collection("user")
assert.NotNil(t, col)
assert.Equal(t, "users", col.collectionName)
}
func Test_Insert_One(t *testing.T) {
fs := New()
// insert single records
var counter int
name := fmt.Sprintf("testCase_%v", counter+1)
for _, v := range MemgodbTestCases {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("user").Insert(v)
require.NoError(t, err)
})
counter++
}
// to insert many records at once
err := fs.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
}
func Test_InsertFromJsonFile(t *testing.T) {
fs := New()
testCases := map[string]struct {
fileName string
expectedError error
}{
"objects [slice] file": {
fileName: "./testJsonFiles/objects.json",
},
"object [map] file": {
fileName: "./testJsonFiles/object.json",
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("user").InsertFromJsonFile(testCase.fileName)
assert.Equal(t, testCase.expectedError, err)
})
}
testCases = map[string]struct {
fileName string
expectedError error
}{
"[string] file": {
fileName: "./testJsonFiles/string.json",
expectedError: errors.New("file must contain either an array of [objects ::: slice] or [object ::: map]"),
},
"[empty] file": {
fileName: "./testJsonFiles/empty.json",
expectedError: errors.New("invalid json file"),
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("user").InsertFromJsonFile(testCase.fileName)
require.Error(t, err)
require.ErrorContains(t, err, testCase.expectedError.Error())
})
}
}
func Test__Filter_First(t *testing.T) {
fs := New()
// insert a new records
err := fs.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
testCases := map[string]struct {
expectedError error
filter map[string]any
}{
"not nil params": {
filter: map[string]any{"age": 35.0}, // filter out records of age 35
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
result, err := fs.DataStore().Collection("users").Filter(testCase.filter).First()
require.NoError(t, err)
assert.NotNil(t, result)
})
}
testCases = map[string]struct {
expectedError error
filter map[string]any
}{
"nil params": {
expectedError: ErrFilterParams,
filter: nil, // for nil params
},
"incorrect params": {
expectedError: ErrRecordNotFound,
filter: map[string]any{"age": 0},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
result, err := fs.DataStore().Collection("users").Filter(testCase.filter).First()
require.ErrorIs(t, err, testCase.expectedError)
assert.Nil(t, result)
})
}
}
func Test_Filter_All(t *testing.T) {
fs := New()
// insert a new records
err := fs.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
testCases := map[string]struct {
expectedError error
filter map[string]any
}{
"not nil params": {
expectedError: nil,
filter: map[string]any{"age": 35.0}, // filter out records of age 35
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
results, err := fs.DataStore().Collection("users").Filter(testCase.filter).All()
require.NoError(t, err)
assert.NotNil(t, results)
})
}
testCases = map[string]struct {
expectedError error
filter map[string]any
}{
"incorrect params": {
expectedError: ErrRecordNotFound,
filter: map[string]any{"age": 0},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
_, err := fs.DataStore().Collection("users").Filter(testCase.filter).All()
require.ErrorIs(t, err, testCase.expectedError)
})
}
}
func Test_Delete_One(t *testing.T) {
fs := New()
// insert a new record
err := fs.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
filters := map[string]map[string]any{
"not nil params": {"age": 35.0}, // filter out record of age 35
}
for name, v := range filters {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("users").Delete(v).One()
require.NoError(t, err)
})
}
filters = map[string]map[string]any{
"nil params": nil, // for nil params
}
for name, v := range filters {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("users").Delete(v).One()
require.Error(t, err)
})
}
}
func Test_Delete_All(t *testing.T) {
ch := New()
// insert a new record
err := ch.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
filters := map[string]map[string]any{
"not nil params": {"age": 35.0}, // filter out record of age 35
}
for name, v := range filters {
t.Run(name, func(t *testing.T) {
err := ch.DataStore().Collection("users").Delete(v).All()
fmt.Println("Delete all Error: ", err)
require.NoError(t, err)
})
}
filters = map[string]map[string]any{
"nil params": nil, // for nil params
}
for name, v := range filters {
t.Run(name, func(t *testing.T) {
err := ch.DataStore().Collection("users").Delete(v).All()
fmt.Println("Delete all Error: ", err)
require.NoError(t, err)
})
}
}
func Test_Update_One(t *testing.T) {
fs := New()
// insert a new record
err := fs.DataStore().Collection("user").Insert(MemgodbTestCases)
require.NoError(t, err)
testCases := map[string]struct {
expectedError error
filter map[string]any
update map[string]any
}{
"correct filter params": {
expectedError: nil,
filter: map[string]any{
"age": 35.0,
},
update: map[string]any{
"age": 29,
},
},
"nil filter params": {
expectedError: ErrFilterParams,
filter: nil,
update: map[string]any{
"age": 28,
},
},
"not found params": {
expectedError: ErrRecordNotFound,
filter: map[string]any{
"age": 300.0,
},
update: map[string]any{
"age": 28,
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
err := fs.DataStore().Collection("user").Update(testCase.filter, testCase.update).One()
if err != nil {
require.ErrorIs(t, err, testCase.expectedError)
}
})
}
}
func Test_Persist(t *testing.T) {
ch := New()
err := ch.DataStore().Persist()
require.NoError(t, err)
}
func Test_LoadDefault(t *testing.T) {
ch := New()
err := ch.DataStore().LoadDefault()
require.NoError(t, err)
}