@@ -520,3 +520,72 @@ func TestContainerQueryItems(t *testing.T) {
520520 }
521521 }
522522}
523+
524+ func TestContainerExecuteBatch (t * testing.T ) {
525+ batchResponseRaw := []map [string ]interface {}{
526+ {"statusCode" : 200 , "requestCharge" : 10.0 , "eTag" : "someETag" , "resourceBody" : "someBody" },
527+ {"statusCode" : 201 , "requestCharge" : 11.0 , "eTag" : "someETag2" },
528+ }
529+
530+ jsonString , err := json .Marshal (batchResponseRaw )
531+ if err != nil {
532+ t .Fatal (err )
533+ }
534+
535+ srv , close := mock .NewTLSServer ()
536+ defer close ()
537+ srv .SetResponse (
538+ mock .WithBody (jsonString ),
539+ mock .WithStatusCode (http .StatusOK ),
540+ mock .WithHeader (cosmosHeaderEtag , "someEtag" ),
541+ mock .WithHeader (cosmosHeaderActivityId , "someActivityId" ),
542+ mock .WithHeader (cosmosHeaderRequestCharge , "13.42" ))
543+
544+ verifier := pipelineVerifier {}
545+
546+ pl := azruntime .NewPipeline ("azcosmostest" , "v1.0.0" , azruntime.PipelineOptions {PerCall : []policy.Policy {& verifier }}, & policy.ClientOptions {Transport : srv })
547+ client := & Client {endpoint : srv .URL (), pipeline : pl }
548+
549+ database , _ := newDatabase ("databaseId" , client )
550+ container , _ := newContainer ("containerId" , database )
551+
552+ pk := NewPartitionKeyString ("pk" )
553+ batch := container .NewTransactionalBatch (pk )
554+ _ , err = container .ExecuteTransactionalBatch (context .TODO (), batch , nil )
555+ if err == nil {
556+ t .Fatal ("Expected error, but got nil" )
557+ }
558+
559+ batch .ReadItem ("someId" , nil )
560+
561+ body := map [string ]string {
562+ "foo" : "bar" ,
563+ }
564+
565+ itemMarshall , _ := json .Marshal (body )
566+ batch .CreateItem (itemMarshall , nil )
567+
568+ _ , err = container .ExecuteTransactionalBatch (context .TODO (), batch , nil )
569+ if err != nil {
570+ t .Fatal (err )
571+ }
572+
573+ if len (verifier .requests ) != 1 {
574+ t .Fatalf ("Expected 1 request, got %d" , len (verifier .requests ))
575+ }
576+
577+ request := verifier .requests [0 ]
578+
579+ if request .method != http .MethodPost {
580+ t .Errorf ("Expected method to be %s, but got %s" , http .MethodPost , request .method )
581+ }
582+
583+ if request .url .RequestURI () != "/dbs/databaseId/colls/containerId/docs" {
584+ t .Errorf ("Expected url to be %s, but got %s" , "/dbs/databaseId/colls/containerId/docs" , request .url .RequestURI ())
585+ }
586+
587+ marshalledOperations , _ := json .Marshal (batch .operations )
588+ if request .body != string (marshalledOperations ) {
589+ t .Errorf ("Expected %v, but got %v" , string (marshalledOperations ), request .body )
590+ }
591+ }
0 commit comments