@@ -19,6 +19,7 @@ package couchdb
19
19
import (
20
20
"encoding/json"
21
21
"fmt"
22
+ "net/http"
22
23
"os"
23
24
"strings"
24
25
"testing"
@@ -32,6 +33,7 @@ import (
32
33
)
33
34
34
35
const badConnectURL = "couchdb:5990"
36
+ const badParseConnectURL = "http://host.com|5432"
35
37
const updateDocumentConflictError = "conflict"
36
38
const updateDocumentConflictReason = "Document update conflict."
37
39
@@ -103,12 +105,89 @@ func TestDBConnectionDef(t *testing.T) {
103
105
func TestDBBadConnectionDef (t * testing.T ) {
104
106
105
107
//create a new connection
106
- _ , err := CreateConnectionDefinition ("^^^localhost:5984" , couchDBDef .Username , couchDBDef .Password ,
108
+ _ , err := CreateConnectionDefinition (badParseConnectURL , couchDBDef .Username , couchDBDef .Password ,
107
109
couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
108
110
testutil .AssertError (t , err , fmt .Sprintf ("Did not receive error when trying to create database connection definition with a bad hostname" ))
109
111
110
112
}
111
113
114
+ func TestBadCouchDBInstance (t * testing.T ) {
115
+
116
+ //TODO continue changes to return and removal of sprintf in followon changes
117
+ if ! ledgerconfig .IsCouchDBEnabled () {
118
+ t .Skip ("CouchDB is not enabled" )
119
+ return
120
+ }
121
+ //Create a bad connection definition
122
+ badConnectDef := CouchConnectionDef {URL : badParseConnectURL , Username : "" , Password : "" ,
123
+ MaxRetries : 3 , MaxRetriesOnStartup : 10 , RequestTimeout : time .Second * 30 }
124
+
125
+ client := & http.Client {}
126
+
127
+ //Create a bad couchdb instance
128
+ badCouchDBInstance := CouchInstance {badConnectDef , client }
129
+
130
+ //Create a bad CouchDatabase
131
+ badDB := CouchDatabase {badCouchDBInstance , "baddb" }
132
+
133
+ //Test CreateCouchDatabase with bad connection
134
+ _ , err := CreateCouchDatabase (badCouchDBInstance , "baddbtest" )
135
+ testutil .AssertError (t , err , "Error should have been thrown with CreateCouchDatabase and invalid connection" )
136
+
137
+ //Test CreateSystemDatabasesIfNotExist with bad connection
138
+ err = CreateSystemDatabasesIfNotExist (badCouchDBInstance )
139
+ testutil .AssertError (t , err , "Error should have been thrown with CreateSystemDatabasesIfNotExist and invalid connection" )
140
+
141
+ //Test CreateDatabaseIfNotExist with bad connection
142
+ _ , err = badDB .CreateDatabaseIfNotExist ()
143
+ testutil .AssertError (t , err , "Error should have been thrown with CreateDatabaseIfNotExist and invalid connection" )
144
+
145
+ //Test GetDatabaseInfo with bad connection
146
+ _ , _ , err = badDB .GetDatabaseInfo ()
147
+ testutil .AssertError (t , err , "Error should have been thrown with GetDatabaseInfo and invalid connection" )
148
+
149
+ //Test VerifyCouchConfig with bad connection
150
+ _ , _ , err = badCouchDBInstance .VerifyCouchConfig ()
151
+ testutil .AssertError (t , err , "Error should have been thrown with VerifyCouchConfig and invalid connection" )
152
+
153
+ //Test EnsureFullCommit with bad connection
154
+ _ , err = badDB .EnsureFullCommit ()
155
+ testutil .AssertError (t , err , "Error should have been thrown with EnsureFullCommit and invalid connection" )
156
+
157
+ //Test DropDatabase with bad connection
158
+ _ , err = badDB .DropDatabase ()
159
+ testutil .AssertError (t , err , "Error should have been thrown with DropDatabase and invalid connection" )
160
+
161
+ //Test ReadDoc with bad connection
162
+ _ , _ , err = badDB .ReadDoc ("1" )
163
+ testutil .AssertError (t , err , "Error should have been thrown with ReadDoc and invalid connection" )
164
+
165
+ //Test SaveDoc with bad connection
166
+ _ , err = badDB .SaveDoc ("1" , "1" , nil )
167
+ testutil .AssertError (t , err , "Error should have been thrown with SaveDoc and invalid connection" )
168
+
169
+ //Test DeleteDoc with bad connection
170
+ err = badDB .DeleteDoc ("1" , "1" )
171
+ testutil .AssertError (t , err , "Error should have been thrown with DeleteDoc and invalid connection" )
172
+
173
+ //Test ReadDocRange with bad connection
174
+ _ , err = badDB .ReadDocRange ("1" , "2" , 1000 , 0 )
175
+ testutil .AssertError (t , err , "Error should have been thrown with ReadDocRange and invalid connection" )
176
+
177
+ //Test QueryDocuments with bad connection
178
+ _ , err = badDB .QueryDocuments ("1" )
179
+ testutil .AssertError (t , err , "Error should have been thrown with QueryDocuments and invalid connection" )
180
+
181
+ //Test BatchRetrieveIDRevision with bad connection
182
+ _ , err = badDB .BatchRetrieveIDRevision (nil )
183
+ testutil .AssertError (t , err , "Error should have been thrown with BatchRetrieveIDRevision and invalid connection" )
184
+
185
+ //Test BatchUpdateDocuments with bad connection
186
+ _ , err = badDB .BatchUpdateDocuments (nil )
187
+ testutil .AssertError (t , err , "Error should have been thrown with BatchUpdateDocuments and invalid connection" )
188
+
189
+ }
190
+
112
191
func TestDBCreateSaveWithoutRevision (t * testing.T ) {
113
192
114
193
if ledgerconfig .IsCouchDBEnabled () {
@@ -304,6 +383,15 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
304
383
//Retrieve the info for the new database and make sure the name matches
305
384
_ , _ , errdbinfo := db .GetDatabaseInfo ()
306
385
testutil .AssertError (t , errdbinfo , fmt .Sprintf ("Error should have been thrown for missing database" ))
386
+
387
+ //Attempt to save the document with an invalid id
388
+ _ , saveerr = db .SaveDoc (string ([]byte {0xff , 0xfe , 0xfd }), "" , & CouchDoc {JSONValue : assetJSON , Attachments : nil })
389
+ testutil .AssertError (t , saveerr , fmt .Sprintf ("Error should have been thrown when saving a document with an invalid ID" ))
390
+
391
+ //Attempt to read a document with an invalid id
392
+ _ , _ , readerr := db .ReadDoc (string ([]byte {0xff , 0xfe , 0xfd }))
393
+ testutil .AssertError (t , readerr , fmt .Sprintf ("Error should have been thrown when reading a document with an invalid ID" ))
394
+
307
395
}
308
396
}
309
397
@@ -433,6 +521,7 @@ func TestPrefixScan(t *testing.T) {
433
521
//Retrieve the info for the new database and make sure the name matches
434
522
_ , _ , errdbinfo := db .GetDatabaseInfo ()
435
523
testutil .AssertError (t , errdbinfo , fmt .Sprintf ("Error should have been thrown for missing database" ))
524
+
436
525
}
437
526
}
438
527
@@ -831,6 +920,12 @@ func TestRichQuery(t *testing.T) {
831
920
//There should be 2 results for owner="tom" with a limit of 2
832
921
testutil .AssertEquals (t , len (* queryResult ), 2 )
833
922
923
+ //Test query with invalid index -------------------------------------------------------------------
924
+ queryString = "{\" selector\" :{\" owner\" :\" tom\" }, \" use_index\" :[\" _design/indexOwnerDoc\" ,\" indexOwner\" ]}"
925
+
926
+ _ , err = db .QueryDocuments (queryString )
927
+ testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for an invalid index" ))
928
+
834
929
}
835
930
}
836
931
}
0 commit comments