@@ -29,6 +29,7 @@ import (
29
29
"github.com/hyperledger/fabric/common/ledger/testutil"
30
30
"github.com/hyperledger/fabric/core/ledger/ledgerconfig"
31
31
ledgertestutil "github.com/hyperledger/fabric/core/ledger/testutil"
32
+ logging "github.com/op/go-logging"
32
33
"github.com/spf13/viper"
33
34
)
34
35
@@ -82,6 +83,11 @@ func TestMain(m *testing.M) {
82
83
viper .Set ("ledger.state.couchDBConfig.maxRetriesOnStartup" , 10 )
83
84
viper .Set ("ledger.state.couchDBConfig.requestTimeout" , time .Second * 35 )
84
85
86
+ //set the logging level to DEBUG to test debug only code
87
+ logging .SetLevel (logging .DEBUG , "couchdb" )
88
+
89
+ viper .Set ("logging.peer" , "debug" )
90
+
85
91
// Create CouchDB definition from config parameters
86
92
couchDBDef = GetCouchDBDefinition ()
87
93
@@ -205,6 +211,18 @@ func TestBadCouchDBInstance(t *testing.T) {
205
211
_ , err = badDB .BatchUpdateDocuments (nil )
206
212
testutil .AssertError (t , err , "Error should have been thrown with BatchUpdateDocuments and invalid connection" )
207
213
214
+ //Test ListIndex with bad connection
215
+ _ , err = badDB .ListIndex ()
216
+ testutil .AssertError (t , err , "Error should have been thrown with ListIndex and invalid connection" )
217
+
218
+ //Test CreateIndex with bad connection
219
+ err = badDB .CreateIndex ("" )
220
+ testutil .AssertError (t , err , "Error should have been thrown with CreateIndex and invalid connection" )
221
+
222
+ //Test DeleteIndex with bad connection
223
+ err = badDB .DeleteIndex ("" , "" )
224
+ testutil .AssertError (t , err , "Error should have been thrown with DeleteIndex and invalid connection" )
225
+
208
226
}
209
227
210
228
func TestDBCreateSaveWithoutRevision (t * testing.T ) {
@@ -316,6 +334,27 @@ func TestDBBadConnection(t *testing.T) {
316
334
}
317
335
}
318
336
337
+ func TestBadDBCredentials (t * testing.T ) {
338
+
339
+ if ledgerconfig .IsCouchDBEnabled () {
340
+
341
+ database := "testdbbadcredentials"
342
+ err := cleanup (database )
343
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to cleanup Error: %s" , err ))
344
+ defer cleanup (database )
345
+
346
+ if err == nil {
347
+ //create a new instance and database object
348
+ _ , err := CreateCouchInstance (couchDBDef .URL , "fred" , "fred" ,
349
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
350
+ testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for bad credentials" ))
351
+
352
+ }
353
+
354
+ }
355
+
356
+ }
357
+
319
358
func TestDBCreateDatabaseAndPersist (t * testing.T ) {
320
359
321
360
if ledgerconfig .IsCouchDBEnabled () {
@@ -862,6 +901,154 @@ func TestCouchDBVersion(t *testing.T) {
862
901
863
902
}
864
903
904
+ func TestIndexOperations (t * testing.T ) {
905
+
906
+ if ledgerconfig .IsCouchDBEnabled () {
907
+
908
+ database := "testindexoperations"
909
+ err := cleanup (database )
910
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to cleanup Error: %s" , err ))
911
+ defer cleanup (database )
912
+
913
+ byteJSON1 := []byte (`{"_id":"1", "asset_name":"marble1","color":"blue","size":1,"owner":"jerry"}` )
914
+ byteJSON2 := []byte (`{"_id":"2", "asset_name":"marble2","color":"red","size":2,"owner":"tom"}` )
915
+ byteJSON3 := []byte (`{"_id":"3", "asset_name":"marble3","color":"green","size":3,"owner":"jerry"}` )
916
+ byteJSON4 := []byte (`{"_id":"4", "asset_name":"marble4","color":"purple","size":4,"owner":"tom"}` )
917
+ byteJSON5 := []byte (`{"_id":"5", "asset_name":"marble5","color":"blue","size":5,"owner":"jerry"}` )
918
+ byteJSON6 := []byte (`{"_id":"6", "asset_name":"marble6","color":"white","size":6,"owner":"tom"}` )
919
+ byteJSON7 := []byte (`{"_id":"7", "asset_name":"marble7","color":"white","size":7,"owner":"tom"}` )
920
+ byteJSON8 := []byte (`{"_id":"8", "asset_name":"marble8","color":"white","size":8,"owner":"tom"}` )
921
+ byteJSON9 := []byte (`{"_id":"9", "asset_name":"marble9","color":"white","size":9,"owner":"tom"}` )
922
+ byteJSON10 := []byte (`{"_id":"10", "asset_name":"marble10","color":"white","size":10,"owner":"tom"}` )
923
+
924
+ //create a new instance and database object --------------------------------------------------------
925
+ couchInstance , err := CreateCouchInstance (couchDBDef .URL , couchDBDef .Username , couchDBDef .Password ,
926
+ couchDBDef .MaxRetries , couchDBDef .MaxRetriesOnStartup , couchDBDef .RequestTimeout )
927
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
928
+ db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
929
+
930
+ //create a new database
931
+ _ , errdb := db .CreateDatabaseIfNotExist ()
932
+ testutil .AssertNoError (t , errdb , fmt .Sprintf ("Error when trying to create database" ))
933
+
934
+ batchUpdateDocs := []* CouchDoc {}
935
+
936
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON1 , Attachments : nil })
937
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON2 , Attachments : nil })
938
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON3 , Attachments : nil })
939
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON4 , Attachments : nil })
940
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON5 , Attachments : nil })
941
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON6 , Attachments : nil })
942
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON7 , Attachments : nil })
943
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON8 , Attachments : nil })
944
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON9 , Attachments : nil })
945
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : byteJSON10 , Attachments : nil })
946
+
947
+ _ , err = db .BatchUpdateDocuments (batchUpdateDocs )
948
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error adding batch of documents" ))
949
+
950
+ //Create an index definition
951
+ indexDefSize := "{\" index\" :{\" fields\" :[{\" size\" :\" desc\" }]},\" ddoc\" :\" indexSizeSortDoc\" , \" name\" :\" indexSizeSortName\" ,\" type\" :\" json\" }"
952
+
953
+ //Create the index
954
+ err = db .CreateIndex (indexDefSize )
955
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while creating an index" ))
956
+
957
+ //Retrieve the index
958
+ listResult , err := db .ListIndex ()
959
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while retrieving indexes" ))
960
+
961
+ //There should only be one item returned
962
+ testutil .AssertEquals (t , len (* listResult ), 1 )
963
+ for _ , elem := range * listResult {
964
+ testutil .AssertEquals (t , elem .DesignDocument , "indexSizeSortDoc" )
965
+ testutil .AssertEquals (t , elem .Name , "indexSizeSortName" )
966
+ testutil .AssertEquals (t , elem .Definition , "{\" fields\" :[{\" size\" :\" desc\" }]}" )
967
+ }
968
+
969
+ //Create an index definition with no DesignDocument or name
970
+ indexDefColor := "{\" index\" :{\" fields\" :[{\" color\" :\" desc\" }]}}"
971
+
972
+ //Create the index
973
+ err = db .CreateIndex (indexDefColor )
974
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while creating an index" ))
975
+
976
+ //Retrieve the list of indexes
977
+ listResult , err = db .ListIndex ()
978
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while retrieving indexes" ))
979
+
980
+ //There should be two indexes returned
981
+ testutil .AssertEquals (t , len (* listResult ), 2 )
982
+
983
+ //Delete the named index
984
+ err = db .DeleteIndex ("indexSizeSortDoc" , "indexSizeSortName" )
985
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while deleting an index" ))
986
+
987
+ //Retrieve the list of indexes
988
+ listResult , err = db .ListIndex ()
989
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while retrieving indexes" ))
990
+
991
+ //There should be one index returned
992
+ testutil .AssertEquals (t , len (* listResult ), 1 )
993
+
994
+ //Delete the unnamed index
995
+ for _ , elem := range * listResult {
996
+ err = db .DeleteIndex (elem .DesignDocument , string (elem .Name ))
997
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while deleting an index" ))
998
+ }
999
+
1000
+ //Retrieve the list of indexes, should be zero
1001
+ listResult , err = db .ListIndex ()
1002
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while retrieving indexes" ))
1003
+ testutil .AssertEquals (t , len (* listResult ), 0 )
1004
+
1005
+ //Create a query string with a descending sort, this will require an index
1006
+ queryString := "{\" selector\" :{\" size\" : {\" $gt\" : 0}},\" fields\" : [\" _id\" , \" _rev\" , \" owner\" , \" asset_name\" , \" color\" , \" size\" ], \" sort\" :[{\" size\" :\" desc\" }], \" limit\" : 10,\" skip\" : 0}"
1007
+
1008
+ //Execute a query with a sort, this should throw the exception
1009
+ _ , err = db .QueryDocuments (queryString )
1010
+ testutil .AssertError (t , err , fmt .Sprintf ("Error thrown while querying without a valid index" ))
1011
+
1012
+ //Create the index
1013
+ err = db .CreateIndex (indexDefSize )
1014
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while creating an index" ))
1015
+
1016
+ //Execute a query with an index, this should succeed
1017
+ _ , err = db .QueryDocuments (queryString )
1018
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while querying with an index" ))
1019
+
1020
+ //Create another index definition
1021
+ indexDefSize = "{\" index\" :{\" fields\" :[{\" data.size\" :\" desc\" },{\" data.owner\" :\" desc\" }]},\" ddoc\" :\" indexSizeOwnerSortDoc\" , \" name\" :\" indexSizeOwnerSortName\" ,\" type\" :\" json\" }"
1022
+
1023
+ //Create the index
1024
+ err = db .CreateIndex (indexDefSize )
1025
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while creating an index" ))
1026
+
1027
+ //Retrieve the indexes
1028
+ listResult , err = db .ListIndex ()
1029
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error thrown while retrieving indexes" ))
1030
+
1031
+ //There should only be two definitions
1032
+ testutil .AssertEquals (t , len (* listResult ), 2 )
1033
+
1034
+ //Create an invalid index definition with an invalid JSON
1035
+ indexDefSize = "{\" index\" {\" fields\" :[{\" data.size\" :\" desc\" },{\" data.owner\" :\" desc\" }]},\" ddoc\" :\" indexSizeOwnerSortDoc\" , \" name\" :\" indexSizeOwnerSortName\" ,\" type\" :\" json\" }"
1036
+
1037
+ //Create the index
1038
+ err = db .CreateIndex (indexDefSize )
1039
+ testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for an invalid index JSON" ))
1040
+
1041
+ //Create an invalid index definition with a valid JSON and an invalid index definition
1042
+ indexDefSize = "{\" index\" :{\" fields2\" :[{\" data.size\" :\" desc\" },{\" data.owner\" :\" desc\" }]},\" ddoc\" :\" indexSizeOwnerSortDoc\" , \" name\" :\" indexSizeOwnerSortName\" ,\" type\" :\" json\" }"
1043
+
1044
+ //Create the index
1045
+ err = db .CreateIndex (indexDefSize )
1046
+ testutil .AssertError (t , err , fmt .Sprintf ("Error should have been thrown for an invalid index definition" ))
1047
+
1048
+ }
1049
+
1050
+ }
1051
+
865
1052
func TestRichQuery (t * testing.T ) {
866
1053
867
1054
if ledgerconfig .IsCouchDBEnabled () {
0 commit comments