"""Unit tests for YouTubeAssetSL.""" from unittest import mock from unittest.mock import MagicMock from unittest.mock import patch import pytest from feed_ingestion.flows.youtube_asset.stage_loader import YouTubeAssetSL class TestStageLoader(object): """Unit tests for YouTubeAssetSL.""" @pytest.fixture def mock_sql_loader(self): """Return sql_loader mock.""" sql_loader_path = ( 'feed_ingestion.flows.spotify.snowflake_executor.sql_loader') with patch(sql_loader_path) as sql_loader: yield sql_loader @pytest.fixture def mock_stage_loader(self, mock_sql_loader): """Yield executor context.""" stage_loader = YouTubeAssetSL(MagicMock(), mock_sql_loader) stage_loader.resolve_sql_loader_and_execute = MagicMock() yield stage_loader def test_load_staging_raw_table_the_orchard(self, mock_stage_loader): """Test load_staging_raw_table.""" stage_name = 'test_stage' staging_raw = 'test_staging_raw' date = '2018-01-01' source_files_dict = { 'files': [ {'file_name': 'test1', 'file_size': 42}, {'file_name': 'dmgi', 'file_size': 42}, {'file_name': 'test2', 'file_size': 73}]} licensor = 'theorchard' mock_stage_loader.load_staging_raw_table( staging_raw_table=staging_raw, source_files_dict=source_files_dict, date=date, stage_name=stage_name, licensor=licensor ) assert mock_stage_loader.resolve_sql_loader_and_execute.call_count == 3 for file in source_files_dict['files']: if file['file_name'] == 'dmgi': sql_template_name = 'load_staging_raw_theorchard_dmgi' else: sql_template_name = 'load_staging_raw' mock_stage_loader.resolve_sql_loader_and_execute.assert_any_call( sql_template_name, params=dict( db=mock.ANY, schema=mock.ANY, stage=stage_name, staging_raw_table=staging_raw, file_name=file['file_name'], file_size=file['file_size'], licensor=licensor, download_date=date, ingestion_time=mock.ANY)) def test_load_staging_raw_table_sme(self, mock_stage_loader): """Test load_staging_raw_table.""" stage_name = 'test_stage' staging_raw = 'test_staging_raw' date = '2018-01-01' source_files_dict = { 'files': [ {'file_name': 'test1', 'file_size': 42}, {'file_name': 'test2', 'file_size': 73}]} licensor = 'sme' mock_stage_loader.load_staging_raw_table( staging_raw_table=staging_raw, source_files_dict=source_files_dict, date=date, stage_name=stage_name, licensor=licensor ) assert mock_stage_loader.resolve_sql_loader_and_execute.call_count == 2 for file in source_files_dict['files']: sql_template_name = 'load_staging_raw_sme' mock_stage_loader.resolve_sql_loader_and_execute.assert_any_call( sql_template_name, params=dict( db=mock.ANY, schema=mock.ANY, stage=stage_name, staging_raw_table=staging_raw, file_name=file['file_name'], file_size=file['file_size'], licensor=licensor, download_date=date, ingestion_time=mock.ANY))