async def test_batch_single_insert(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = '001' entity['RowKey'] = 'batch_insert' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() batch = [('create', entity)] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] e = await self.table.get_entity(row_key=entity['RowKey'], partition_key=entity['PartitionKey']) assert e['test'] == entity['test'].value assert e['test2'] == entity['test2'] assert e['test3'] == entity['test3'] assert e['test4'] == entity['test4'].value finally: await self._tear_down()
async def test_batch_single_update(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = '001' entity['RowKey'] = 'batch_insert' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() resp = await self.table.create_entity(entity) assert resp is not None entity['test3'] = 5 entity['test5'] = datetime.utcnow() batch = [('update', entity, {'mode':UpdateMode.MERGE})] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] result = await self.table.get_entity(row_key=entity['RowKey'], partition_key=entity['PartitionKey']) assert result['PartitionKey'] == u'001' assert result['RowKey'] == u'batch_insert' assert result['test3'] == 5 finally: await self._tear_down()
async def test_batch_delete(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = u'001' entity['RowKey'] = u'batch_delete' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = u'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() await self.table.create_entity(entity) entity = await self.table.get_entity(partition_key=u'001', row_key=u'batch_delete') assert 3 == entity['test3'] batch = [('delete', entity)] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' not in transaction_result[0] with pytest.raises(ResourceNotFoundError): entity = await self.table.get_entity(partition_key=entity['PartitionKey'], row_key=entity['RowKey']) finally: await self._tear_down()
async def test_batch_insert_merge(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = '001' entity['RowKey'] = 'batch_insert_merge' entity['test'] = True entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() batch = [('upsert', entity, {'mode':UpdateMode.MERGE})] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] entity = await self.table.get_entity('001', 'batch_insert_merge') assert entity is not None assert 'value' == entity['test2'] assert 1234567890 == entity['test4'] finally: await self._tear_down()
async def test_batch_update(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = u'001' entity['RowKey'] = u'batch_update' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = u'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() await self.table.create_entity(entity) entity = await self.table.get_entity(u'001', u'batch_update') assert 3 == entity['test3'] entity['test2'] = u'value1' batch = [('update', entity)] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] result = await self.table.get_entity('001', 'batch_update') assert 'value1' == result['test2'] assert entity['PartitionKey'] == u'001' assert entity['RowKey'] == u'batch_update' finally: await self._tear_down()
def _create_random_entity_dict(self, pk=None, rk=None): """ Creates a dictionary-based entity with fixed values, using all of the supported data types. """ # partition = pk if pk is not None else self.get_resource_name('pk').decode('utf-8') # row = rk if rk is not None else self.get_resource_name('rk').decode('utf-8') partition, row = self._create_pk_rk(pk, rk) properties = { 'PartitionKey': partition, 'RowKey': row, 'age': 39, 'sex': u'male', 'married': True, 'deceased': False, 'optional': None, 'ratio': 3.1, 'evenratio': 3.0, 'large': 933311100, 'Birthday': datetime(1973, 10, 4, tzinfo=tzutc()), 'birthday': datetime(1970, 10, 4, tzinfo=tzutc()), 'binary': b'binary', 'other': EntityProperty(value=20, type=EdmType.INT32), 'clsid': uuid.UUID('c9da6455-213d-42c9-9a79-3e9149a57833') } return TableEntity(**properties)
async def test_batch_inserts(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers set_custom_default_matcher( compare_bodies=False, excluded_headers= "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id" ) # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) transaction_count = 0 batch = [] for i in range(10): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert 'etag' in transaction_result[0] entities = self.table.query_entities( "PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert assert entities is not None assert 10 == length finally: await self._tear_down()
def test_batch_single_op_if_doesnt_match( self, tables_storage_account_name, tables_primary_storage_account_key): set_bodiless_matcher() # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) batch = [] transaction_count = 0 for i in range(10): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 entity = self._create_random_entity_dict() self.table.create_entity(entity) # Act sent_entity1 = self._create_updated_entity_dict( entity['PartitionKey'], entity['RowKey']) batch = [('update', sent_entity1, { 'etag': u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"', 'match_condition': MatchConditions.IfNotModified })] with pytest.raises(TableTransactionError) as error: self.table.submit_transaction(batch) assert error.value.status_code == 412 assert error.value.error_code == TableErrorCode.update_condition_not_satisfied # Assert received_entity = self.table.get_entity(entity['PartitionKey'], entity['RowKey']) self._assert_default_entity(received_entity) finally: self._tear_down()
def test_batch_sas_auth(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): set_bodiless_matcher() # Arrange self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: token = self.generate_sas( generate_table_sas, tables_primary_cosmos_account_key, self.table_name, permission=TableSasPermissions(add=True, read=True, update=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) token = AzureSasCredential(token) # Act service = TableServiceClient( self.account_url(tables_cosmos_account_name, "cosmos"), credential=token, ) table = service.get_table_client(self.table_name) entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) batch = [] transaction_count = 0 for i in range(10): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = table.submit_transaction(batch) assert transaction_result total_entities = 0 for e in table.list_entities(): total_entities += 1 assert total_entities == transaction_count finally: self._tear_down()
async def test_batch_sas_auth(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: token = generate_table_sas( tables_storage_account_name, tables_primary_storage_account_key, self.table_name, permission=TableSasPermissions(add=True, read=True, update=True, delete=True), expiry=datetime.utcnow() + timedelta(hours=1), start=datetime.utcnow() - timedelta(minutes=1), ) token = AzureSasCredential(token) # Act service = TableServiceClient( self.account_url(tables_storage_account_name, "table"), credential=token, ) table = service.get_table_client(self.table_name) entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True, EdmType.BOOLEAN) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890, EdmType.INT32) batch = [] transaction_count = 0 for i in range(10): entity.RowKey = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = await table.submit_transaction(batch) assert transaction_result is not None total_entities = 0 async for e in table.list_entities(): total_entities += 1 assert total_entities == transaction_count finally: await self._tear_down()
async def test_batch_too_many_ops(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange await self._set_up(cosmos_account, cosmos_account_key) try: entity = self._create_random_entity_dict('001', 'batch_negative_1') await self.table.create_entity(entity) # Act with self.assertRaises(ValueError): batch = self.table.create_batch() for i in range(0, 101): entity = TableEntity() entity.PartitionKey = 'large' entity.RowKey = 'item{0}'.format(i) batch.create_entity(entity) # Assert finally: await self._tear_down()
def test_batch_too_many_ops(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key) try: entity = self._create_random_entity_dict('001', 'batch_negative_1') self.table.create_entity(entity) # Act with pytest.raises(BatchErrorException): batch = self.table.create_batch() for i in range(0, 101): entity = TableEntity() entity.PartitionKey = 'large' entity.RowKey = 'item{0}'.format(i) batch.create_entity(entity) # Assert finally: self._tear_down()
async def test_batch_merge(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): set_bodiless_matcher() # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = u'001' entity['RowKey'] = u'batch_merge' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = u'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() await self.table.create_entity(entity) resp_entity = await self.table.get_entity(partition_key=u'001', row_key=u'batch_merge') assert 3 == entity['test3'] entity = TableEntity() entity['PartitionKey'] = u'001' entity['RowKey'] = u'batch_merge' entity['test2'] = u'value1' batch = [('update', entity, {'mode': UpdateMode.MERGE})] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] resp_entity = await self.table.get_entity(partition_key=u'001', row_key=u'batch_merge') assert entity['test2'] == resp_entity['test2'] assert 1234567890 == resp_entity['test4'] assert entity['PartitionKey'] == resp_entity['PartitionKey'] assert entity['RowKey'] == resp_entity['RowKey'] finally: await self._tear_down()
async def test_batch_inserts(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): set_bodiless_matcher() # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) transaction_count = 0 batch = [] for i in range(10): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert 'etag' in transaction_result[0] entities = self.table.query_entities( "PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert assert entities is not None assert 10 == length finally: await self._tear_down()
def test_batch_too_many_ops(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: entity = self._create_random_entity_dict('001', 'batch_negative_1') self.table.create_entity(entity) # Act with pytest.raises(BatchErrorException): batch = self.table.create_batch() for i in range(0, 101): entity = TableEntity() entity.PartitionKey = 'large' entity.RowKey = 'item{0}'.format(i) batch.create_entity(entity) self.table.send_batch(batch) # Assert finally: self._tear_down()
def test_batch_too_many_ops(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: entity = self._create_random_entity_dict('001', 'batch_negative_1') self.table.create_entity(entity) # Act with pytest.raises(TableTransactionError): batch = [] for i in range(0, 101): entity = TableEntity() entity['PartitionKey'] = 'large' entity['RowKey'] = 'item{0}'.format(i) batch.append(('create', entity.copy())) self.table.submit_transaction(batch) # Assert finally: self._tear_down()
def test_batch_single_op_if_doesnt_match( self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) batch = [] transaction_count = 0 for i in range(10): entity.RowKey = str(i) batch.append(('create', entity.copy())) transaction_count += 1 entity = self._create_random_entity_dict() self.table.create_entity(entity) # Act sent_entity1 = self._create_updated_entity_dict( entity['PartitionKey'], entity['RowKey']) batch = [('update', sent_entity1, { 'etag': u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"', 'match_condition': MatchConditions.IfNotModified })] with pytest.raises(TableTransactionError): self.table.submit_transaction(batch) # Assert received_entity = self.table.get_entity(entity['PartitionKey'], entity['RowKey']) self._assert_default_entity(received_entity) finally: self._tear_down()
def test_batch_inserts(self, tables_storage_account_name, tables_primary_storage_account_key): set_bodiless_matcher() # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity['PartitionKey'] = 'batch_inserts' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) transaction_count = 0 batch = [] for i in range(100): entity['RowKey'] = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert 'etag' in transaction_result[0] entities = list( self.table.query_entities("PartitionKey eq 'batch_inserts'")) # Assert assert entities is not None assert transaction_count == len(entities) e = self.table.get_entity('batch_inserts', '1') finally: self._tear_down()
def test_batch_update(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = u'001' entity.RowKey = u'batch_update' entity.test = EntityProperty(True) entity.test2 = u'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_entity(entity) entity = self.table.get_entity(u'001', u'batch_update') assert 3 == entity.test3 entity.test2 = u'value1' batch = self.table.create_batch() batch.update_entity(entity) transaction_result = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result[0][0]['RowKey'] == u'batch_update' assert 'etag' in transaction_result[0][1] result = self.table.get_entity('001', 'batch_update') assert 'value1' == result.test2 assert entity.PartitionKey == u'001' assert entity.RowKey == u'batch_update' finally: self._tear_down()
async def test_batch_inserts(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) transaction_count = 0 batch = [] for i in range(10): entity.RowKey = str(i) batch.append(('create', entity.copy())) transaction_count += 1 transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert 'etag' in transaction_result[0] entities = self.table.query_entities( "PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert assert entities is not None assert 10 == length finally: await self._tear_down()
async def test_batch_single_insert(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key) try: # Act entity = TableEntity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.create_entity(entity) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) sent_entity = transaction_result.get_entity(entity.RowKey) assert sent_entity is not None e = await self.table.get_entity(row_key=entity.RowKey, partition_key=entity.PartitionKey) assert e.test == entity.test.value assert e.test2 == entity.test2 assert e.test3 == entity.test3 assert e.test4 == entity.test4.value assert sent_entity['test'] == entity.test.value assert sent_entity['test2'] == entity.test2 assert sent_entity['test3'] == entity.test3 assert sent_entity['test4'] == entity.test4.value finally: await self._tear_down()
def test_batch_single_insert(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.create_entity(entity) transaction_result = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) sent_entity = transaction_result.get_entity(entity.RowKey) self.assertIsNotNone(sent_entity) e = self.table.get_entity(row_key=entity.RowKey, partition_key=entity.PartitionKey) self.assertEqual(e.test, entity.test.value) self.assertEqual(e.test2, entity.test2) self.assertEqual(e.test3, entity.test3) self.assertEqual(e.test4, entity.test4.value) self.assertEqual(sent_entity['test'], entity.test.value) self.assertEqual(sent_entity['test2'], entity.test2) self.assertEqual(sent_entity['test3'], entity.test3) self.assertEqual(sent_entity['test4'], entity.test4.value) finally: self._tear_down()
def test_batch_single_op_if_doesnt_match(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) batch = self.table.create_batch() transaction_count = 0 for i in range(10): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 entity = self._create_random_entity_dict() self.table.create_entity(entity) # Act sent_entity1 = self._create_updated_entity_dict( entity['PartitionKey'], entity['RowKey']) batch = self.table.create_batch() batch.update_entity( sent_entity1, etag=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"', match_condition=MatchConditions.IfNotModified) with self.assertRaises(HttpResponseError): self.table.send_batch(batch) # Assert received_entity = self.table.get_entity(entity['PartitionKey'], entity['RowKey']) self._assert_default_entity(received_entity) finally: self._tear_down()
async def test_batch_inserts(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) transaction_count = 0 batch = self.table.create_batch() for i in range(100): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert transaction_result[0][0]['RowKey'] == '0' assert transaction_result[transaction_count - 1][0]['RowKey'] == '99' assert 'etag' in transaction_result[0][1] entities = self.table.query_entities( "PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert assert entities is not None assert 100 == length finally: await self._tear_down()
async def test_batch_single_update(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers set_custom_default_matcher( compare_bodies=False, excluded_headers= "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id" ) # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = '001' entity['RowKey'] = 'batch_insert' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() resp = await self.table.create_entity(entity) assert resp is not None entity['test3'] = 5 entity['test5'] = datetime.utcnow() batch = [('update', entity, {'mode': UpdateMode.MERGE})] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] result = await self.table.get_entity( row_key=entity['RowKey'], partition_key=entity['PartitionKey']) assert result['PartitionKey'] == u'001' assert result['RowKey'] == u'batch_insert' assert result['test3'] == 5 finally: await self._tear_down()
async def test_batch_delete(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange await self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = u'001' entity.RowKey = u'batch_delete' entity.test = EntityProperty(True) entity.test2 = u'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() await self.table.create_entity(entity) entity = await self.table.get_entity(partition_key=u'001', row_key=u'batch_delete') assert 3 == entity.test3 batch = self.table.create_batch() batch.delete_entity(partition_key=entity.PartitionKey, row_key=entity.RowKey) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result[0][0]['RowKey'] == 'batch_delete' assert 'etag' not in transaction_result[0][1] with pytest.raises(ResourceNotFoundError): entity = await self.table.get_entity( partition_key=entity.PartitionKey, row_key=entity.RowKey) finally: await self._tear_down()
def test_batch_inserts(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # Arrange self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) batch = self.table.create_batch() transaction_count = 0 for i in range(20): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 transaction_result = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert transaction_result[0][0]['RowKey'] == u'0' assert transaction_result[ transaction_count - 1][0]['RowKey'] == str(transaction_count - 1) assert 'etag' in transaction_result[0][1] entities = list( self.table.query_entities("PartitionKey eq 'batch_inserts'")) # Assert assert entities is not None assert transaction_count == len(entities) e = self.table.get_entity('batch_inserts', '1') finally: self._tear_down()
async def test_batch_delete(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers set_custom_default_matcher( compare_bodies=False, excluded_headers= "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id" ) # Arrange await self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = u'001' entity['RowKey'] = u'batch_delete' entity['test'] = EntityProperty(True, EdmType.BOOLEAN) entity['test2'] = u'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() await self.table.create_entity(entity) entity = await self.table.get_entity(partition_key=u'001', row_key=u'batch_delete') assert 3 == entity['test3'] batch = [('delete', entity)] transaction_result = await self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' not in transaction_result[0] with pytest.raises(ResourceNotFoundError): entity = await self.table.get_entity( partition_key=entity['PartitionKey'], row_key=entity['RowKey']) finally: await self._tear_down()
async def test_batch_inserts(self, resource_group, location, storage_account, storage_account_key): # Arrange await self._set_up(storage_account, storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = 'batch_inserts' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) transaction_count = 0 batch = self.table.create_batch() for i in range(100): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, transaction_count) assert transaction_result.get_entity(entity.RowKey) is not None entities = self.table.query_entities( "PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert assert entities is not None assert 100 == length finally: await self._tear_down()
def test_batch_insert(self, tables_cosmos_account_name, tables_primary_cosmos_account_key): # this can be reverted to set_bodiless_matcher() after tests are re-recorded and don't contain these headers set_custom_default_matcher( compare_bodies=False, excluded_headers= "Authorization,Content-Length,x-ms-client-request-id,x-ms-request-id" ) # Arrange self._set_up(tables_cosmos_account_name, tables_primary_cosmos_account_key, url="cosmos") try: # Act entity = TableEntity() entity['PartitionKey'] = '001' entity['RowKey'] = 'batch_insert_replace' entity['test'] = True entity['test2'] = 'value' entity['test3'] = 3 entity['test4'] = EntityProperty(1234567890, EdmType.INT32) entity['test5'] = datetime.utcnow() batch = [('upsert', entity)] transaction_result = self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] entity = self.table.get_entity('001', 'batch_insert_replace') assert entity is not None assert 'value' == entity['test2'] assert 1234567890 == entity['test4'] finally: self._tear_down()