"""Test for ows-artist model.""" from owsrequest import request from owsrequest import test_utils from owsrequest.test_utils import MockOwsResponse from project_manager.models import ows_artist def raise_value_error(_): """Raise error when parsing json.""" raise ValueError('No json here!') def test_get_artist_info(monkeypatch, get_artist_info_ok): """Test successful request for artist information from ows-artist.""" artist_id = 123 artist_payload = { 'id': artist_id, 'name': 'Solange Knowles', 'artist_type': 'artist'} monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([get_artist_info_ok])) response = ows_artist.get_artist_info(artist_id) assert response.status == 200 assert response.message.get('name') == artist_payload['name'] def test_get_artist_info_not_found( monkeypatch, get_artist_info_not_found): """Test `Not Found` response when fetching an artist's information.""" artist_id = 123 data = {'code': 'not_found_error', 'message': 'not found message'} monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([get_artist_info_not_found])) response = ows_artist.get_artist_info(artist_id) assert response.status == 404 assert response.errors.get('code') == data['code'] assert response.errors.get('message') == data['message'] def test_get_artist_info_fatal_error(monkeypatch): """Test `500 Error` when fetching an artist's information.""" artist_id = 123 monkeypatch.setattr( request, 'get', test_utils.mock_ows_requests([{ 'service': 'ows-artist', 'path': '/artist/{}'.format(artist_id), 'status': 500}]) ) monkeypatch.setattr(MockOwsResponse, 'json', raise_value_error) response = ows_artist.get_artist_info(artist_id) assert response.status == 500 assert response.errors['code'] == 'Internal Error' def test_create_artist_info(monkeypatch, create_artist_info_ok): """Test successful request for artist information from ows-artist.""" artist = { 'name': 'Solange Knowles', 'vendor_id': 123, 'subaccount_id': 0 } monkeypatch.setattr( request, 'post', test_utils.mock_ows_requests([create_artist_info_ok])) response = ows_artist.create_artist_info(artist) assert response.status == 200 assert response.message.get('name') == artist['name'] def test_create_artist_info_bad_params(monkeypatch, create_artist_info_ok): """Test bad params request for artist information from ows-artist.""" artist = { 'name': 'Solange Knowles' } monkeypatch.setattr( request, 'post', test_utils.mock_ows_requests([create_artist_info_ok])) response = ows_artist.create_artist_info(artist) assert response.status == 400 def test_create_artist_info_fatal_error(monkeypatch): """Test `500 Error` when fetching an artist's information.""" artist = { 'name': 'test', 'vendor_id': 123, 'subaccount_id': 0 } monkeypatch.setattr( request, 'post', test_utils.mock_ows_requests([{ 'service': 'ows-artist', 'path': '/artist', 'status': 500}]) ) monkeypatch.setattr(MockOwsResponse, 'json', raise_value_error) response = ows_artist.create_artist_info(artist) assert response.status == 500 assert response.errors['code'] == 'Internal Error'