Пример #1
0
def test_get_result_by_id_error(http_code, token):
    """
    Any unexpected http code should raise an exception.
    """
    connection_mock = Mock()
    connection_mock.get_url.return_value.status_code = http_code
    with pytest.raises(FlowclientConnectionError):
        get_result_by_query_id(connection_mock, "99")
Пример #2
0
def test_get_result_by_id_error(http_code, exception, api_response, token):
    """
    Test requesting a query by id raises appropriate exceptions.
    """
    api_response.status_code = http_code
    c = Connection("foo", token)
    with pytest.raises(exception):
        get_result_by_query_id(c, "99")
Пример #3
0
def test_get_result_by_id_poll_loop(monkeypatch):
    """
    Test requesting a query polls.
    """
    ready_mock = Mock(side_effect=[(False, None), StopIteration])
    monkeypatch.setattr("flowclient.client.query_is_ready", ready_mock)
    with pytest.raises(StopIteration):
        get_result_by_query_id("placeholder", "99")

    assert 2 == ready_mock.call_count
Пример #4
0
def test_get_result_by_id(token):
    """
    Test requesting a query by id makes the right calls.
    """
    connection_mock = Mock()
    connection_mock.get_url.return_value.json.return_value = {
        "query_id": "99",
        "query_result": [{
            "name": "foo"
        }],
    }
    type(connection_mock.get_url.return_value).status_code = PropertyMock(
        side_effect=(303, 200))
    connection_mock.get_url.return_value.headers = {
        "Location": "/api/0/foo/Test"
    }

    df = get_result_by_query_id(connection_mock, "99")

    # Query id should be requested
    assert call("poll/99") in connection_mock.get_url.call_args_list

    # Query json should be requested
    assert call("foo/Test") in connection_mock.get_url.call_args_list
    # Query result should contain the id, and the dataframe
    assert 1 == len(df)
    assert "foo" == df.name[0]
Пример #5
0
def test_get_result_by_id(api_response, api_mock, token):
    """
    Test requesting a query by id makes the right calls.
    """
    api_response.json.return_value = {
        "query_id": "99",
        "query_result": [{
            "name": "foo"
        }],
    }
    type(api_response).status_code = PropertyMock(side_effect=(303, 200))
    api_response.headers = {"Location": "/Test"}
    c = Connection("foo", token)

    df = get_result_by_query_id(c, "99")

    # Query id should be requested
    assert (call("foo/api/0/poll/99", allow_redirects=False)
            in api_mock.get.call_args_list)

    # Query json should be requested
    assert call("foo/Test",
                allow_redirects=False) in api_mock.get.call_args_list
    # Query result should contain the id, and the dataframe
    assert 1 == len(df)
    assert "foo" == df.name[0]
Пример #6
0
def test_get_result_by_id_error(monkeypatch, http_code, token):
    """
    Any unexpected http code should raise an exception.
    """
    dummy_reply = MagicMock()
    dummy_reply.headers.__getitem__.return_value = "/api/0/DUMMY_LOCATION"
    monkeypatch.setattr(
        flowclient.client,
        "query_is_ready",
        lambda connection, query_id: (True, dummy_reply),
    )
    connection_mock = Mock()
    connection_mock.get_url.return_value.status_code = http_code
    connection_mock.get_url.return_value.json.return_value = {"msg": "MESSAGE"}
    with pytest.raises(
        FlowclientConnectionError,
        match=f"Could not get result. API returned with status code: {http_code}. Reason: MESSAGE",
    ):
        get_result_by_query_id(connection_mock, "99")
    assert call("DUMMY_LOCATION") in connection_mock.get_url.call_args_list
Пример #7
0
def test_get_result_by_id_poll_loop(monkeypatch):
    """
    Test requesting a query polls.
    """
    ready_mock = Mock(
        side_effect=[
            (
                False,
                Mock(
                    json=Mock(
                        return_value=dict(
                            progress=dict(eligible=2, running=2, queued=0)
                        )
                    )
                ),
            ),
            StopIteration,
        ]
    )
    monkeypatch.setattr("flowclient.client.query_is_ready", ready_mock)
    with pytest.raises(StopIteration):
        get_result_by_query_id(connection="placeholder", query_id="99")

    assert 2 == ready_mock.call_count