...

Source file src/github.com/Azure/azure-sdk-for-go/storage/table_batch_test.go

Documentation: github.com/Azure/azure-sdk-for-go/storage

     1  // +build go1.7
     2  
     3  package storage
     4  
     5  // Copyright (c) Microsoft Corporation. All rights reserved.
     6  // Licensed under the MIT License. See License.txt in the project root for license information.
     7  
     8  import (
     9  	"time"
    10  
    11  	"github.com/gofrs/uuid"
    12  	chk "gopkg.in/check.v1"
    13  )
    14  
    15  type TableBatchSuite struct{}
    16  
    17  var _ = chk.Suite(&TableBatchSuite{})
    18  
    19  func (s *TableBatchSuite) Test_BatchInsertMultipleEntities(c *chk.C) {
    20  	cli := getBasicClient(c).GetTableService()
    21  	rec := cli.client.appendRecorder(c)
    22  	defer rec.Stop()
    23  
    24  	table := cli.GetTableReference(tableName(c, "me"))
    25  	err := table.Create(30, EmptyPayload, nil)
    26  	c.Assert(err, chk.IsNil)
    27  	defer table.Delete(30, nil)
    28  
    29  	entity := table.GetEntityReference("mypartitionkey", "myrowkey")
    30  	props := map[string]interface{}{
    31  		"AmountDue":      200.23,
    32  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
    33  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
    34  		"IsActive":       true,
    35  		"NumberOfOrders": int64(255),
    36  	}
    37  	entity.Properties = props
    38  
    39  	entity2 := table.GetEntityReference("mypartitionkey", "myrowkey2")
    40  	props2 := map[string]interface{}{
    41  		"AmountDue":      111.23,
    42  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
    43  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
    44  		"IsActive":       true,
    45  		"NumberOfOrders": int64(255),
    46  	}
    47  	entity2.Properties = props2
    48  
    49  	batch := table.NewBatch()
    50  	batch.InsertOrReplaceEntity(entity, false)
    51  	batch.InsertOrReplaceEntity(entity2, false)
    52  
    53  	err = batch.ExecuteBatch()
    54  	c.Assert(err, chk.IsNil)
    55  
    56  	options := QueryOptions{
    57  		Top: 2,
    58  	}
    59  
    60  	results, err := table.QueryEntities(30, FullMetadata, &options)
    61  	c.Assert(err, chk.IsNil)
    62  	c.Assert(results.Entities, chk.HasLen, 2)
    63  }
    64  
    65  func (s *TableBatchSuite) Test_BatchInsertSameEntryMultipleTimes(c *chk.C) {
    66  	cli := getBasicClient(c).GetTableService()
    67  	rec := cli.client.appendRecorder(c)
    68  	defer rec.Stop()
    69  
    70  	table := cli.GetTableReference(tableName(c))
    71  	err := table.Create(30, EmptyPayload, nil)
    72  	c.Assert(err, chk.IsNil)
    73  	defer table.Delete(30, nil)
    74  
    75  	entity := table.GetEntityReference("mypartitionkey", "myrowkey")
    76  	props := map[string]interface{}{
    77  		"AmountDue":      200.23,
    78  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
    79  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
    80  		"IsActive":       true,
    81  		"NumberOfOrders": int64(255),
    82  	}
    83  	entity.Properties = props
    84  
    85  	batch := table.NewBatch()
    86  	batch.InsertOrReplaceEntity(entity, false)
    87  	batch.InsertOrReplaceEntity(entity, false)
    88  
    89  	err = batch.ExecuteBatch()
    90  	c.Assert(err, chk.NotNil)
    91  	v, ok := err.(AzureStorageServiceError)
    92  	if ok {
    93  		c.Assert(v.Code, chk.Equals, "InvalidDuplicateRow")
    94  	}
    95  }
    96  
    97  func (s *TableBatchSuite) Test_BatchInsertDeleteSameEntity(c *chk.C) {
    98  	cli := getBasicClient(c).GetTableService()
    99  	rec := cli.client.appendRecorder(c)
   100  	defer rec.Stop()
   101  
   102  	table := cli.GetTableReference(tableName(c))
   103  	err := table.Create(30, EmptyPayload, nil)
   104  	c.Assert(err, chk.IsNil)
   105  	defer table.Delete(30, nil)
   106  
   107  	entity := table.GetEntityReference("mypartitionkey", "myrowkey")
   108  	props := map[string]interface{}{
   109  		"AmountDue":      200.23,
   110  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
   111  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
   112  		"IsActive":       true,
   113  		"NumberOfOrders": int64(255),
   114  	}
   115  	entity.Properties = props
   116  
   117  	batch := table.NewBatch()
   118  	batch.InsertOrReplaceEntity(entity, false)
   119  	batch.DeleteEntity(entity, true)
   120  
   121  	err = batch.ExecuteBatch()
   122  	c.Assert(err, chk.NotNil)
   123  
   124  	v, ok := err.(AzureStorageServiceError)
   125  	if ok {
   126  		c.Assert(v.Code, chk.Equals, "InvalidDuplicateRow")
   127  	}
   128  }
   129  
   130  func (s *TableBatchSuite) Test_BatchInsertThenDeleteDifferentBatches(c *chk.C) {
   131  	cli := getBasicClient(c).GetTableService()
   132  	rec := cli.client.appendRecorder(c)
   133  	defer rec.Stop()
   134  
   135  	table := cli.GetTableReference(tableName(c))
   136  	err := table.Create(30, EmptyPayload, nil)
   137  	c.Assert(err, chk.IsNil)
   138  	defer table.Delete(30, nil)
   139  
   140  	entity := table.GetEntityReference("mypartitionkey", "myrowkey")
   141  	props := map[string]interface{}{
   142  		"AmountDue":      200.23,
   143  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
   144  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
   145  		"IsActive":       true,
   146  		"NumberOfOrders": int64(255),
   147  	}
   148  	entity.Properties = props
   149  
   150  	batch := table.NewBatch()
   151  	batch.InsertOrReplaceEntity(entity, false)
   152  	err = batch.ExecuteBatch()
   153  	c.Assert(err, chk.IsNil)
   154  
   155  	options := QueryOptions{
   156  		Top: 2,
   157  	}
   158  
   159  	results, err := table.QueryEntities(30, FullMetadata, &options)
   160  	c.Assert(err, chk.IsNil)
   161  	c.Assert(results.Entities, chk.HasLen, 1)
   162  
   163  	batch = table.NewBatch()
   164  	batch.DeleteEntity(entity, true)
   165  	err = batch.ExecuteBatch()
   166  	c.Assert(err, chk.IsNil)
   167  
   168  	// Timeout set to 15 for this test to work propwrly with the recordings
   169  	results, err = table.QueryEntities(15, FullMetadata, &options)
   170  	c.Assert(err, chk.IsNil)
   171  	c.Assert(results.Entities, chk.HasLen, 0)
   172  }
   173  
   174  func (s *TableBatchSuite) Test_BatchInsertThenMergeDifferentBatches(c *chk.C) {
   175  	cli := getBasicClient(c).GetTableService()
   176  	rec := cli.client.appendRecorder(c)
   177  	defer rec.Stop()
   178  
   179  	table := cli.GetTableReference(tableName(c))
   180  	err := table.Create(30, EmptyPayload, nil)
   181  	c.Assert(err, chk.IsNil)
   182  	defer table.Delete(30, nil)
   183  
   184  	entity := table.GetEntityReference("mypartitionkey", "myrowkey")
   185  	props := map[string]interface{}{
   186  		"AmountDue":      200.23,
   187  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
   188  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
   189  		"IsActive":       true,
   190  		"NumberOfOrders": int64(255),
   191  	}
   192  	entity.Properties = props
   193  
   194  	batch := table.NewBatch()
   195  	batch.InsertOrReplaceEntity(entity, false)
   196  	err = batch.ExecuteBatch()
   197  	c.Assert(err, chk.IsNil)
   198  
   199  	entity2 := table.GetEntityReference("mypartitionkey", "myrowkey")
   200  	props2 := map[string]interface{}{
   201  		"AmountDue":      200.23,
   202  		"CustomerCode":   uuid.FromStringOrNil("c9da6455-213d-42c9-9a79-3e9149a57833"),
   203  		"CustomerSince":  time.Date(1992, time.December, 20, 21, 55, 0, 0, time.UTC),
   204  		"DifferentField": 123,
   205  		"NumberOfOrders": int64(255),
   206  	}
   207  	entity2.Properties = props2
   208  
   209  	batch = table.NewBatch()
   210  	batch.InsertOrReplaceEntity(entity2, false)
   211  	err = batch.ExecuteBatch()
   212  	c.Assert(err, chk.IsNil)
   213  
   214  	options := QueryOptions{
   215  		Top: 2,
   216  	}
   217  
   218  	results, err := table.QueryEntities(30, FullMetadata, &options)
   219  	c.Assert(err, chk.IsNil)
   220  	c.Assert(results.Entities, chk.HasLen, 1)
   221  }
   222  

View as plain text