"""Test for ows_artist interface.""" from unittest.mock import MagicMock import flexmock from owsrequest import request import requests from feature_fm.constants.error import ERROR_CODE_OWS_ARTIST_REQUEST from feature_fm.constants.ows_services import CONTENT_TYPE from feature_fm.constants.ows_services import DEFAULT_PAGE_LIMIT from feature_fm.constants.ows_services import DEFAULT_PAGE_OFFSET from feature_fm.constants.ows_services import OWS_ARTIST from feature_fm.models import ows_artist def test_get_artists_default(): """Test getting artists with the default parameters.""" account_type = 'vendor' account_type_id = 1234 artists = { 'items': [ {'artist_id': 1, 'name': 'One', 'genres': []}, {'artist_id': 2, 'name': 'Two', 'genres': []} ], 'pagination': { 'offset': 0, 'total_records': 2, 'limit': 10 } } expected_path = ('/vendor/1234/full-artists' '?page_limit={0}&page_offset={1}' '&updated_since={2}').format( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, 0) ows_artist_response = flexmock(requests.models.Response()) ows_artist_response.status_code = 200 (flexmock(ows_artist_response) .should_receive('json') .and_return(artists)) (flexmock(request) .should_receive('get') .with_args( OWS_ARTIST, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_artist_response)) result = ows_artist.get_artists(account_type, account_type_id) assert result.status == 200 assert result.message == artists def test_get_artists_params(): """Test getting artists with specific params.""" account_type = 'vendor' account_type_id = 1234 page_limit = 20 page_offset = 10 updated_since = 1514764800 expected_path = ('/vendor/1234/full-artists' '?page_limit={0}&page_offset={1}' '&updated_since={2}').format( page_limit, page_offset, updated_since) ows_artist_response = flexmock(requests.models.Response()) ows_artist_response.status_code = 200 (flexmock(ows_artist_response) .should_receive('json') .and_return({})) (flexmock(request) .should_receive('get') .with_args( OWS_ARTIST, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_artist_response)) result = ows_artist.get_artists( account_type, account_type_id, page_limit, page_offset, updated_since) assert result.status == 200 def test_get_artists_error(): """Test getting artists and the request returns an error.""" account_type = 'vendor' account_type_id = 1234 ows_artist_error = 'ows_artist_error' ows_artist_response = flexmock(requests.models.Response()) ows_artist_response.status_code = 500 expected_path = ('/vendor/1234/full-artists' '?page_limit={0}&page_offset={1}' '&updated_since={2}').format( DEFAULT_PAGE_LIMIT, DEFAULT_PAGE_OFFSET, 0) (flexmock(ows_artist_response) .should_receive('text') .and_return(ows_artist_error)) (flexmock(request) .should_receive('get') .with_args( OWS_ARTIST, expected_path, headers={'Content-Type': CONTENT_TYPE}) .and_return(ows_artist_response)) result = ows_artist.get_artists(account_type, account_type_id) assert result.status == 500 assert result.errors == { 'code': ERROR_CODE_OWS_ARTIST_REQUEST, 'message': ows_artist_error } def test_get_artists_bulk(): """Test getting artists bulk.""" account_type = 'vendor' account_id = 1234 artist_ids = [1, 2] artists = { 'items': [ {'artist_id': 1, 'name': 'One', 'genres': []}, {'artist_id': 2, 'name': 'Two', 'genres': []} ] } expected_path = ('/{0}/{1}/full-artists/bulk').format( account_type, account_id) ows_artist_response = flexmock(requests.models.Response()) ows_artist_response.status_code = 200 (flexmock(ows_artist_response) .should_receive('json') .and_return(artists)) (flexmock(request) .should_receive('post') .with_args( OWS_ARTIST, expected_path, headers={'Content-Type': CONTENT_TYPE}, json={'artist_ids': artist_ids}) .and_return(ows_artist_response)) result = ows_artist.get_artists_bulk( account_type, account_id, artist_ids) assert result.status == 200 assert result.message == artists def test_get_artists_bulk_error(): """Test getting artists bulk and the request returns an error.""" account_type = 'vendor' account_id = 1234 artist_ids = [1, 2] ows_artist_error = 'ows_artist_error' ows_artist_response = flexmock(requests.models.Response()) ows_artist_response.status_code = 500 expected_path = ('/{0}/{1}/full-artists/bulk').format( account_type, account_id) (flexmock(ows_artist_response) .should_receive('text') .and_return(ows_artist_error)) (flexmock(request) .should_receive('post') .with_args( OWS_ARTIST, expected_path, headers={'Content-Type': CONTENT_TYPE}, json={'artist_ids': artist_ids}) .and_return(ows_artist_response)) result = ows_artist.get_artists_bulk( account_type, account_id, artist_ids) assert result.status == 500 assert result.errors == { 'code': ERROR_CODE_OWS_ARTIST_REQUEST, 'message': ows_artist_error } def test_get_all_artist_ids(monkeypatch): """Test getting all artist ids.""" account_type = 'vendor' account_id = 1234 first_response = requests.models.Response() first_response.status_code = 200 first_response.json = MagicMock(return_value={ 'items': [ {'id': 1, 'name': 'One'}, {'id': 2, 'name': 'Two'} ], 'pagination': { 'total_records': 4 } }) second_response = requests.models.Response() second_response.status_code = 200 second_response.json = MagicMock(return_value={ 'items': [ {'id': 3, 'name': 'Three'}, {'id': 4, 'name': 'Four'} ], 'pagination': { 'total_records': 4 } }) request_responses = [first_response, second_response] monkeypatch.setattr( request, 'get', MagicMock(side_effect=request_responses)) result = ows_artist.get_all_artist_ids(account_type, account_id) assert result assert result.message['items'] == [1, 2, 3, 4] def test_get_all_artist_ids_error(monkeypatch): """Test getting all artist ids when a request fails.""" account_type = 'vendor' account_id = 1234 request_response = requests.models.Response() request_response.status_code = 500 monkeypatch.setattr( request, 'get', MagicMock(return_value=request_response)) result = ows_artist.get_all_artist_ids(account_type, account_id) assert not result def test_get_artist_by_id(monkeypatch): """Test getting artist by id.""" account_type = 'vendor' account_id = 1234 artist_id = 123 request_response = requests.models.Response() request_response.status_code = 200 request_response.json = MagicMock(return_value={ 'name': 'Artist Name', 'genres': ['Latin Music'], 'id': 123}) expected_result = { 'name': 'Artist Name', 'genres': ['Latin Music'], 'artist_id': 123} monkeypatch.setattr( request, 'get', MagicMock(return_value=request_response)) result = ows_artist.get_artist_by_id(account_type, account_id, artist_id) assert result.message == expected_result assert result.status == 200 def test_get_artist_by_id_error(monkeypatch): """Test getting artist by id.""" account_type = 'vendor' account_id = 1234 artist_id = 123 request_response = requests.models.Response() request_response.status_code = 500 monkeypatch.setattr( request, 'get', MagicMock(return_value=request_response)) result = ows_artist.get_artist_by_id(account_type, account_id, artist_id) assert not result assert result.status == 500