"""Lambda test module.""" import requests import index from models import ows_artist from models import ows_product from models import ows_product_digital from models import ows_track def test_handler_process_release_artist( mocker, product_info_fixture, filter_artists_info_fixture, digital_product_fixture, release_artist_kinesis_event_fixture, product_id, release_artist_db_row, release_artist_fixture_with_artist_info_id, session): """Process release artist with existing artist info. Artist_info is not created because it already exists. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value=filter_artists_info_fixture) mocker.patch.object( ows_product_digital, 'update_digital_product_info', return_value=digital_product_fixture) mocker.spy(ows_artist, 'create_artist_info') index.handler(release_artist_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( release_artist_db_row['artist_name'], product_info_fixture['vendor_id'], session ) ows_product_digital.update_digital_product_info.assert_called_with( { 'product_id': digital_product_fixture['product_id'], 'product_artists': [{ 'id': release_artist_fixture_with_artist_info_id['id'], 'artist_info_id': release_artist_fixture_with_artist_info_id[ 'artist_info_id'], }] }, session ) assert not ows_artist.create_artist_info.called def test_handler_process_track_artist( mocker, product_info_fixture, filter_artists_info_fixture, track_artist_kinesis_event_fixture, track_id, product_id, track_artist_db_row, get_track_info_fixture, session): """Process track artist with existing artist info. Artist_info is not created because it already exists. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value=filter_artists_info_fixture) mocker.patch.object( ows_track, 'get_track_info', return_value=get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.spy(ows_artist, 'create_artist_info') index.handler(track_artist_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_artist_db_row['name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, track_artist_db_row['type'], track_artist_db_row['id'], {'artist_info_id': filter_artists_info_fixture['items'][0]['id']}, session ) assert not ows_artist.create_artist_info.called def test_handler_process_track_artist_create_artist_info( mocker, product_info_fixture, track_id, create_artist_fixture, track_artist_kinesis_event_fixture, product_id, track_artist_db_row, artist_name, get_track_info_fixture, vendor_id, session): """Process track artist create artist_info. No matching artist_info is found so it is created. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler(track_artist_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_artist_db_row['name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, track_artist_db_row['type'], track_artist_db_row['id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_track_artist_existing_artist_info_id( mocker, track_artist_with_artist_info_id_kinesis_event_fixture): """Process a track artist with an existing artist_info_id.""" mocker.spy(ows_product, 'get_product_info') mocker.spy(ows_artist, 'create_artist_info') mocker.spy(ows_artist, 'filter_artists_info') mocker.spy(ows_track, 'get_track_info') mocker.spy(ows_track, 'update_track_contributor') index.handler( track_artist_with_artist_info_id_kinesis_event_fixture, None) assert not ows_product.get_product_info.called assert not ows_artist.create_artist_info.called assert not ows_artist.filter_artists_info.called assert not ows_track.get_track_info.called assert not ows_track.update_track_contributor.called def test_handler_process_track_artist_existing_artist_info_id_update( mocker, product_info_fixture, track_id, create_artist_fixture, track_artist_kinesis_update_event_fixture, product_id, track_artist_db_row, artist_name, get_track_info_fixture, vendor_id, session): """Process track artist update event. No matching artist_info is found so it is created. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler(track_artist_kinesis_update_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_artist_db_row['name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, track_artist_db_row['type'], track_artist_db_row['id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_track_artist_existing_artist_info_id_update_same_name( mocker, product_info_fixture, track_id, create_artist_fixture, track_artist_with_artist_info_id_kinesis_update_event_fixture, product_id, track_artist_db_row, artist_name, get_track_info_fixture, vendor_id, session ): """Process track artist update event with an existing artist_info_id. Even though the record has an existing artist_info_id it needs to update the value because the name has changed. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler( track_artist_with_artist_info_id_kinesis_update_event_fixture, None ) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_artist_db_row['name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, track_artist_db_row['type'], track_artist_db_row['id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_release_artist_create_artist_info( mocker, product_info_fixture, digital_product_fixture, release_artist_kinesis_event_fixture, product_id, release_artist_db_row, artist_name, create_artist_fixture, release_artist_fixture_with_artist_info_id, session, vendor_id): """Process release artist create artist_info. No matching artist_info is found so it is created. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_product_digital, 'update_digital_product_info', return_value=digital_product_fixture) mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler(release_artist_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( release_artist_db_row['artist_name'], product_info_fixture['vendor_id'], session ) ows_product_digital.update_digital_product_info.assert_called_with( { 'product_id': digital_product_fixture['product_id'], 'product_artists': [{ 'id': release_artist_fixture_with_artist_info_id['id'], 'artist_info_id': release_artist_fixture_with_artist_info_id[ 'artist_info_id'] }] }, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_release_artist_existing_artist_info_id( mocker, release_artist_with_artist_info_id_kinesis_event_fixture): """Process a release artist with an existing artist_info_id.""" mocker.spy(ows_product, 'get_product_info') mocker.spy(ows_artist, 'create_artist_info') mocker.spy(ows_artist, 'filter_artists_info') mocker.spy(ows_product_digital, 'get_digital_product_info') mocker.spy(ows_product_digital, 'update_digital_product_info') index.handler( release_artist_with_artist_info_id_kinesis_event_fixture, None) assert not ows_product.get_product_info.called assert not ows_artist.create_artist_info.called assert not ows_artist.filter_artists_info.called assert not ows_product_digital.get_digital_product_info.called assert not ows_product_digital.update_digital_product_info.called def test_handler_process_release_artist_delete( mocker, release_artist_kinesis_delete_event_fixture): """Process a delete release artist event.""" mocker.spy(requests, 'get') mocker.spy(requests, 'put') mocker.spy(requests, 'post') index.handler(release_artist_kinesis_delete_event_fixture, None) assert not requests.get.called assert not requests.put.called assert not requests.post.called def test_handler_process_track_artist_delete( mocker, track_artist_kinesis_delete_event_fixture): """Process a delete track artist event.""" mocker.spy(requests, 'get') mocker.spy(requests, 'put') mocker.spy(requests, 'post') index.handler(track_artist_kinesis_delete_event_fixture, None) assert not requests.get.called assert not requests.put.called assert not requests.post.called def test_handler_process_track_writer( mocker, product_info_fixture, filter_artists_info_fixture, track_writer_kinesis_event_fixture, track_id, product_id, track_writer_db_row, track_writer_get_track_info_fixture, session): """Process track writer with existing artist info. Artist_info is not created because it already exists. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value=filter_artists_info_fixture) mocker.patch.object( ows_track, 'get_track_info', return_value=track_writer_get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.spy(ows_artist, 'create_artist_info') index.handler(track_writer_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_writer_db_row['writer_name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, 'writer', track_writer_db_row['track_writer_id'], {'artist_info_id': filter_artists_info_fixture['items'][0]['id']}, session ) assert not ows_artist.create_artist_info.called def test_handler_process_track_writer_create_artist_info( mocker, product_info_fixture, track_id, create_artist_fixture, track_writer_kinesis_event_fixture, product_id, track_writer_db_row, artist_name, track_writer_get_track_info_fixture, vendor_id, session): """Process track writer create artist_info. No matching artist_info is found so it is created. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=track_writer_get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler(track_writer_kinesis_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_writer_db_row['writer_name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, 'writer', track_writer_db_row['track_writer_id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_track_writer_existing_artist_info_id( mocker, track_writer_with_artist_info_id_kinesis_event_fixture): """Process a track writer with an existing artist_info_id.""" mocker.spy(ows_product, 'get_product_info') mocker.spy(ows_artist, 'create_artist_info') mocker.spy(ows_artist, 'filter_artists_info') mocker.spy(ows_track, 'get_track_info') mocker.spy(ows_track, 'update_track_contributor') index.handler( track_writer_with_artist_info_id_kinesis_event_fixture, None) assert not ows_product.get_product_info.called assert not ows_artist.create_artist_info.called assert not ows_artist.filter_artists_info.called assert not ows_track.get_track_info.called assert not ows_track.update_track_contributor.called def test_handler_process_track_writer_delete( mocker, track_writer_kinesis_delete_event_fixture): """Process a delete track writer event.""" mocker.spy(requests, 'get') mocker.spy(requests, 'put') mocker.spy(requests, 'post') index.handler(track_writer_kinesis_delete_event_fixture, None) assert not requests.get.called assert not requests.put.called assert not requests.post.called def test_handler_process_track_writer_existing_artist_info_id_update( mocker, product_info_fixture, track_id, create_artist_fixture, track_writer_kinesis_update_event_fixture, product_id, track_writer_db_row, artist_name, track_writer_get_track_info_fixture, vendor_id, session ): """Process track writer update event. No matching artist_info is found so it is created. """ mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=track_writer_get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler(track_writer_kinesis_update_event_fixture, None) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_writer_db_row['writer_name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, 'writer', track_writer_db_row['track_writer_id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_track_writer_existing_artist_info_id_update_same_name( mocker, product_info_fixture, track_id, create_artist_fixture, track_writer_with_artist_info_id_kinesis_update_event_fixture, product_id, track_writer_db_row, artist_name, track_writer_get_track_info_fixture, vendor_id, session ): """Process track writer with an artist_info_id update event.""" mocker.patch.object( ows_product, 'get_product_info', return_value=product_info_fixture) mocker.patch.object( ows_artist, 'filter_artists_info', return_value={'items': []}) mocker.patch.object( ows_track, 'get_track_info', return_value=track_writer_get_track_info_fixture) mocker.patch.object(ows_track, 'update_track_contributor') mocker.patch.object( ows_artist, 'create_artist_info', return_value=create_artist_fixture) index.handler( track_writer_with_artist_info_id_kinesis_update_event_fixture, None ) ows_product.get_product_info.assert_called_with(product_id, session) ows_artist.filter_artists_info.assert_called_with( track_writer_db_row['writer_name'], product_info_fixture['vendor_id'], session ) ows_track.get_track_info.assert_called_with(track_id, session) ows_track.update_track_contributor.assert_called_with( track_id, 'writer', track_writer_db_row['track_writer_id'], {'artist_info_id': create_artist_fixture['id']}, session ) ows_artist.create_artist_info.assert_called_with( artist_name, vendor_id, session ) def test_handler_process_track_artist_with_blank_name( mocker, blank_track_artist_kinesis_event_fixture): """Process a track artist with blank name.""" mocker.spy(ows_product, 'get_product_info') mocker.spy(ows_artist, 'create_artist_info') mocker.spy(ows_artist, 'filter_artists_info') mocker.spy(ows_track, 'get_track_info') mocker.spy(ows_track, 'update_track_contributor') index.handler( blank_track_artist_kinesis_event_fixture, None) assert not ows_product.get_product_info.called assert not ows_artist.create_artist_info.called assert not ows_artist.filter_artists_info.called assert not ows_track.get_track_info.called assert not ows_track.update_track_contributor.called def test_handler_process_track_writer_with_blank_name( mocker, blank_track_writer_kinesis_event_fixture): """Process a track writer with blank writer_name.""" mocker.spy(ows_product, 'get_product_info') mocker.spy(ows_artist, 'create_artist_info') mocker.spy(ows_artist, 'filter_artists_info') mocker.spy(ows_track, 'get_track_info') mocker.spy(ows_track, 'update_track_contributor') index.handler( blank_track_writer_kinesis_event_fixture, None) assert not ows_product.get_product_info.called assert not ows_artist.create_artist_info.called assert not ows_artist.filter_artists_info.called assert not ows_track.get_track_info.called assert not ows_track.update_track_contributor.called