forked from rhinoman/couchdb-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bulk_docs_test.go
121 lines (109 loc) · 2.97 KB
/
bulk_docs_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
package couchdb
import "testing"
func TestBulkDocumentClosed(t *testing.T) {
var err error
dbName := createTestDb(t)
conn := getConnection(t)
db := conn.SelectDB(dbName, adminAuth)
bulk := db.NewBulkDocument()
theDoc := TestDocument{
Title: "My Document",
Note: "This is my note",
}
theID := getUuid()
err = bulk.Save(theDoc, theID, "")
errorify(t, err)
// first time
_, err = bulk.Commit()
errorify(t, err)
// second times
_, err = bulk.Commit()
if err == nil {
t.Log("ERROR: Must be caused exception when Commit() for second times")
}
deleteTestDb(t, dbName)
}
func TestBulkDocumentInsertUpdateDelete(t *testing.T) {
var err error
dbName := createTestDb(t)
conn := getConnection(t)
db := conn.SelectDB(dbName, adminAuth)
theID1 := getUuid()
theRev1 := ""
theDoc1 := TestDocument{
Title: "My Document " + theID1,
Note: "This is my note",
}
theID2 := getUuid()
theRev2 := ""
theDoc2 := TestDocument{
Title: "My Document " + theID2,
Note: "This is my note",
}
bulkInsert := db.NewBulkDocument()
okInsertTheDoc1 := false
err = bulkInsert.Save(theDoc1, theID1, "")
errorify(t, err)
okInsertTheDoc2 := false
err = bulkInsert.Save(theDoc2, theID2, "")
errorify(t, err)
insertResults, err := bulkInsert.Commit()
errorify(t, err)
for _, insertResult := range insertResults {
if insertResult.ID == theID1 {
theRev1 = insertResult.Revision
okInsertTheDoc1 = insertResult.Ok
} else if insertResult.ID == theID2 {
theRev2 = insertResult.Revision
okInsertTheDoc2 = insertResult.Ok
}
}
if !(okInsertTheDoc1 && okInsertTheDoc2) {
t.Log("ERROR: failed to insert documents")
}
bulkUpdate := db.NewBulkDocument()
okUpdateTheDoc1 := false
theDoc1.Note = theDoc1.Note + " " + theRev1
err = bulkUpdate.Save(theDoc1, theID1, theRev1)
errorify(t, err)
okUpdateTheDoc2 := false
theDoc2.Note = theDoc2.Note + " " + theRev2
err = bulkUpdate.Save(theDoc2, theID2, theRev2)
errorify(t, err)
updateResults, err := bulkUpdate.Commit()
errorify(t, err)
for _, updateResult := range updateResults {
if updateResult.ID == theID1 {
theRev1 = updateResult.Revision
okUpdateTheDoc1 = updateResult.Ok
} else if updateResult.ID == theID2 {
theRev2 = updateResult.Revision
okUpdateTheDoc2 = updateResult.Ok
}
}
if !(okUpdateTheDoc1 && okUpdateTheDoc2) {
t.Log("ERROR: failed to update documents")
}
bulkDelete := db.NewBulkDocument()
okDeleteTheDoc1 := false
err = bulkDelete.Delete(theID1, theRev1)
errorify(t, err)
okDeleteTheDoc2 := false
err = bulkDelete.Delete(theID2, theRev2)
errorify(t, err)
deleteResults, err := bulkDelete.Commit()
errorify(t, err)
for _, deleteResult := range deleteResults {
if deleteResult.ID == theID1 {
theRev1 = deleteResult.Revision
okDeleteTheDoc1 = deleteResult.Ok
} else if deleteResult.ID == theID2 {
theRev2 = deleteResult.Revision
okDeleteTheDoc2 = deleteResult.Ok
}
}
if !(okDeleteTheDoc1 && okDeleteTheDoc2) {
t.Log("ERROR: failed to delete documents")
}
deleteTestDb(t, dbName)
}