def test_batch_insert_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_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.send_batch(batch) # Assert assert resp is not None entity, headers = self.table.get_entity('001', 'batch_insert_merge', response_hook=lambda e, h: (e, h)) assert entity is not None assert 'value' == entity.test2 assert 1234567890 == entity.test4 assert list(resp)[0].headers['Etag'] == headers['etag'] 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.get_entity(entity.RowKey) is not None 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()
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') 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()
def test_batch_update(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_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.value 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.get_entity(entity.RowKey) is not None result = self.table.get_entity('001', 'batch_update') assert 'value1' == result.test2.value assert entity.PartitionKey == u'001' assert entity.RowKey == u'batch_update' finally: self._tear_down()
def test_batch_reuse(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, cosmos_account_key) try: table2 = self._get_table_reference('table2') table2.create_table() # 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() batch = TableBatchClient() batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' batch.create_entity(entity) self.table.send_batch(batch) table2.send_batch(batch) batch = TableBatchClient() entity.RowKey = 'batch_all_operations_together' batch.create_entity(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_entity(entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(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.send_batch(batch) resp = table2.send_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()
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) entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() resp = await self.table.create_entity(entity) assert resp is not None 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) assert transaction_result[0][0]['PartitionKey'] == '001' assert 'etag' in transaction_result[0][1] 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_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_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()
def test_batch_all_operations_together(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 = '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() self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' self.table.create_entity(entity) batch = self.table.create_batch() entity.RowKey = 'batch_all_operations_together' batch.create_entity(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_entity(entity) entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(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') resp = self.table.send_batch(batch) # Assert assert 6 == len(list(resp)) entities = list(self.table.query_items("PartitionKey eq '003'")) assert 5 == len(entities) finally: self._tear_down()
def test_batch_all_operations_together_context_manager( 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 = '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() self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' self.table.create_entity(entity) 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 = list(self.table.query_entities("PartitionKey eq '003'")) assert 4 == len(entities) finally: self._tear_down()
def test_batch_all_operations_together_context_manager( self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange self._set_up(cosmos_account, 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() self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' self.table.create_entity(entity) 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_item(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='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') # Assert entities = list(self.table.query_items("PartitionKey eq '003'")) self.assertEqual(5, len(entities)) finally: self._tear_down()
async def test_batch_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 = u'001' entity.RowKey = u'batch_merge' 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) 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 = 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) assert transaction_result[0][0]['RowKey'] == 'batch_merge' assert 'etag' in transaction_result[0][1] 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 test_batch_merge(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_account_key) try: # Act entity = TableEntity() entity.PartitionKey = u'001' entity.RowKey = u'batch_merge' entity.test = EntityProperty(True) entity.test2 = u'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_entity(entity) resp_entity = self.table.get_entity(partition_key=u'001', row_key=u'batch_merge') self.assertEqual(3, entity.test3) entity = TableEntity() entity.PartitionKey = u'001' entity.RowKey = u'batch_merge' entity.test2 = u'value1' batch = self.table.create_batch() batch.update_entity(entity, mode=UpdateMode.MERGE) transaction_result = self.table.send_batch(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) self.assertIsNotNone(transaction_result.get_entity(entity.RowKey)) resp_entity = self.table.get_entity(partition_key=u'001', row_key=u'batch_merge') self.assertEqual(entity.test2, resp_entity.test2.value) self.assertEqual(1234567890, resp_entity.test4.value) self.assertEqual(entity.PartitionKey, resp_entity.PartitionKey) self.assertEqual(entity.RowKey, resp_entity.RowKey) finally: self._tear_down()
async def test_batch_reuse(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: table2 = self._get_table_reference('table2') table2.create_table() # 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() batch = self.table.create_batch() batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' batch.create_entity(entity) await self.table.send_batch(batch) with pytest.raises(HttpResponseError): resp = await table2.send_batch(batch) # Assert entities = self.table.query_entities("PartitionKey eq '003'") length = 0 async for e in entities: length += 1 assert 5 == length finally: await self._tear_down()
async def test_batch_merge(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_merge' 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) 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 = 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) assert transaction_result.get_entity(entity.RowKey) is not None 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 test_batch_merge(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_merge' entity.test = EntityProperty(True) entity.test2 = u'value' entity.test3 = 3 entity.test4 = EntityProperty(1234567890) entity.test5 = datetime.utcnow() self.table.create_entity(entity) resp_entity = 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 = self.table.submit_transaction(batch) # Assert self._assert_valid_batch_transaction(transaction_result, 1) assert 'etag' in transaction_result[0] resp_entity = 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: 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_merge(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 = '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') assert 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 assert resp is not None entity, headers = self.table.get_entity('001', 'batch_merge', response_hook=lambda e, h: (e, h)) assert 'value1' == entity.test2 assert 1234567890 == entity.test4 assert list(resp)[0].headers['Etag'] == headers['etag'] finally: 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()
async def test_batch_reuse(self, resource_group, location, cosmos_account, cosmos_account_key): # Arrange await self._set_up(cosmos_account, cosmos_account_key) try: table2 = self._get_table_reference('table2') table2.create_table() # 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() batch = self.table.create_batch() batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' batch.create_entity(entity) await self.table.send_batch(batch) with self.assertRaises(HttpResponseError): resp = await table2.send_batch(batch) # Assert entities = self.table.query_entities("PartitionKey eq '003'") length = 0 async for e in entities: length += 1 self.assertEqual(5, length) 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()
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()
def test_batch_reuse(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: table2 = self._get_table_reference('table2') table2.create_table() # 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() batch = [] batch.append(('upsert', entity.copy())) entity.RowKey = 'batch_all_operations_together-2' batch.append(('upsert', entity.copy())) entity.RowKey = 'batch_all_operations_together-3' batch.append(('upsert', entity.copy())) entity.RowKey = 'batch_all_operations_together-4' batch.append(('upsert', entity.copy())) resp1 = self.table.submit_transaction(batch) resp2 = table2.submit_transaction(batch) entities = list(self.table.query_entities("PartitionKey eq '003'")) assert 4 == len(entities) table2 = list(table2.query_entities("PartitionKey eq '003'")) assert 4 == len(entities) finally: self._tear_down()
def test_inferred_types(self): # Arrange # Act entity = TableEntity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty(True) entity.test2 = EntityProperty(b'abcdef') entity.test3 = EntityProperty(u'c9da6455-213d-42c9-9a79-3e9149a57833') entity.test4 = EntityProperty(datetime(1973, 10, 4, tzinfo=tzutc())) entity.test5 = EntityProperty(u"stringystring") entity.test6 = EntityProperty(3.14159) entity.test7 = EntityProperty(100) entity.test8 = EntityProperty(2**33, EdmType.INT64) # Assert self.assertEqual(entity.test.type, EdmType.BOOLEAN) self.assertEqual(entity.test2.type, EdmType.BINARY) self.assertEqual(entity.test3.type, EdmType.GUID) self.assertEqual(entity.test4.type, EdmType.DATETIME) self.assertEqual(entity.test5.type, EdmType.STRING) self.assertEqual(entity.test6.type, EdmType.DOUBLE) self.assertEqual(entity.test7.type, EdmType.INT32) self.assertEqual(entity.test8.type, EdmType.INT64)
def test_batch_reuse(self, tables_storage_account_name, tables_primary_storage_account_key): # Arrange self._set_up(tables_storage_account_name, tables_primary_storage_account_key) try: table2 = self._get_table_reference('table2') table2.create_table() # 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() batch = self.table.create_batch() batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' batch.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' batch.create_entity(entity) self.table.send_batch(batch) with pytest.raises(BatchErrorException): resp = table2.send_batch(batch) entities = list(self.table.query_entities("PartitionKey eq '003'")) assert 4 == len(entities) finally: 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()
async def test_batch_all_operations_together( 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) transaction_count = 0 batch = self.table.create_batch() entity.RowKey = 'batch_all_operations_together' batch.create_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-1' batch.delete_entity(entity.PartitionKey, entity.RowKey) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.update_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(entity, mode=UpdateMode.MERGE) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.upsert_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-5' batch.upsert_entity(entity, mode=UpdateMode.MERGE) 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( 'batch_all_operations_together') is not None assert transaction_result.get_entity( 'batch_all_operations_together-1') is not None assert transaction_result.get_entity( 'batch_all_operations_together-2') is not None assert transaction_result.get_entity( 'batch_all_operations_together-3') is not None assert transaction_result.get_entity( 'batch_all_operations_together-4') is not None assert transaction_result.get_entity( 'batch_all_operations_together-5') is not None entities = self.table.query_entities("PartitionKey eq '003'") length = 0 async for e in entities: length += 1 assert 5 == length finally: await self._tear_down()
def test_batch_all_operations_together(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 = '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() self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' self.table.create_entity(entity) transaction_count = 0 batch = self.table.create_batch() entity.RowKey = 'batch_all_operations_together' batch.create_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-1' batch.delete_entity(entity.PartitionKey, entity.RowKey) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.update_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(entity, mode=UpdateMode.MERGE) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.upsert_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-5' batch.upsert_entity(entity, mode=UpdateMode.MERGE) 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'batch_all_operations_together' assert 'etag' in transaction_result[0][1] assert transaction_result[1][0][ 'RowKey'] == u'batch_all_operations_together-1' assert 'etag' not in transaction_result[1][1] assert transaction_result[2][0][ 'RowKey'] == u'batch_all_operations_together-2' assert 'etag' in transaction_result[2][1] assert transaction_result[3][0][ 'RowKey'] == u'batch_all_operations_together-3' assert 'etag' in transaction_result[3][1] assert transaction_result[4][0][ 'RowKey'] == u'batch_all_operations_together-4' assert 'etag' in transaction_result[4][1] assert transaction_result[5][0][ 'RowKey'] == u'batch_all_operations_together-5' assert 'etag' in transaction_result[5][1] # Assert entities = list(self.table.query_entities("PartitionKey eq '003'")) assert 5 == len(entities) finally: self._tear_down()
async def test_batch_all_operations_together( 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) transaction_count = 0 batch = [] entity.RowKey = 'batch_all_operations_together' batch.append((TransactionOperation.CREATE, entity.copy())) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-1' batch.append((TransactionOperation.DELETE, entity.copy())) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.append((TransactionOperation.UPDATE, entity.copy())) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.append((TransactionOperation.UPDATE, entity.copy(), { 'mode': UpdateMode.REPLACE })) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.append((TransactionOperation.UPSERT, entity.copy())) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-5' batch.append((TransactionOperation.UPSERT, entity.copy(), { 'mode': UpdateMode.REPLACE })) 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] assert 'etag' not in transaction_result[1] assert 'etag' in transaction_result[2] assert 'etag' in transaction_result[3] assert 'etag' in transaction_result[4] assert 'etag' in transaction_result[5] entities = self.table.query_entities("PartitionKey eq '003'") length = 0 async for e in entities: length += 1 assert 5 == length finally: await self._tear_down()
def test_batch_all_operations_together(self, resource_group, location, storage_account, storage_account_key): # Arrange self._set_up(storage_account, storage_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() self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-2' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-3' self.table.create_entity(entity) entity.RowKey = 'batch_all_operations_together-4' self.table.create_entity(entity) transaction_count = 0 batch = self.table.create_batch() entity.RowKey = 'batch_all_operations_together' batch.create_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-1' batch.delete_entity(entity.PartitionKey, entity.RowKey) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-2' entity.test3 = 10 batch.update_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-3' entity.test3 = 100 batch.update_entity(entity, mode=UpdateMode.MERGE) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.upsert_entity(entity) transaction_count += 1 entity.RowKey = 'batch_all_operations_together-5' batch.upsert_entity(entity, mode=UpdateMode.MERGE) 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('batch_all_operations_together')) self.assertIsNotNone( transaction_result.get_entity( 'batch_all_operations_together-1')) self.assertIsNotNone( transaction_result.get_entity( 'batch_all_operations_together-2')) self.assertIsNotNone( transaction_result.get_entity( 'batch_all_operations_together-3')) self.assertIsNotNone( transaction_result.get_entity( 'batch_all_operations_together-4')) self.assertIsNotNone( transaction_result.get_entity( 'batch_all_operations_together-5')) # Assert entities = list(self.table.query_entities("PartitionKey eq '003'")) self.assertEqual(5, len(entities)) finally: self._tear_down()