Skip to content

Commit 8c7a426

Browse files
author
Chris Elder
committed
[FAB-3414] Improve unit tests for couchdb
Improve unit tests for /fabric/core/ledger/util/couchdb Change-Id: I1b3c8d28b921abcebf2240bedd180392461c2290 Signed-off-by: Chris Elder <[email protected]>
1 parent c5c60c3 commit 8c7a426

File tree

1 file changed

+96
-1
lines changed

1 file changed

+96
-1
lines changed

core/ledger/util/couchdb/couchdb_test.go

+96-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package couchdb
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"net/http"
2223
"os"
2324
"strings"
2425
"testing"
@@ -32,6 +33,7 @@ import (
3233
)
3334

3435
const badConnectURL = "couchdb:5990"
36+
const badParseConnectURL = "http://host.com|5432"
3537
const updateDocumentConflictError = "conflict"
3638
const updateDocumentConflictReason = "Document update conflict."
3739

@@ -103,12 +105,89 @@ func TestDBConnectionDef(t *testing.T) {
103105
func TestDBBadConnectionDef(t *testing.T) {
104106

105107
//create a new connection
106-
_, err := CreateConnectionDefinition("^^^localhost:5984", couchDBDef.Username, couchDBDef.Password,
108+
_, err := CreateConnectionDefinition(badParseConnectURL, couchDBDef.Username, couchDBDef.Password,
107109
couchDBDef.MaxRetries, couchDBDef.MaxRetriesOnStartup, couchDBDef.RequestTimeout)
108110
testutil.AssertError(t, err, fmt.Sprintf("Did not receive error when trying to create database connection definition with a bad hostname"))
109111

110112
}
111113

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+
112191
func TestDBCreateSaveWithoutRevision(t *testing.T) {
113192

114193
if ledgerconfig.IsCouchDBEnabled() {
@@ -304,6 +383,15 @@ func TestDBCreateDatabaseAndPersist(t *testing.T) {
304383
//Retrieve the info for the new database and make sure the name matches
305384
_, _, errdbinfo := db.GetDatabaseInfo()
306385
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+
307395
}
308396
}
309397

@@ -433,6 +521,7 @@ func TestPrefixScan(t *testing.T) {
433521
//Retrieve the info for the new database and make sure the name matches
434522
_, _, errdbinfo := db.GetDatabaseInfo()
435523
testutil.AssertError(t, errdbinfo, fmt.Sprintf("Error should have been thrown for missing database"))
524+
436525
}
437526
}
438527

@@ -831,6 +920,12 @@ func TestRichQuery(t *testing.T) {
831920
//There should be 2 results for owner="tom" with a limit of 2
832921
testutil.AssertEquals(t, len(*queryResult), 2)
833922

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+
834929
}
835930
}
836931
}

0 commit comments

Comments
 (0)