Ejemplo n.º 1
0
def test_api_response_without_request_response_shows_error_when_debugging():
    """An ApiResponse object that is initialized without a request response
    object shows an error in the json when debugging."""
    response = ApiResponse()

    # It's not yet initilized with an error
    assert response.error is None

    # But it will give an error converting to json (when debug mode is set)
    assert response.json()['error'] == \
        'Response has not yet been created with a requests.response.'
Ejemplo n.º 2
0
def test_api_response_can_be_converted_to_string():
    """ApiResponse can be converted to a string."""
    request_response = RequestResponseStub(payload={'test': 123},
                                           status_code=200)
    response = ApiResponse(request_response)

    expected_string = '{"status_code": 200, "success": true, "payload": ' \
        + '{"test": 123}}'

    assert response.string() == expected_string
    assert str(response) == expected_string
Ejemplo n.º 3
0
def test_api_response_can_have_error_set_on_response_wout_request_response():
    """An ApiResponse object can have an error set on a response that has been
    initilized without a request response."""
    response = ApiResponse()

    response.error = 'Test error'

    assert response.error == 'Test error'
    assert response.response is None
    assert not response.success
    assert not response.status_code
    assert response.payload == {}
Ejemplo n.º 4
0
def test_api_response_can_be_initialized_without_request_response():
    """An ApiResponse object can be initialized without a request response
    object."""
    response = ApiResponse()

    assert not response.error
    assert not response.response
    assert not response.success
    assert not response.status_code
    assert response.payload == {}
    assert response.json() == {
            'status_code':  None,
            'success':      False,
            'payload':      {}
        }
Ejemplo n.º 5
0
def test_api_response_works_with_request_response_int():
    """An ApiResponse object works with a request response object that is
    simply an integer."""
    request_response = RequestResponseStub(payload=5, status_code=200)
    response = ApiResponse(request_response)

    assert response.payload == 5
Ejemplo n.º 6
0
def test_api_response_can_be_initialized_with_request_response():
    """An ApiResponse object can be initialized with a request response object.
    """
    request_response = RequestResponseStub(payload={'test': 123},
                                           status_code=200)
    response = ApiResponse(request_response)

    assert not response.error
    assert response.response == request_response
    assert response.success
    assert str(response.status_code) == '200'
    assert response.payload == {'test': 123}
    assert response.json() == {
            'status_code':   200,
            'success':      True,
            'payload':      {'test': 123}
        }
Ejemplo n.º 7
0
def test_api_response_works_with_request_response_list():
    """An ApiResponse object works with a request response object that is a
    list."""
    request_response = RequestResponseStub(payload=[{'test1': 123},
                                                    {'test2': 456},
                                                    {'test3': 789}],
                                           status_code=200)
    response = ApiResponse(request_response)

    assert response.payload == [{'test1': 123},
                                {'test2': 456},
                                {'test3': 789}]
Ejemplo n.º 8
0
def test_api_response_payload_is_normalized_to_snake_case():
    """An ApiResponse object's payload keys are normalized to snake case."""
    pre_normalized_data = {
        'test':          123,
        'testTest':      123,
        'testTestTest':  123,
        'testTTL':       123,
    }
    request_response = RequestResponseStub(payload=pre_normalized_data)
    response = ApiResponse(request_response)

    assert response.payload == {
        'test':            123,
        'test_test':       123,
        'test_test_test':  123,
        'test_ttl':        123,
    }