예제 #1
0
def test_parameter_case(mock_session):
    """
    Test that parameter names are sent in camelCase rather than snake_case.
    """
    api_response = {'key': 'value'}
    session_context = mock_session.return_value.__enter__.return_value
    session_context.request.return_value.json.return_value = api_response

    # To avoid needing CIVIS_API_KEY set in the environment.
    op = {
        "parameters": [{
            'name': 'firstParameter',
            'in': 'query'
        }, {
            'name': 'secondParameter',
            'in': 'query'
        }]
    }
    invoke("WIBBLE",
           "/wobble/wubble",
           op,
           first_parameter='a',
           second_parameter='b')

    mock_session.call_args[1]['user_agent'] = 'civis-cli'
    session_context.request.assert_called_with(
        url='https://api.civisanalytics.com/wobble/wubble',
        json={},
        params={
            'firstParameter': 'a',
            'secondParameter': 'b'
        },
        method='WIBBLE')
예제 #2
0
def test_parameter_case(mock_request, mock_yaml,
                        mock_make_api_request_headers):
    """
    Test that parameter names are sent in camelCase rather than snake_case.

    We mock yaml because otherwise yaml will complain about being able to
    serialize the mock response object.
    """

    # To avoid needing CIVIS_API_KEY set in the environment.
    mock_make_api_request_headers.return_value = {}
    op = {
        "parameters": [{
            'name': 'firstParameter',
            'in': 'query'
        }, {
            'name': 'secondParameter',
            'in': 'query'
        }]
    }
    invoke("WIBBLE",
           "/wobble/wubble",
           op,
           first_parameter='a',
           second_parameter='b')

    mock_request.assert_called_with(
        url='https://api.civisanalytics.com/wobble/wubble',
        headers={},
        json={},
        params={
            'firstParameter': 'a',
            'secondParameter': 'b'
        },
        method='WIBBLE')
예제 #3
0
def test_blank_output(mock_session):
    """
    Test that endpoints that return blank results don't cause exceptions.
    """
    # The response object's json method will raise a ValueError when the output
    # is blank.
    session_context = mock_session.return_value.__enter__.return_value
    session_context.request.return_value.json.side_effect = ValueError()

    op = {"parameters": []}
    invoke("WIBBLE", "/wobble/wubble", op)
예제 #4
0
def test_blank_output(mock_session):
    """
    Test that endpoints that return blank results don't cause exceptions.
    """
    # The response object's json method will raise a ValueError when the output
    # is blank.
    session_context = mock_session.return_value.__enter__.return_value
    session_context.send.return_value.json.side_effect = ValueError()
    session_context.send.return_value.status_code = 200

    op = {"parameters": []}
    with pytest.raises(SystemExit) as pytest_wrapped_e:
        invoke("WIBBLE", "/wobble/wubble", op)

    assert pytest_wrapped_e.type == SystemExit
    assert pytest_wrapped_e.value.code == 0
예제 #5
0
def test_blank_output(mock_request, mock_yaml, mock_make_api_request_headers):
    """
    Test that endpoints that return blank results don't cause exceptions.

    We mock make_api_request_headers because the invoke function get the API
    key from that, so this test will fail in environments where an API key
    isn't in the env (e.g., travis).

    We mock yaml because otherwise yaml will complain about being able to
    serialize the mock response object.
    """

    mock_make_api_request_headers.return_value = {}

    # The response object's json method will raise a ValueError when the output
    # is blank.
    mock_request.return_value.json.side_effect = ValueError()

    op = {"parameters": []}
    invoke("WIBBLE", "/wobble/wubble", op)
예제 #6
0
def test_failure_exit_code(mock_session):
    """
    Test that we return a nonzero exit code when the API request fails.
    """
    # first test that we get a zero exit code when the API request succeeds
    session_context = mock_session.return_value.__enter__.return_value
    session_context.send.return_value.json.side_effect = ValueError()
    session_context.send.return_value.status_code = 200

    op = {"parameters": []}

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        invoke("WIBBLE", "/wobble/wubble", op)
    assert pytest_wrapped_e.value.code == 0

    # now test that we get a nonzero exit code when the API request fails
    session_context.send.return_value.status_code = 404

    with pytest.raises(SystemExit) as pytest_wrapped_e:
        invoke("WIBBLE", "/wobble/wubble", op)
    assert pytest_wrapped_e.value.code != 0