@@ -35,6 +35,9 @@ var badConnectURL = "couchdb:5990"
35
35
var username = ""
36
36
var password = ""
37
37
38
+ const updateDocumentConflictError = "conflict"
39
+ const updateDocumentConflictReason = "Document update conflict."
40
+
38
41
func cleanup (database string ) error {
39
42
//create a new connection
40
43
couchInstance , err := CreateCouchInstance (connectURL , username , password )
@@ -125,7 +128,7 @@ func TestDBCreateEnsureFullCommit(t *testing.T) {
125
128
//create a new instance and database object
126
129
couchInstance , err := CreateCouchInstance (connectURL , username , password )
127
130
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
128
- db := CouchDatabase {couchInstance : * couchInstance , dbName : database }
131
+ db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
129
132
130
133
//create a new database
131
134
_ , errdb := db .CreateDatabaseIfNotExist ()
@@ -142,6 +145,7 @@ func TestDBCreateEnsureFullCommit(t *testing.T) {
142
145
}
143
146
}
144
147
}
148
+
145
149
func TestDBBadDatabaseName (t * testing.T ) {
146
150
147
151
if ledgerconfig .IsCouchDBEnabled () == true {
@@ -318,7 +322,7 @@ func TestDBBadJSON(t *testing.T) {
318
322
}
319
323
320
324
func TestPrefixScan (t * testing.T ) {
321
- if ! ledgerconfig .IsCouchDBEnabled () {
325
+ if ! ledgerconfig .IsCouchDBEnabled () == true {
322
326
return
323
327
}
324
328
database := "testprefixscan"
@@ -612,7 +616,7 @@ func TestRichQuery(t *testing.T) {
612
616
//create a new instance and database object --------------------------------------------------------
613
617
couchInstance , err := CreateCouchInstance (connectURL , username , password )
614
618
testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
615
- db := CouchDatabase {couchInstance : * couchInstance , dbName : database }
619
+ db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
616
620
617
621
//create a new database
618
622
_ , errdb := db .CreateDatabaseIfNotExist ()
@@ -772,3 +776,122 @@ func TestRichQuery(t *testing.T) {
772
776
}
773
777
}
774
778
}
779
+
780
+ func TestBatchCreateRetrieve (t * testing.T ) {
781
+
782
+ if ledgerconfig .IsCouchDBEnabled () == true {
783
+
784
+ byteJSON01 := []byte (`{"_id":"marble01","asset_name":"marble01","color":"blue","size":"1","owner":"jerry"}` )
785
+ byteJSON02 := []byte (`{"_id":"marble02","asset_name":"marble02","color":"red","size":"2","owner":"tom"}` )
786
+ byteJSON03 := []byte (`{"_id":"marble03","asset_name":"marble03","color":"green","size":"3","owner":"jerry"}` )
787
+ byteJSON04 := []byte (`{"_id":"marble04","asset_name":"marble04","color":"purple","size":"4","owner":"tom"}` )
788
+ byteJSON05 := []byte (`{"_id":"marble05","asset_name":"marble05","color":"blue","size":"5","owner":"jerry"}` )
789
+
790
+ attachment1 := & Attachment {}
791
+ attachment1 .AttachmentBytes = []byte (`marble01 - test attachment` )
792
+ attachment1 .ContentType = "application/octet-stream"
793
+ attachment1 .Name = "data"
794
+ attachments1 := []Attachment {}
795
+ attachments1 = append (attachments1 , * attachment1 )
796
+
797
+ attachment2 := & Attachment {}
798
+ attachment2 .AttachmentBytes = []byte (`marble02 - test attachment` )
799
+ attachment2 .ContentType = "application/octet-stream"
800
+ attachment2 .Name = "data"
801
+ attachments2 := []Attachment {}
802
+ attachments2 = append (attachments2 , * attachment2 )
803
+
804
+ attachment3 := & Attachment {}
805
+ attachment3 .AttachmentBytes = []byte (`marble03 - test attachment` )
806
+ attachment3 .ContentType = "application/octet-stream"
807
+ attachment3 .Name = "data"
808
+ attachments3 := []Attachment {}
809
+ attachments3 = append (attachments3 , * attachment3 )
810
+
811
+ attachment4 := & Attachment {}
812
+ attachment4 .AttachmentBytes = []byte (`marble04 - test attachment` )
813
+ attachment4 .ContentType = "application/octet-stream"
814
+ attachment4 .Name = "data"
815
+ attachments4 := []Attachment {}
816
+ attachments4 = append (attachments4 , * attachment4 )
817
+
818
+ attachment5 := & Attachment {}
819
+ attachment5 .AttachmentBytes = []byte (`marble05 - test attachment` )
820
+ attachment5 .ContentType = "application/octet-stream"
821
+ attachment5 .Name = "data"
822
+ attachments5 := []Attachment {}
823
+ attachments5 = append (attachments5 , * attachment5 )
824
+
825
+ database := "testbatch"
826
+ err := cleanup (database )
827
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to cleanup Error: %s" , err ))
828
+ defer cleanup (database )
829
+
830
+ //create a new instance and database object --------------------------------------------------------
831
+ couchInstance , err := CreateCouchInstance (connectURL , username , password )
832
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when trying to create couch instance" ))
833
+ db := CouchDatabase {CouchInstance : * couchInstance , DBName : database }
834
+
835
+ //create a new database
836
+ _ , errdb := db .CreateDatabaseIfNotExist ()
837
+ testutil .AssertNoError (t , errdb , fmt .Sprintf ("Error when trying to create database" ))
838
+
839
+ batchUpdateDocs := []* CouchDoc {}
840
+
841
+ value1 := & CouchDoc {JSONValue : byteJSON01 , Attachments : attachments1 }
842
+ value2 := & CouchDoc {JSONValue : byteJSON02 , Attachments : attachments2 }
843
+ value3 := & CouchDoc {JSONValue : byteJSON03 , Attachments : attachments3 }
844
+ value4 := & CouchDoc {JSONValue : byteJSON04 , Attachments : attachments4 }
845
+ value5 := & CouchDoc {JSONValue : byteJSON05 , Attachments : attachments5 }
846
+
847
+ batchUpdateDocs = append (batchUpdateDocs , value1 )
848
+ batchUpdateDocs = append (batchUpdateDocs , value2 )
849
+ batchUpdateDocs = append (batchUpdateDocs , value3 )
850
+ batchUpdateDocs = append (batchUpdateDocs , value4 )
851
+ batchUpdateDocs = append (batchUpdateDocs , value5 )
852
+
853
+ batchUpdateResp , err := db .BatchUpdateDocuments (batchUpdateDocs )
854
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting to update a batch of documents" ))
855
+
856
+ //check to make sure each batch update response was successful
857
+ for _ , updateDoc := range batchUpdateResp {
858
+ testutil .AssertEquals (t , updateDoc .Ok , true )
859
+ }
860
+
861
+ //----------------------------------------------
862
+ //Test Retrieve JSON
863
+ dbGetResp , _ , geterr := db .ReadDoc ("marble01" )
864
+ testutil .AssertNoError (t , geterr , fmt .Sprintf ("Error when attempting read a document" ))
865
+
866
+ assetResp := & Asset {}
867
+ geterr = json .Unmarshal (dbGetResp .JSONValue , & assetResp )
868
+ testutil .AssertNoError (t , geterr , fmt .Sprintf ("Error when trying to retrieve a document" ))
869
+ //Verify the owner retrieved matches
870
+ testutil .AssertEquals (t , assetResp .Owner , "jerry" )
871
+
872
+ //----------------------------------------------
873
+ //Test retrieve binary
874
+ dbGetResp , _ , geterr = db .ReadDoc ("marble03" )
875
+ testutil .AssertNoError (t , geterr , fmt .Sprintf ("Error when attempting read a document" ))
876
+ //Retrieve the attachments
877
+ attachments := dbGetResp .Attachments
878
+ //Only one was saved, so take the first
879
+ retrievedAttachment := attachments [0 ]
880
+ //Verify the text matches
881
+ testutil .AssertEquals (t , attachment3 .AttachmentBytes , retrievedAttachment .AttachmentBytes )
882
+ //----------------------------------------------
883
+ //Test Bad Updates
884
+ batchUpdateDocs = []* CouchDoc {}
885
+ batchUpdateDocs = append (batchUpdateDocs , value1 )
886
+ batchUpdateDocs = append (batchUpdateDocs , value2 )
887
+ batchUpdateResp , err = db .BatchUpdateDocuments (batchUpdateDocs )
888
+ testutil .AssertNoError (t , err , fmt .Sprintf ("Error when attempting to update a batch of documents" ))
889
+ //No revision was provided, so these two updates should fail
890
+ //Verify that the "Ok" field is returned as false
891
+ for _ , updateDoc := range batchUpdateResp {
892
+ testutil .AssertEquals (t , updateDoc .Ok , false )
893
+ testutil .AssertEquals (t , updateDoc .Error , updateDocumentConflictError )
894
+ testutil .AssertEquals (t , updateDoc .Reason , updateDocumentConflictReason )
895
+ }
896
+ }
897
+ }
0 commit comments