"""Test for the ows_artist module.""" import json from flexmock import flexmock from owsrequest import request from owsrequest import test_utils import requests from label_copy_export.connectors import loggly from label_copy_export.constants import ows_services from label_copy_export.models import ows_artist logger = loggly.get_current_logger() TEST_ARTIST_ID = '1234567890' def test_get_ows_artist_response_404_json(monkeypatch): """Ensure that 404 response doesn't breaks the flow. Test that error message is propagated in response.errors. """ error_json = {'text': 'not found'} call_spec = { 'service': ows_services.OWS_ARTIST, 'path': '/artist/None', 'status': 404, 'json': error_json } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_artist.get_ows_artist(None) assert not result assert json.loads(result.errors) == error_json assert result.status == call_spec['status'] def test_get_ows_artist_response_with_valid_data(monkeypatch): """Test that with valid data the result will be successful.""" valid_json = {'name': 'NewOne'} call_spec = { 'service': ows_services.OWS_ARTIST, 'path': '/artist/{}'.format(TEST_ARTIST_ID), 'status': 200, 'json': valid_json } mocked_process = test_utils.mock_ows_requests([call_spec]) monkeypatch.setattr( request, 'process', mocked_process, raising=False) result = ows_artist.get_ows_artist(TEST_ARTIST_ID) assert result assert result.message == valid_json def test_get_ows_artist_response_invalid_json(): """Test get_ows_artist with invalid JSON in response. Tested function should return decoded original message on JSON decode exception. """ not_valid_json = b'non-JSON content' response = flexmock(requests.models.Response()) response._content = not_valid_json response.status_code = 401 flexmock(request).should_receive('process').and_return(response) result = ows_artist.get_ows_artist(TEST_ARTIST_ID) assert not result assert result.errors == not_valid_json.decode('utf8')