-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocument_test.go
121 lines (95 loc) · 3.19 KB
/
document_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 gocouchlib
import (
"fmt"
"net/url"
"testing"
)
type EmployeeDoc struct {
EmployeeId int
EmployeeName string
EmployeeAge int
}
func TestDocumentExists(t *testing.T) {
s := &Server{"http://couchdb1:5984",
url.UserPassword("testuser", "password"),
}
db := &Database{"gocouch", s}
doc1 := &Document{Id: "doc1", Rev: "3-63cad646d83b402c86639c25d9dabd8a", Db: db}
exists, couchResp := doc1.Exists()
fmt.Println("Doc1 exists:", exists)
fmt.Println("Doc1 content:", doc1, couchResp.StatusCode, couchResp.Headers)
}
func TestDocumentGet(t *testing.T) {
s := &Server{"http://couchdb1:5984",
url.UserPassword("testuser", "password"),
}
db := &Database{"gocouch", s}
doc1 := &Document{Id: "doc1", Db: db}
json, _ := doc1.Get()
fmt.Println("=> TestDocumentGet():", json)
switch json.(type) {
case JsonObj:
default:
t.Error("Document.Get() did not return a Json document")
}
}
func TestDocumentSave(t *testing.T) {
s := &Server{"http://couchdb1:5984",
url.UserPassword("testuser", "password"),
}
db := &Database{"gocouch", s}
// case: both _id and _rev are not specified
new_doc1 := &Document{Db: db, Json: EmployeeDoc{10, "Hiranya", 32}}
fmt.Println("=> new_doc1: ", new_doc1)
success, couchResp := new_doc1.Save()
if !success {
t.Error("New document without _id and _rev did not save successfully. CouchResponse:", couchResp)
}
// case: only _id is specified, no _rev
new_doc2 := &Document{Db: db, Id: "new_doc2", Json: EmployeeDoc{20, "John", 12}}
success, couchResp = new_doc2.Save()
if !success {
t.Error("New document with _id specified but without _rev, did not save successfully. CouchResponse:", couchResp)
}
success, couchResp = new_doc2.Delete()
if !success {
t.Error("new_doc2 deletion not successful. CouchResponse:", couchResp)
}
// case: both _id and _rev is specified. Therefore an update to doc
new_doc3 := &Document{Db: db, Id: "new_doc3", Json: EmployeeDoc{30, "Deshani", 22}}
success, couchResp = new_doc3.Save()
if !success {
t.Error("new_doc3 did not save successfully. CouchResponse:", couchResp)
}
emp2 := new_doc3.Json.(EmployeeDoc)
emp2.EmployeeName = "Chirag"
new_doc3.Json = emp2
success, couchResp = new_doc3.Save()
if !success {
t.Error("new_doc3 second save was not successful. CouchResponse:", couchResp)
}
success, couchResp = new_doc3.Delete()
if !success {
t.Error("new_doc3 deletion was not successful. CouchResponse:", couchResp)
}
}
func TestDocumentDelete(t *testing.T) {
s := &Server{"http://couchdb1:5984",
url.UserPassword("testuser", "password"),
}
db := &Database{"gocouch", s}
new_delete_doc1 := &Document{Id: "new_delete_doc1", Db: db, Json: EmployeeDoc{100, "Hiranya", 33}}
fmt.Println("=> new_delete_doc1: ", new_delete_doc1)
success, couchResp := new_delete_doc1.Save()
if !success {
t.Error("new_delete_doc1 did not save successfully. CouchResponse:", couchResp)
}
success, couchResp = new_delete_doc1.Delete()
fmt.Println("=> new_delete_doc1: ", new_delete_doc1)
if !success {
t.Error("new_delete_doc1 deletion was not successful. CouchResponse:", couchResp)
}
if !new_delete_doc1.Deleted {
t.Error("new_delete_doc1's deleted attribute (corresponds to _deleted) has not been set to TRUE")
}
}