def test_request_is_valid_and_successful(self, mock_process, mock_url_for,
                                             mock_async):
        """The request includes an id for a task that is finished (success)."""
        mock_result = mock.MagicMock()
        mock_result.status = "SUCCESS"

        document_id = '1234.5678v2'
        mock_result.result = {
            "document_id": document_id,
            "references": [],
            "extraction": "bazbat"
        }
        mock_async.return_value = mock_result

        def url_for(endpoint, doc_id=None):
            """Mock :func:`flask.url_for`."""
            return '/%s/%s' % (endpoint, doc_id)

        mock_url_for.side_effect = url_for

        task_id = 'asdf1234-5678'
        response, status, headers = extraction.status(task_id)
        self.assertEqual(status, 303,
                         "Response status should be 303 See Other")
        self.assertIn('Location', headers, "Location header should be set")
        self.assertTrue(headers['Location'].endswith(document_id),
                        "Location header should point to ref endpoint.")
        try:
            json.dumps(response)
        except TypeError:
            self.fail("Response content should be JSON-serializable")
    def test_request_valid_task_does_not_exist(self, mock_process, mock_async):
        """The request includes an id for a task that doesn't exist."""
        type(mock_async).status = "PENDING"

        task_id = 'asdf1234-5678'
        response, status, headers = extraction.status(task_id)
        self.assertEqual(status, 404,
                         "Response status should be 404 Not Found")
        try:
            json.dumps(response)
        except TypeError:
            self.fail("Response content should be JSON-serializable")
    def test_request_is_valid_and_retrying(self, mock_process, mock_aync):
        """The request includes an id for a task that is being retried."""
        mock_result = mock.MagicMock()
        mock_result.status = "RETRY"
        mock_aync.return_value = mock_result

        task_id = 'asdf1234-5678'
        response, status, headers = extraction.status(task_id)
        self.assertEqual(status, 200, "Response status should be 200 OK")
        try:
            json.dumps(response)
        except TypeError:
            self.fail("Response content should be JSON-serializable")
 def test_request_is_valid_and_failed(self, mock_process, mock_async):
     """The request includes an id for an existing task that is failed."""
     mock_result = mock.MagicMock()
     mock_result.status = "FAILURE"
     mock_async.return_value = mock_result
     mock_result.result = RuntimeError('Something went wrong')
     task_id = 'asdf1234-5678'
     response, status, headers = extraction.status(task_id)
     self.assertEqual(status, 200, "Response status should be 200 OK")
     try:
         json.dumps(response)
     except TypeError:
         self.fail("Response content should be JSON-serializable")
예제 #5
0
def task_status(task_id: str) -> tuple:
    """Get the status of a reference extraction task."""
    data, code, headers = extraction.status(task_id)
    return jsonify(data), code, headers