"""Test response.""" import json from sony_metadata import response def test_response_creation(): """Test the default response creation.""" resp = response.Response() assert not resp.message assert not resp.errors assert resp assert resp.status == 200 def test_response_creation_with_message(): """Test the creation of a response with a message.""" message = 'something' resp = response.Response(message=message) assert resp assert resp.message == message assert resp.status == 200 def test_response_creation_with_errors(): """Test the response creation with errors.""" error = 'something' resp = response.Response(errors=error) assert not resp.message assert not resp assert resp.errors == error assert resp.status == 400 # default def test_create_error_response(): """Test the creation of a fatal response.""" error = 'something' calls = [ (response.create_fatal_response, 500, response.ERROR_CODE_INTERNAL_ERROR), (response.create_not_found_response, 404, response.ERROR_CODE_NOT_FOUND)] for call, status, code in calls: resp = call() assert not resp assert resp.status == status resp = call(error) assert resp.errors.get('code') == code assert resp.errors.get('message') == error resp = response.create_error_response('code', error) assert resp.errors.get('code') == 'code' assert resp.errors.get('message') == error def test_flaskify_dict_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message=dict(key='value'))) assert resp.data == b'{"key": "value"}' assert resp.mimetype == 'application/json' assert resp.status_code == 200 def test_flaskify_list_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message=['value1', 'value2'])) assert resp.data == b'["value1", "value2"]' assert resp.mimetype == 'application/json' assert resp.status_code == 200 def test_flaskify_string_response(): """Test flaskifying a response.""" resp = response.flaskify(response.Response(message='value1')) assert resp.data == b'value1' assert resp.mimetype == 'text/plain' assert resp.status_code == 200 def test_flaskify_error_response(): """Flaskify an error response.""" resp = response.flaskify(response.create_fatal_response('Fatal Error')) assert resp.status_code == 500 data = json.loads(resp.data.decode('utf8')) assert data == {'message': 'Fatal Error', 'code': 'internal_error'}