def test_calculate_max_concurrent_usage_task( self, mock_calculate_max_concurrent_usage): """ Test happy path behavior. The calculation function should be called and the task is set to complete. """ calculate_max_concurrent_usage_task.retry = MagicMock( side_effect=Retry) task_id = uuid4() user_id = self.user.id request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=user_id, date=request_date, task_id=task_id) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) calculate_max_concurrent_usage_task.run(str(request_date), user_id) mock_calculate_max_concurrent_usage.assert_called_with( request_date, user_id) self.assertEqual( ConcurrentUsageCalculationTask.objects.get(task_id=task_id).status, ConcurrentUsageCalculationTask.COMPLETE, )
def test_calculate_max_concurrent_usage_exception( self, mock_calculate_max_concurrent_usage): """ Test that task is marked as ERROR if calculate_max_concurrent_usage fails. The calculate function is called here, but we force it to raise an exception. Also verify that we logged the underlying exception. """ error_message = "it is a mystery" mock_calculate_max_concurrent_usage.side_effect = Exception( error_message) task_id = uuid4() user_id = self.user.id request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=user_id, date=request_date, task_id=task_id) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) with self.assertRaises(Exception), self.assertLogs( "api.tasks", level="WARNING") as logging_watcher: calculate_max_concurrent_usage_task.run(str(request_date), user_id) self.assertIn(error_message, logging_watcher.output[0]) self.assertEqual( ConcurrentUsageCalculationTask.objects.get(task_id=task_id).status, ConcurrentUsageCalculationTask.ERROR, ) mock_calculate_max_concurrent_usage.assert_called_with( request_date, user_id)
def test_invalid_user(self, mock_calculate_max_concurrent_usage): """Test that task exits early if user does not exist.""" task_id = uuid4() request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=self.user.id, date=request_date, task_id=task_id) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) calculate_max_concurrent_usage_task.run(request_date, _faker.pyint()) mock_calculate_max_concurrent_usage.assert_not_called()
def test_task_retry_if_another_task_is_running(self): """Test that task retries if another task is running with same user and date.""" calculate_max_concurrent_usage_task.retry = MagicMock( side_effect=Retry) request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=self.user.id, date=request_date, task_id=uuid4()) concurrent_task.status = ConcurrentUsageCalculationTask.RUNNING concurrent_task.save() with self.assertRaises(Retry): calculate_max_concurrent_usage_task(str(request_date), self.user.id)
def test_early_return_on_unexpected_status( self, mock_calculate_max_concurrent_usage): """Test early return if task status is not SCHEDULED.""" task_id = uuid4() user_id = self.user.id request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask( user_id=user_id, date=request_date, task_id=task_id, status=ConcurrentUsageCalculationTask.COMPLETE, ) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) mock_calculate_max_concurrent_usage.assert_not_called()
def test_cancel(self): """Test that cancel sets status to CANCELED.""" user = util_helper.generate_test_user() task_id = uuid4() concurrent_task = ConcurrentUsageCalculationTask(user_id=user.id, date=_faker.date(), task_id=task_id) concurrent_task.save() self.assertEqual(concurrent_task.status, ConcurrentUsageCalculationTask.SCHEDULED) with patch("celery.current_app") as mock_celery: concurrent_task.cancel() mock_celery.control.revoke.assert_called_with(task_id) self.assertEqual( ConcurrentUsageCalculationTask.objects.get(task_id=task_id).status, ConcurrentUsageCalculationTask.CANCELED, )
def test_calculate_max_concurrent_usage_task(self): """Test ConcurrentUsageCalculationTask is set to complete.""" calculate_max_concurrent_usage_task.retry = MagicMock( side_effect=Retry) task_id = uuid4() request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=self.user.id, date=request_date, task_id=task_id) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) calculate_max_concurrent_usage_task.run(str(request_date), self.user.id) self.assertEqual( ConcurrentUsageCalculationTask.objects.get(task_id=task_id).status, ConcurrentUsageCalculationTask.COMPLETE, )
def test_calculate_max_concurrent_usage_exception( self, mock_calculate_max_concurrent_usage): """Test that task is marked as ERROR if calculate_max_concurrent_usage fails.""" mock_calculate_max_concurrent_usage.side_effect = Exception() task_id = uuid4() request_date = datetime.date(2019, 5, 1) concurrent_task = ConcurrentUsageCalculationTask(user_id=self.user.id, date=request_date, task_id=task_id) concurrent_task.save() calculate_max_concurrent_usage_task.push_request(id=task_id) with self.assertRaises(Exception): calculate_max_concurrent_usage_task.run(str(request_date), self.user.id) self.assertEqual( ConcurrentUsageCalculationTask.objects.get(task_id=task_id).status, ConcurrentUsageCalculationTask.ERROR, )