forked from leesper/couchdb-golang
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapping_test.go
387 lines (318 loc) · 7.96 KB
/
mapping_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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package couchdb
import (
"math/big"
"reflect"
"testing"
)
type Post struct {
Title string `json:"title"`
Document
}
func TestAutomaticID(t *testing.T) {
post := Post{Title: "Foo bar"}
if post.GetID() != "" {
t.Error("post ID not empty", post.ID)
}
err := Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
if post.GetID() == "" {
t.Error("post ID empty")
}
if post.GetRev() == "" {
t.Error("post rev empty")
}
doc, err := mappingDB.Get(post.GetID(), nil)
if err != nil {
t.Fatal("db get error", err)
}
if doc["title"].(string) != "Foo bar" {
t.Errorf("doc title %s want Foo bar", doc["title"].(string))
}
if doc["_rev"].(string) != post.GetRev() {
t.Errorf("post rev %s want %s", post.GetRev(), doc["_rev"].(string))
}
}
func TestExplicitIDByInit(t *testing.T) {
post := Post{Document: DocumentWithID("foo_bar"), Title: "Foo bar"}
if post.GetID() != "foo_bar" {
t.Fatalf("post ID %s want foo_bar", post.GetID())
}
err := Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
doc, err := mappingDB.Get(post.GetID(), nil)
if err != nil {
t.Fatal("db get error", err)
}
if doc["title"].(string) != "Foo bar" {
t.Errorf("doc title %s want Foo bar", doc["title"].(string))
}
if doc["_id"].(string) != post.GetID() {
t.Errorf("post id %s want %s", post.GetID(), doc["_id"].(string))
}
if doc["_rev"].(string) != post.GetRev() {
t.Errorf("post rev %s want %s", post.GetRev(), doc["_rev"].(string))
}
}
func TestExplicitIDBySetter(t *testing.T) {
post := Post{Title: "Foo bar"}
post.SetID("foo_baz")
if post.GetID() != "foo_baz" {
t.Errorf("post ID %s want foo_bar", post.GetID())
}
err := Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
doc, err := mappingDB.Get(post.GetID(), nil)
if err != nil {
t.Fatal("db get error", err)
}
if doc["title"].(string) != "Foo bar" {
t.Errorf("doc title %s want Foo bar", doc["title"].(string))
}
if doc["_id"].(string) != post.GetID() {
t.Errorf("post id %s want %s", post.GetID(), doc["_id"].(string))
}
if doc["_rev"].(string) != post.GetRev() {
t.Errorf("post rev %s want %s", post.GetRev(), doc["_rev"].(string))
}
}
func TestChangeIDFailure(t *testing.T) {
post := Post{Title: "Foo bar"}
err := Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
err = Load(mappingDB, post.GetID(), &post)
if err != nil {
t.Fatal("document load error", err)
}
err = post.SetID("foo_bar")
if err != ErrSetID {
t.Errorf("document set id error %v want %v", err, ErrSetID)
}
}
type NotDocument struct{}
func TestNotADocument(t *testing.T) {
notDocument := NotDocument{}
err := Store(mappingDB, ¬Document)
if err != ErrNotDocumentEmbedded {
t.Fatalf("store error %v want %v", err, ErrNotDocumentEmbedded)
}
}
type User struct {
Name string `json:"name"`
Age int `json:"age"`
Marriage Marriage `json:"marriage"`
Document
}
type Marriage struct {
Male bool `json:"male"`
Married string `json:"married"`
}
func TestNestedStruct(t *testing.T) {
jack := User{
Name: "Jack",
Age: 18,
Document: DocumentWithID("jack"),
Marriage: Marriage{Male: true, Married: "Lucy"},
}
err := Store(mappingDB, &jack)
if err != nil {
t.Fatal("store error", err)
}
doc, err := mappingDB.Get("jack", nil)
if err != nil {
t.Fatal("db get error", err)
}
objDoc, err := ToJSONCompatibleMap(jack)
if err != nil {
t.Fatal("to json compatible error", err)
}
if !reflect.DeepEqual(doc, objDoc) {
t.Error("doc and obj not equal")
}
docObj := User{}
err = FromJSONCompatibleMap(&docObj, doc)
if err != nil {
t.Fatal("from json compatible error", err)
}
if !reflect.DeepEqual(jack, docObj) {
t.Error("objs not equal")
}
}
func TestBatchUpdate(t *testing.T) {
post1 := Post{Title: "Foo bar"}
post2 := Post{Title: "Foo baz"}
postMap1, err := ToJSONCompatibleMap(post1)
if err != nil {
t.Fatal("to json compatible error", err)
}
postMap2, err := ToJSONCompatibleMap(post2)
if err != nil {
t.Fatal("to json compatible error", err)
}
results, err := mappingDB.Update([]map[string]interface{}{postMap1, postMap2}, nil)
if err != nil {
t.Fatal("db update error", err)
}
if len(results) != 2 {
t.Fatalf("len(results) = %d want 2", len(results))
}
for idx, res := range results {
if res.Err != nil {
t.Errorf("result %d error %v", idx, res.Err)
}
}
}
func TestStoreExisting(t *testing.T) {
post := Post{Title: "Foo bar"}
err := Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
err = Store(mappingDB, &post)
if err != nil {
t.Fatal("document store error", err)
}
results, err := mappingDB.View("_all_docs", nil, nil)
if err != nil {
t.Fatal("db view _all_docs error", err)
}
rows, err := results.Rows()
if err != nil {
t.Fatal("rows error", err)
}
total := 0
for _, row := range rows {
if post.GetID() == row.ID {
total++
}
}
if total != 1 {
t.Errorf("total %d want 1", total)
}
}
type PostWithComment struct {
Title string `json:"title"`
Comments []map[string]string `json:"comments"`
Document
}
func TestCompareDocWithObj(t *testing.T) {
postDoc := map[string]interface{}{
"comments": []map[string]string{
{"author": "Joe", "content": "Hey"},
},
}
err := mappingDB.Set("test", postDoc)
if err != nil {
t.Fatal("db set error", err)
}
post1 := PostWithComment{}
err = Load(mappingDB, "test", &post1)
if err != nil {
t.Fatal("document load error", err)
}
post2 := PostWithComment{}
err = FromJSONCompatibleMap(&post2, postDoc)
if err != nil {
t.Fatal("from map error", err)
}
if !reflect.DeepEqual(post1, post2) {
t.Errorf("post1 %v != post2 %v", post1, post2)
}
}
type Thing struct {
Numbers []float64
Document
}
func compareFloat(x, y float64) int {
a := big.NewFloat(x)
b := big.NewFloat(y)
return a.Cmp(b)
}
func TestSliceFieldFloat(t *testing.T) {
err := mappingDB.Set("float", map[string]interface{}{"numbers": []float64{1.0, 2.0}})
if err != nil {
t.Fatal("db set error", err)
}
thing := Thing{}
err = Load(mappingDB, "float", &thing)
if err != nil {
t.Fatal("document load error", err)
}
if compareFloat(thing.Numbers[0], 1.0) != 0 {
t.Errorf("thing numbers[0] %v want 1.0", thing.Numbers[0])
}
}
func TestViewFieldProperty(t *testing.T) {
err := Store(mappingDB, &testItem)
if err != nil {
t.Fatal("document store error", err)
}
viewDef, err := testItem.withIncludeDocs()
if err != nil {
t.Fatal("view with include docs error", err)
}
results, err := viewDef.View(mappingDB, nil)
if err != nil {
t.Fatal("view definition error", err)
}
rows, err := results.Rows()
if err != nil {
t.Fatal("rows error", err)
}
for _, row := range rows {
val := row.Val.(map[string]interface{})
id, rev := val["_id"].(string), val["_rev"].(string)
if id == testItem.GetID() {
if rev != testItem.GetRev() {
t.Errorf("rows[0] Rev %s want %s", rev, testItem.GetRev())
}
}
}
}
func TestView(t *testing.T) {
err := Store(mappingDB, &testItem)
if err != nil {
t.Fatal("document store error", err)
}
results, err := mappingDB.View("test/withoutIncludeDocs", nil, nil)
if err != nil {
t.Fatal("db view error", err)
}
rows, err := results.Rows()
if err != nil {
t.Fatal("rows error", err)
}
for _, row := range rows {
val := row.Val.(map[string]interface{})
id, rev := val["_id"].(string), val["_rev"].(string)
if id == testItem.GetID() {
if rev != testItem.GetRev() {
t.Errorf("rows[0] Rev %s want %s", rev, testItem.GetRev())
}
}
}
results, err = mappingDB.View("test/withIncludeDocs", nil, nil)
if err != nil {
t.Fatal("db view error", err)
}
rows, err = results.Rows()
if err != nil {
t.Fatal("rows error", err)
}
for _, row := range rows {
val := row.Val.(map[string]interface{})
id, rev := val["_id"].(string), val["_rev"].(string)
if id == testItem.GetID() {
if rev != testItem.GetRev() {
t.Errorf("rows[0] Rev %s want %s", rev, testItem.GetRev())
}
}
}
}