@@ -777,7 +777,7 @@ func TestRichQuery(t *testing.T) {
777
777
}
778
778
}
779
779
780
- func TestBatchCreateRetrieve (t * testing.T ) {
780
+ func TestBatchBatchOperations (t * testing.T ) {
781
781
782
782
if ledgerconfig .IsCouchDBEnabled () == true {
783
783
@@ -893,5 +893,115 @@ func TestBatchCreateRetrieve(t *testing.T) {
893
893
testutil .AssertEquals (t , updateDoc .Error , updateDocumentConflictError )
894
894
testutil .AssertEquals (t , updateDoc .Reason , updateDocumentConflictReason )
895
895
}
896
+
897
+ //----------------------------------------------
898
+ //Test Batch Retrieve Keys and Update
899
+
900
+ var keys []string
901
+
902
+ keys = append (keys , "marble01" )
903
+ keys = append (keys , "marble03" )
904
+
905
+ batchRevs , err := db .BatchRetrieveIDRevision (keys )
906
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting retrieve revisions" ))
907
+
908
+ batchUpdateDocs = []* CouchDoc {}
909
+
910
+ //iterate through the revision docs
911
+ for _ , revdoc := range batchRevs {
912
+ if revdoc .ID == "marble01" {
913
+ //update the json with the rev and add to the batch
914
+ marble01Doc := addRevisionAndDeleteStatus (revdoc .Rev , byteJSON01 , false )
915
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : marble01Doc , Attachments : attachments1 })
916
+ }
917
+
918
+ if revdoc .ID == "marble03" {
919
+ //update the json with the rev and add to the batch
920
+ marble03Doc := addRevisionAndDeleteStatus (revdoc .Rev , byteJSON03 , false )
921
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : marble03Doc , Attachments : attachments3 })
922
+ }
923
+ }
924
+
925
+ //Update couchdb with the batch
926
+ batchUpdateResp , err = db .BatchUpdateDocuments (batchUpdateDocs )
927
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting to update a batch of documents" ))
928
+ //check to make sure each batch update response was successful
929
+ for _ , updateDoc := range batchUpdateResp {
930
+ testutil .AssertEquals (t , updateDoc .Ok , true )
931
+ }
932
+
933
+ //----------------------------------------------
934
+ //Test Batch Delete
935
+
936
+ keys = []string {}
937
+
938
+ keys = append (keys , "marble02" )
939
+ keys = append (keys , "marble04" )
940
+
941
+ batchRevs , err = db .BatchRetrieveIDRevision (keys )
942
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting retrieve revisions" ))
943
+
944
+ batchUpdateDocs = []* CouchDoc {}
945
+
946
+ //iterate through the revision docs
947
+ for _ , revdoc := range batchRevs {
948
+ if revdoc .ID == "marble02" {
949
+ //update the json with the rev and add to the batch
950
+ marble02Doc := addRevisionAndDeleteStatus (revdoc .Rev , byteJSON02 , true )
951
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : marble02Doc , Attachments : attachments1 })
952
+ }
953
+ if revdoc .ID == "marble04" {
954
+ //update the json with the rev and add to the batch
955
+ marble04Doc := addRevisionAndDeleteStatus (revdoc .Rev , byteJSON04 , true )
956
+ batchUpdateDocs = append (batchUpdateDocs , & CouchDoc {JSONValue : marble04Doc , Attachments : attachments3 })
957
+ }
958
+ }
959
+
960
+ //Update couchdb with the batch
961
+ batchUpdateResp , err = db .BatchUpdateDocuments (batchUpdateDocs )
962
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting to update a batch of documents" ))
963
+
964
+ //check to make sure each batch update response was successful
965
+ for _ , updateDoc := range batchUpdateResp {
966
+ testutil .AssertEquals (t , updateDoc .Ok , true )
967
+ }
968
+
969
+ //Retrieve the test document
970
+ dbGetResp , _ , geterr = db .ReadDoc ("marble02" )
971
+ testutil .AssertNoError (t , geterr , fmt .Sprintf ("Error when trying to retrieve a document" ))
972
+
973
+ //assert the value was deleted
974
+ testutil .AssertNil (t , dbGetResp )
975
+
976
+ //Retrieve the test document
977
+ dbGetResp , _ , geterr = db .ReadDoc ("marble04" )
978
+ testutil .AssertNoError (t , geterr , fmt .Sprintf ("Error when trying to retrieve a document" ))
979
+
980
+ //assert the value was deleted
981
+ testutil .AssertNil (t , dbGetResp )
982
+ }
983
+ }
984
+
985
+ //addRevisionAndDeleteStatus adds keys for version and chaincodeID to the JSON value
986
+ func addRevisionAndDeleteStatus (revision string , value []byte , deleted bool ) []byte {
987
+
988
+ //create a version mapping
989
+ jsonMap := make (map [string ]interface {})
990
+
991
+ json .Unmarshal (value , & jsonMap )
992
+
993
+ //add the revision
994
+ if revision != "" {
995
+ jsonMap ["_rev" ] = revision
996
+ }
997
+
998
+ //If this record is to be deleted, set the "_deleted" property to true
999
+ if deleted {
1000
+ jsonMap ["_deleted" ] = true
896
1001
}
1002
+ //marshal the data to a byte array
1003
+ returnJSON , _ := json .Marshal (jsonMap )
1004
+
1005
+ return returnJSON
1006
+
897
1007
}
0 commit comments