Exemple #1
0
def arxiv():
    """
    Defines a GET route for the arxiv API.
    Make the request object API ready.
    Transform the response into JSON format

    Returns:
        object (JSON): A response object for further querying
    """

    qrystr_params = {"filters": {}}

    for arg, values in request.args.items():
        if arg.startswith("filter_"):
            qrystr_params["filters"][arg.replace("filter_", "")] = values

    request_object = req.ArxivDocumentListRequestObject.from_dict(
        qrystr_params)

    repo = ar.ArxivRepo()
    use_case = uc.ProcessArxivDocuments(repo)

    response = use_case.execute(request_object)

    return Response(
        json.dumps(response.value, cls=ser.ArxivDocEncoder),
        mimetype="application/json",
        status=STATUS_CODES[response.type],
    )
def test_arxiv_doc_list_without_parameters(domain_arxivdocs):
    """Tests calling the ProcessArxivDocuments method without any params

    Args:
        domain_arxivdocs ([type]): the expected results

    Raises:
        AssertionError: If nothing was returned
        AssertionError: If the response is not the expected one
    """

    repo = mock.Mock()
    repo.list.return_value = domain_arxivdocs

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict({})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    if not bool(response_object):
        raise AssertionError("response_object is empty")
    repo.list.assert_called_with(filters=None)

    if response_object.value != domain_arxivdocs:
        raise AssertionError("respons differs form expected")
Exemple #3
0
def test_arxiv_doc_list_without_parameters(domain_storagerooms):
    repo = mock.Mock()
    repo.list.return_value = domain_storagerooms

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict({})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    assert bool(response_object) is True
    repo.list.assert_called_with(filters=None)

    assert response_object.value == domain_storagerooms
Exemple #4
0
def test_arxiv_doc_list_handles_generic_error():
    repo = mock.Mock()
    repo.list.side_effect = Exception("Just an error message")

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict({})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    assert bool(response_object) is False
    assert response_object.value == {
        "type": res.ResponseFailure.SYSTEM_ERROR,
        "message": "Exception: Just an error message",
    }
Exemple #5
0
def test_arxiv_doc_list_handles_bad_request():
    repo = mock.Mock()

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict(
        {"filters": 5})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    assert bool(response_object) is False
    assert response_object.value == {
        "type": res.ResponseFailure.PARAMETERS_ERROR,
        "message": "filters: Is not iterable",
    }
def test_arxiv_doc_list_handles_bad_request():
    """Tests handling a usecase with a bad request
    """

    repo = mock.Mock()

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict({"filters": 5})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    if bool(response_object):
        raise AssertionError("response_object supposed to be empty")
    if response_object.value != {
        "type": res.ResponseFailure.PARAMETERS_ERROR,
        "message": "filters: Is not iterable",
    }:
        raise AssertionError("error response differs from expected")
def test_arxiv_doc_list_handles_generic_error():
    """Tests handling of a generic error when the request is empty
    """

    repo = mock.Mock()
    repo.list.side_effect = Exception("Just an error message")

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    request_object = req.ArxivDocumentListRequestObject.from_dict({})

    response_object = arxiv_doc_list_use_case.execute(request_object)

    if bool(response_object):
        raise AssertionError("response_object supposed to be empty")
    if response_object.value != {
        "type": res.ResponseFailure.SYSTEM_ERROR,
        "message": "Exception: Just an error message",
    }:
        raise AssertionError("error response differs from expected")
def test_arxiv_doc_list_with_filters(domain_arxivdocs):
    """Tests calling the usecase filter with parameters
    TODO - implement that part.

    Args:
        domain_arxivdocs ([type]): The expected filtered documents
    """

    repo = mock.Mock()
    repo.list.return_value = domain_arxivdocs

    arxiv_doc_list_use_case = uc.ProcessArxivDocuments(repo)
    qry_filters = {"a": 5}
    request_object = req.ArxivDocumentListRequestObject.from_dict(
        {"filters": qry_filters}
    )

    response_object = arxiv_doc_list_use_case.execute(request_object)

    if not bool(response_object):
        raise AssertionError("response_object is empty")
    repo.list.assert_called_with(filters=qry_filters)
    if response_object.value != domain_arxivdocs:
        raise AssertionError("respons differs form expected")