"""Unit tests for YouTubeMonthlySL class overriding StageLoader component.""" from unittest import mock from unittest.mock import MagicMock from unittest.mock import patch from freezegun.api import FakeDatetime from feed_ingestion.flows.youtube_monthly.stage_loader import ( YouTubeMonthlySL ) @patch('feed_ingestion.flows.youtube_monthly.tasks.util') def test_clean_staging_raw_table(mock_util, monkeypatch): """Test clean_staging_raw_table method.""" executor = MagicMock() sql_loader = MagicMock() execute_mock = MagicMock() monkeypatch.setattr( YouTubeMonthlySL, 'resolve_sql_loader_and_execute', execute_mock ) stage_loader = YouTubeMonthlySL(executor, sql_loader) staging_raw_table = 'my_staging_raw_table' date = '2018-03-01' mock_util.get_first_last_day.return_value = ( FakeDatetime(2018, 3, 1), FakeDatetime(2018, 3, 31) ) stage_loader.clean_staging_raw_table( staging_raw_table, date ) assert execute_mock.call_count == 1 execute_mock.assert_called_with( 'delete_from_staging_raw', params=dict( db=mock.ANY, schema=mock.ANY, staging_raw_table=staging_raw_table, start_date=FakeDatetime(2018, 3, 1), end_date=FakeDatetime(2018, 3, 31) ) ) @patch('feed_ingestion.flows.youtube_monthly.tasks.util') def test_load_staging_raw_table(mock_util, monkeypatch): """Test load_staging_raw_table method.""" executor = MagicMock() sql_loader = MagicMock() execute_mock = MagicMock() monkeypatch.setattr( YouTubeMonthlySL, 'resolve_sql_loader_and_execute', execute_mock ) stage_loader = YouTubeMonthlySL(executor, sql_loader) stage_name = 'my_stage' staging_raw_table = 'my_staging_raw_table' date = '2018-03-01' source_files_dict = { 'files': [ { 'file_name': 'theorchardmusic_US.gz', 'file_size': 1000, 'file_source': 'orchard' }, { 'file_name': 'IODA_US.gz', 'file_size': 500, 'file_source': 'ioda' }, { 'file_name': 'DMGI_US.gz', 'file_size': 300, 'file_source': 'dmgi' } ] } stage_loader.load_staging_raw_table( staging_raw_table=staging_raw_table, source_files_dict=source_files_dict, date=date, stage_name=stage_name ) assert execute_mock.call_count == 3 for file_info in source_files_dict['files']: execute_mock.assert_any_call( 'load_staging_raw_{}'.format(file_info['file_source']), params=dict( db=mock.ANY, schema=mock.ANY, stage=stage_name, staging_raw_table=staging_raw_table, file_name=file_info['file_name'], file_size=file_info['file_size'], file_source=mock.ANY, download_date=date, ingestion_time=mock.ANY, ))