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, 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_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, 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()
def test_batch_delete(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, cosmos_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() self.table.create_entity(entity) entity = self.table.get_entity(partition_key=u'001', row_key=u'batch_delete') self.assertEqual(3, entity.test3) batch = self.table.create_batch() batch.delete_entity(partition_key=entity.PartitionKey, row_key=entity.RowKey) transaction_result = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) with self.assertRaises(ResourceNotFoundError): entity = self.table.get_entity(partition_key=entity.PartitionKey, row_key=entity.RowKey) finally: self._tear_down()
def test_batch_inserts(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, 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) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) entities = list(self.table.query_entities("PartitionKey eq 'batch_inserts'")) # Assert self.assertIsNotNone(entities) self.assertEqual(transaction_count, len(entities)) e = self.table.get_entity('batch_inserts', '1') finally: self._tear_down()
async def test_batch_update(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 = 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() await self.table.create_entity(entity) entity = await self.table.get_entity(u'001', u'batch_update') self.assertEqual(3, entity.test3.value) entity.test2 = u'value1' batch = self.table.create_batch() batch.update_entity(entity) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) result = await self.table.get_entity('001', 'batch_update') self.assertEqual('value1', result.test2.value) self.assertEqual(entity.PartitionKey, u'001') self.assertEqual(entity.RowKey, u'batch_update') finally: await self._tear_down()
def test_batch_merge(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, cosmos_account_key) try: # Act entity = TableEntity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_entity(entity) entity = self.table.get_entity('001', 'batch_merge') self.assertEqual(3, entity.test3) entity = TableEntity() entity.PartitionKey = '001' entity.RowKey = 'batch_merge' entity.test2 = 'value1' batch = self.table.create_batch() batch.update_entity(entity, mode='MERGE') resp = self.table.send_batch(batch) # Assert self.assertIsNotNone(resp) entity, headers = self.table.get_entity('001', 'batch_merge', response_hook=lambda e, h: (e, h)) self.assertEqual('value1', entity.test2) self.assertEqual(1234567890, entity.test4) self.assertEqual(list(resp)[0].headers['Etag'], headers['etag']) finally: self._tear_down()
async def test_batch_single_insert(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 = '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) self.assertIsNotNone(sent_entity) e = await self.table.get_entity(row_key=entity.RowKey, partition_key=entity.PartitionKey) self.assertEqual(e.test, entity.test.value) self.assertEqual(e.test2.value, entity.test2) self.assertEqual(e.test3.value, entity.test3) self.assertEqual(e.test4.value, 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: await self._tear_down()
def test_batch_update(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'] = 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() self.table.create_entity(entity) entity = self.table.get_entity(u'001', u'batch_update') assert 3 == entity['test3'] entity['test2'] = u'value1' batch = [('update', entity)] transaction_result = self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] 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()
def test_batch_insert(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, cosmos_account_key) try: # Act entity = Entity() 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_item(entity) resp = self.table.commit_batch(batch) # Assert assert resp is not None result, headers = self.table.read_item('001', 'batch_insert', response_hook=lambda e, h: (e, h)) assert list(resp)[0].headers['Etag'] == headers['etag'] finally: self._tear_down()
async def test_batch_update(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange await self._set_up(cosmos_account, cosmos_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() await self.table.create_entity(entity) entity = await self.table.get_entity(u'001', u'batch_update') assert 3 == entity.test3.value entity.test2 = u'value1' batch = self.table.create_batch() batch.update_entity(entity) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result.get_entity(entity.RowKey) is not None result = await self.table.get_entity('001', 'batch_update') assert 'value1' == result.test2.value assert entity.PartitionKey == u'001' assert entity.RowKey == u'batch_update' 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) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) entities = self.table.query_entities("PartitionKey eq 'batch_inserts'") length = 0 async for e in entities: length += 1 # Assert self.assertIsNotNone(entities) self.assertEqual(100, length) 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 = [('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_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) 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(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()
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) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) 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, resource_group, location, storage_account, storage_account_key): # Arrange await 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() resp = await self.table.create_entity(entity) self.assertIsNotNone(resp) entity.test3 = 5 entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.update_entity(entity, mode=UpdateMode.MERGE) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) result = await self.table.get_entity( row_key=entity.RowKey, partition_key=entity.PartitionKey) self.assertEqual(result.PartitionKey, u'001') self.assertEqual(result.RowKey, u'batch_insert') self.assertEqual(result.test3.value, 5) finally: await 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, 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()
async def test_batch_delete(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'] = 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()
def test_batch_delete(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 = 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() self.table.create_entity(entity) entity = 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 = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result.get_entity(entity.RowKey) is not None with pytest.raises(ResourceNotFoundError): entity = self.table.get_entity( partition_key=entity.PartitionKey, row_key=entity.RowKey) finally: self._tear_down()
async def test_batch_sas_auth(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: 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 = 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()
def test_batch_reuse(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: table2 = self._get_table_reference('table2') table2.create_table() # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = TableBatchClient() batch.create_item(entity) entity.RowKey = 'batch_all_operations_together-2' batch.create_item(entity) entity.RowKey = 'batch_all_operations_together-3' batch.create_item(entity) entity.RowKey = 'batch_all_operations_together-4' batch.create_item(entity) self.table.commit_batch(batch) table2.commit_batch(batch) batch = TableBatchClient() entity.RowKey = 'batch_all_operations_together' batch.create_item(entity) entity.RowKey = 'batch_all_operations_together-1' batch.delete_item(entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.update_item(entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_item(entity, mode='MERGE') entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.upsert_item(entity) entity.RowKey = 'batch_all_operations_together-5' batch.upsert_item(entity, mode='MERGE') self.table.commit_batch(batch) resp = table2.commit_batch(batch) # Assert self.assertEqual(6, len(list(resp))) entities = list(self.table.query_items("PartitionKey eq '003'")) self.assertEqual(5, len(entities)) finally: self._tear_down()
def test_batch_sas_auth(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: token = self.generate_sas( generate_table_sas, 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 = 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) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) batch = table.create_batch() transaction_count = 0 for i in range(10): entity.RowKey = str(i) batch.create_entity(entity) transaction_count += 1 transaction_result = await table.send_batch(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_all_operations_together_context_manager( 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 = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty(True) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() await self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' await self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' await self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' await self.table.create_entity(entity) async with self.table.create_batch() as batch: entity.RowKey = 'batch_all_operations_together' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-1' batch.delete_entity(entity.PartitionKey, entity.RowKey) entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.update_entity(entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(entity, mode=UpdateMode.MERGE) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.upsert_entity(entity) entity.RowKey = 'batch_all_operations_together-5' batch.upsert_entity(entity, mode=UpdateMode.MERGE) # Assert entities = self.table.query_entities("PartitionKey eq '003'") length = 0 async for e in entities: length += 1 assert 4 == length finally: await self._tear_down()
async def test_batch_merge(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_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()
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_insert_merge(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_merge' entity.test = True entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.upsert_entity(entity, mode=UpdateMode.MERGE) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result.get_entity(entity.RowKey) is not None entity = await self.table.get_entity('001', 'batch_insert_merge') assert entity is not None assert 'value' == entity.test2.value assert 1234567890 == entity.test4.value finally: await self._tear_down()
async def test_batch_insert_merge(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_merge' entity.test = True entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) 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()
def test_batch_insert_merge(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_insert_merge' entity.test = True entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.upsert_item(entity, mode='MERGE') resp = self.table.commit_batch(batch) # Assert self.assertIsNotNone(resp) entity, headers = self.table.read_item('001', 'batch_insert_merge', response_hook=lambda e, h: (e, h)) self.assertIsNotNone(entity) self.assertEqual('value', entity.test2) self.assertEqual(1234567890, entity.test4) self.assertEqual(list(resp)[0].headers['Etag'], headers['etag']) finally: self._tear_down()
def test_batch_insert_merge(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'] = '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 = 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_merge') assert entity is not None assert 'value' == entity['test2'] assert 1234567890 == entity['test4'] finally: self._tear_down()
async def test_batch_insert_replace(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_replace' entity.test = True entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() batch = self.table.create_batch() batch.upsert_entity(entity) transaction_result = await self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert transaction_result[0][0]['RowKey'] == 'batch_insert_replace' assert 'etag' in transaction_result[0][1] entity = await self.table.get_entity('001', 'batch_insert_replace') assert entity is not None assert 'value' == entity.test2 assert 1234567890 == entity.test4 finally: await self._tear_down()