def test_batch_update_if_doesnt_match(self): # Arrange entity = self._create_default_entity_dict() self.ts.insert_entity(self.table_name, entity) # Act sent_entity1 = self._create_updated_entity_dict( entity['PartitionKey'], entity['RowKey']) batch = TableBatch() batch.update_entity( sent_entity1, if_match=u'W/"datetime\'2012-06-15T22%3A51%3A44.9662825Z\'"') try: self.ts.commit_batch(self.table_name, batch) except AzureBatchOperationError as error: self.assertEqual(error.code, 'UpdateConditionNotSatisfied') self.assertTrue( 'The update condition specified in the request was not satisfied.' in str(error)) else: self.fail('AzureBatchOperationError was expected') # Assert received_entity = self.ts.get_entity(self.table_name, entity['PartitionKey'], entity['RowKey']) self._assert_default_entity(received_entity)
def test_batch_update(self): # Arrange # Act entity = Entity() entity.PartitionKey = '001' entity.RowKey = 'batch_update' entity.test = EntityProperty(EdmType.BOOLEAN, 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(EdmType.INT64, '1234567890') entity.test5 = datetime.utcnow() self.ts.insert_entity(self.table_name, entity) entity = self.ts.get_entity(self.table_name, '001', 'batch_update') self.assertEqual(3, entity.test3) entity.test2 = 'value1' batch = TableBatch() batch.update_entity(entity) resp = self.ts.commit_batch(self.table_name, batch) # Assert self.assertIsNotNone(resp) entity = self.ts.get_entity(self.table_name, '001', 'batch_update') self.assertEqual('value1', entity.test2) self.assertEqual(resp[0], entity.etag)
def test_batch_reuse(self): # Arrange table2 = self._get_table_reference('table2') self.ts.create_table(table2) # Act entity = Entity() entity.PartitionKey = '003' entity.RowKey = 'batch_all_operations_together-1' entity.test = EntityProperty(EdmType.BOOLEAN, 'true') entity.test2 = 'value' entity.test3 = 3 entity.test4 = EntityProperty(EdmType.INT64, '1234567890') entity.test5 = datetime.utcnow() batch = TableBatch() batch.insert_entity(entity) entity.RowKey = 'batch_all_operations_together-2' batch.insert_entity(entity) entity.RowKey = 'batch_all_operations_together-3' batch.insert_entity(entity) entity.RowKey = 'batch_all_operations_together-4' batch.insert_entity(entity) self.ts.commit_batch(self.table_name, batch) self.ts.commit_batch(table2, batch) batch = TableBatch() entity.RowKey = 'batch_all_operations_together' batch.insert_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.merge_entity(entity) entity.RowKey = 'batch_all_operations_together-4' entity.test3 = 10 batch.insert_or_replace_entity(entity) entity.RowKey = 'batch_all_operations_together-5' batch.insert_or_merge_entity(entity) self.ts.commit_batch(self.table_name, batch) resp = self.ts.commit_batch(table2, batch) # Assert self.assertEqual(6, len(resp)) entities = list( self.ts.query_entities(self.table_name, "PartitionKey eq '003'", '')) self.assertEqual(5, len(entities))
def test_batch_different_partition_operations_fail(self): # Arrange entity = self._create_default_entity_dict('001', 'batch_negative_1') self.ts.insert_entity(self.table_name, entity) # Act with self.assertRaises(AzureBatchValidationError): batch = TableBatch() entity = self._create_updated_entity_dict('001', 'batch_negative_1') batch.update_entity(entity) entity = self._create_default_entity_dict('002', 'batch_negative_1') batch.insert_entity(entity)
def test_batch_update_if_match(self): # Arrange entity = self._create_default_entity_dict() etag = self.ts.insert_entity(self.table_name, entity) # Act sent_entity = self._create_updated_entity_dict(entity['PartitionKey'], entity['RowKey']) batch = TableBatch() batch.update_entity(sent_entity, etag) resp = self.ts.commit_batch(self.table_name, batch) # Assert self.assertIsNotNone(resp) received_entity = self.ts.get_entity(self.table_name, entity['PartitionKey'], entity['RowKey']) self._assert_updated_entity(received_entity) self.assertEqual(resp[0], received_entity.etag)
def wait_for_tasks_to_complete( table_service, batch_client, entity_pk, entity_rk, job_id): """ Returns when all tasks in the specified job reach the Completed state. """ while True: entity = table_service.get_entity( 'SearchEntity', entity_pk, entity_rk) tasks = [task for task in batch_client.task.list(job_id) if task.id != "JobManager"] incomplete_tasks = [task for task in tasks if task.state != batchmodels.TaskState.completed] complete_tasks = [task for task in tasks if task.state == batchmodels.TaskState.completed] failed_tasks = [task for task in complete_tasks if task.execution_info.exit_code != 0 or task.execution_info.result is batchmodels.TaskExecutionResult.failure] queries = table_service.query_entities( 'SearchQueryEntity', filter="PartitionKey eq '{}'".format(entity.RowKey)) current_batch_count = 0 updateBatch = TableBatch() for task in tasks: matching_queries = [q for q in queries if q.RowKey == task.id] if not matching_queries: print('Could not find query {}'.format(task.id)) continue query = matching_queries[0] update = False state = get_query_state(task) if query._State != state: query._State = state update = True if task.state == batchmodels.TaskState.running: if not hasattr(query, 'StartTime'): query.StartTime = task.execution_info.start_time update = True if task.state == batchmodels.TaskState.completed: if not hasattr(query, 'EndTime'): query.EndTime = task.execution_info.end_time update = True if update: updateBatch.update_entity(query) current_batch_count += 1 if current_batch_count == 99: table_service.commit_batch('SearchQueryEntity', updateBatch) current_batch_count = 0 updateBatch = TableBatch() if current_batch_count > 0: table_service.commit_batch('SearchQueryEntity', updateBatch) all_tasks_complete = not incomplete_tasks any_failures = len(failed_tasks) > 0 entity.CompletedTasks = len(complete_tasks) entity._State = get_search_state(all_tasks_complete, any_failures) if not incomplete_tasks: entity.EndTime = datetime.datetime.utcnow() table_service.update_entity('SearchEntity', entity) return else: table_service.update_entity('SearchEntity', entity) time.sleep(5)