This repository has been archived by the owner on Oct 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
client_test.go
145 lines (129 loc) · 2.85 KB
/
client_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
package cloudant
import (
"fmt"
"os"
"strings"
"testing"
)
func TestInvalidLogin(t *testing.T) {
username := os.Getenv("COUCH_USER")
password := "wR0ng_pa$$w0rd"
host := os.Getenv("COUCH_HOST_URL")
if host == "" {
host = "https://" + username + ".cloudant.com"
}
if username == "" {
t.Fatalf("expected env var COUCH_USER to be set")
}
_, err := CreateClient(username, password, host, 5)
if err == nil {
t.Errorf("missing error from invalid login attempt")
}
if err.Error() != "failed to create session, status 401" {
t.Errorf("unexpected error message: %s", err)
}
}
func TestAllDBs(t *testing.T) {
dbNames := []string{}
client, err := makeClient()
if err != nil {
t.Fatalf("%s", err)
}
for i := 0; i < 50; i++ {
testdbname, err := dbName()
if err != nil {
t.Fatalf("%s", err)
}
_, err = client.GetOrCreate(testdbname)
if err != nil {
t.Fatalf("%s", err)
}
dbNames = append(dbNames, testdbname)
}
defer func() {
for _, name := range dbNames {
client.Delete(name)
}
}()
allDBs := map[string]bool{}
dbList, err := client.AllDBs(&allDBsQuery{})
for _, item := range *dbList {
allDBs[item] = true
}
for _, name := range dbNames {
if _, ok := allDBs[name]; !ok {
t.Errorf("database not found: %s", name)
}
}
}
func TestAllDBs_StartKeyEndKey(t *testing.T) {
if travis() {
fmt.Printf("[SKIP] TestAllDBs_StartKeyEndKey requires CouchDB 2.X")
return
}
dbNames := map[string]bool{}
client, err := makeClient()
if err != nil {
t.Fatalf("%s", err)
}
dbCount := 0
for i := 0; i < 50; i++ {
var dbname string
if i < 25 {
dbname = fmt.Sprintf("aaaa%d", i)
} else {
dbname = fmt.Sprintf("bbbb%d", i)
dbCount++
}
_, err = client.GetOrCreate(dbname)
if err != nil {
t.Fatalf("%s", err)
}
dbNames[dbname] = true
}
defer func() {
for name := range dbNames {
client.Delete(name)
}
}()
q := NewAllDBsQuery().StartKey("bbbb").EndKey("bbbb5").Build()
dbList, err := client.AllDBs(q)
if len(*dbList) != dbCount {
t.Errorf("expected %d databases, found %d", dbCount, len(*dbList))
}
for _, item := range *dbList {
if !strings.HasPrefix(item, "bbbb") {
t.Errorf("unexpected database name: %s", item)
}
}
}
func TestAllDBs_Limit(t *testing.T) {
if travis() {
fmt.Printf("[SKIP] TestAllDBs_Limit requires CouchDB 2.X")
return
}
limit := 20
dbNames := map[string]bool{}
client, err := makeClient()
if err != nil {
t.Fatalf("%s", err)
}
for i := 0; i < 20; i++ {
dbname := fmt.Sprintf("aaaa%d", i)
_, err = client.GetOrCreate(dbname)
if err != nil {
t.Fatalf("%s", err)
}
dbNames[dbname] = true
}
defer func() {
for name := range dbNames {
client.Delete(name)
}
}()
query := NewAllDBsQuery().Limit(limit).Build()
dbList, err := client.AllDBs(query)
if len(*dbList) != limit {
t.Errorf("expected %d databases, found %d", limit, len(*dbList))
}
}