"""Unit tests for megaphone inventory importer.""" from unittest.mock import call, patch from scripts import megaphone_inventory_ingester from tests.utils import db_operations @patch('requests.get') @patch('podcast.models.api_campaign.ApiCampaign.update_advertisement') @patch('podcast.models.ad_action.update_ad_action') @patch('podcast.models.ad_action.get_ad_actions_by_advertisement_ids') @patch('podcast.models.advertisement.get_advertisements_by_order_ids') def test_update_outdated_ad_actions_relink_and_repush_audio( mock_get_advertisements, mock_get_ad_actions, mock_update, mock_update_advertisement, mock_get, advertisement_fixture, ad_actions_fixture, ad_action_asset_fixture, megaphone_ads_fixture ): """Test does relink missing ad actions.""" mock_get_advertisements.return_value = advertisement_fixture mock_get_ad_actions.side_effect = [ { 'items': ad_actions_fixture }, { 'items': [] } ] mock_update.return_value = db_operations.ad_action_data[7] mock_get.return_value.status_code = 200 mock_get.return_value.json.return_value = ad_action_asset_fixture megaphone_inventory_ingester.update_outdated_ad_actions( megaphone_ads_fixture, 'order_id', 'asset_transcoder_api_url') mock_get_advertisements.assert_called_once_with(['order_id']) mock_get_ad_actions.assert_has_calls([ call(['an-old-ad-id', 'another-old-ad-id']), call(['an-ad-id-pre']) ]) mock_update.assert_has_calls([ call(3, {'advertisement_id': 'an-ad-id-pre'}), call(2, {'advertisement_id': 'an-ad-id-pre'}), call(1, {'advertisement_id': 'an-ad-id-pre'}) ]) mock_get.assert_called_once_with( 'asset_transcoder_api_url/assets-by-ids-and-types', headers={ 'Grass-Account-Type': 'vendor', 'Grass-Account-Id': '1', 'Correlation-Id': '12', 'Orchard-Identity-UUID': 'podcast-admin', 'Content-Type': 'application/json' }, params={'object_ids': [8], 'object_types': ['adupload']} ) mock_update_advertisement.assert_called_once_with( 'campaign_id', 'order_id', 'an-ad-id-pre', {'backgroundAudioFileUrl': 'signed_url'}) @patch('scripts.megaphone_inventory_ingester.update_megaphone_audio') @patch('podcast.models.ad_action.update_ad_action') @patch('podcast.models.ad_action.get_ad_actions_by_advertisement_ids') @patch('podcast.models.advertisement.get_advertisements_by_order_ids') def test_update_outdated_ad_actions_only_relink_1( mock_get_advertisements, mock_get_ad_actions, mock_update, mock_update_megaphone_audio, advertisement_fixture, ad_actions_fixture, megaphone_ads_fixture ): """Test does relink missing ad actions and doesn't re-push audio. If a host read ad exists having new megaphone advertisement id then it shouldn't re-push audio. """ mock_get_advertisements.return_value = advertisement_fixture mock_get_ad_actions.side_effect = [ { 'items': ad_actions_fixture }, { 'items': [ { 'id': 4, 'advertisement_id': 'an-ad-id-pre', 'roll_type': 'pre', 'status': 'completed', 'due_date': '2021-08-30 22:00:00' } ] } ] megaphone_inventory_ingester.update_outdated_ad_actions( megaphone_ads_fixture, 'order_id', 'asset_transcoder_api_url') mock_get_advertisements.assert_called_once_with(['order_id']) mock_get_ad_actions.assert_has_calls([ call(['an-old-ad-id', 'another-old-ad-id']), call(['an-ad-id-pre']) ]) mock_update.assert_has_calls([ call(3, {'advertisement_id': 'an-ad-id-pre'}), call(2, {'advertisement_id': 'an-ad-id-pre'}), call(1, {'advertisement_id': 'an-ad-id-pre'}) ]) mock_update_megaphone_audio.assert_not_called() @patch('scripts.megaphone_inventory_ingester.update_megaphone_audio') @patch('podcast.models.ad_action.update_ad_action') @patch('podcast.models.ad_action.get_ad_actions_by_advertisement_ids') @patch('podcast.models.advertisement.get_advertisements_by_order_ids') def test_update_outdated_ad_actions_only_relink_2( mock_get_advertisements, mock_get_ad_actions, mock_update, mock_update_megaphone_audio, advertisement_fixture, ad_actions_fixture, megaphone_ads_fixture ): """Test does relink missing ad actions and doesn't re-push audio. If out of sync host read ads doesn't have audio then it shouldn't re-push audio. """ mock_get_advertisements.return_value = advertisement_fixture mock_get_ad_actions.side_effect = [ { 'items': [ { 'id': 1, 'advertisement_id': 'an-old-ad-id', 'roll_type': 'pre', 'status': 'submitted', 'due_date': '2021-08-10 22:00:00', 'created_date': '2021-08-01 22:00:00', } ] }, { 'items': [] } ] megaphone_inventory_ingester.update_outdated_ad_actions( megaphone_ads_fixture, 'order_id', 'asset_transcoder_api_url') mock_get_advertisements.assert_called_once_with(['order_id']) mock_get_ad_actions.assert_has_calls([ call(['an-old-ad-id', 'another-old-ad-id']), call(['an-ad-id-pre']) ]) mock_update.assert_called_once_with(1, {'advertisement_id': 'an-ad-id-pre'}) mock_update_megaphone_audio.assert_not_called() @patch('podcast.models.ad_action.update_ad_action') @patch('podcast.models.ad_action.get_ad_actions_by_advertisement_ids') @patch('podcast.models.advertisement.get_advertisements_by_order_ids') def test_update_outdated_ad_actions_no_link( mock_get_advertisements, mock_get_ad_actions, mock_update, megaphone_ads_fixture ): """Test does not update missing ad actions if advertisement missing.""" mock_get_advertisements.return_value = { 'items': [ { 'campaign_order_megaphone_id': 'order_id', 'id': 'an-old-ad-id', 'insertion_point': 'post' }, { 'campaign_order_megaphone_id': 'order_id', 'id': 'another-old-ad-id', 'insertion_point': 'post' } ] } mock_get_ad_actions.return_value = { 'items': [ {'id': 1, 'advertisement_id': 'an-old-ad-id', 'roll_type': 'post'}, {'id': 3, 'advertisement_id': 'another-old-ad-id', 'roll_type': 'post'} ] } megaphone_inventory_ingester.update_outdated_ad_actions( megaphone_ads_fixture, 'order_id', 'asset_transcoder_api_url') mock_get_advertisements.assert_called_once_with(['order_id']) mock_get_ad_actions.assert_called_once_with(['an-old-ad-id', 'another-old-ad-id']) mock_update.assert_not_called()