Exemplo n.º 1
0
    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,
        )
Exemplo n.º 2
0
    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)
Exemplo n.º 3
0
    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()
Exemplo n.º 4
0
    def test_return_if_user_does_not_exist(
            self, mock_calculate_max_concurrent_usage):
        """
        Test that the task returns early if user does not exist.

        The calculate function should not be called.
        """
        task_id = uuid4()
        request_date = datetime.date(2019, 5, 1)
        calculate_max_concurrent_usage_task.push_request(id=task_id)
        calculate_max_concurrent_usage_task.run(str(request_date),
                                                _faker.pyint())
        mock_calculate_max_concurrent_usage.assert_not_called()
Exemplo n.º 5
0
    def test_new_task_if_existing_task_is_missing(self, mock_schedule_task):
        """Test that task spawns a new task with same user and date if missing."""
        task_id = uuid4()
        request_date = datetime.date(2019, 5, 1)
        calculate_max_concurrent_usage_task.push_request(id=task_id)

        with self.assertLogs("api.tasks", level="ERROR") as logging_watcher:
            calculate_max_concurrent_usage_task.run(str(request_date),
                                                    self.user.id)
            mock_schedule_task.assert_called_with(request_date, self.user.id)
        self.assertIn(
            f'ConcurrentUsageCalculationTask not found for task ID "{task_id}"',
            logging_watcher.output[0],
        )
Exemplo n.º 6
0
    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,
        )
Exemplo n.º 7
0
    def test_new_task_if_existing_task_is_missing(
            self, mock_schedule_task, mock_calculate_max_concurrent_usage):
        """
        Test that the task schedules a new task with same user and date if missing.

        The calculate function should not be called.
        """
        task_id = uuid4()
        user_id = self.user.id
        request_date = datetime.date(2019, 5, 1)
        calculate_max_concurrent_usage_task.push_request(id=task_id)

        with self.assertLogs("api.tasks", level="WARNING") as logging_watcher:
            calculate_max_concurrent_usage_task.run(str(request_date), user_id)

        mock_schedule_task.assert_called_with(request_date, user_id)
        mock_calculate_max_concurrent_usage.assert_not_called()
        self.assertIn(
            f"ConcurrentUsageCalculationTask not found for task ID {task_id}",
            logging_watcher.output[0],
        )
Exemplo n.º 8
0
    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,
        )