"""Unit tests for utils for iTunes data ingestion workflow.""" import os from pathlib import Path from unittest.mock import MagicMock from unittest.mock import patch import boto3 from garcon_contrib.dynamo_feed_status import \ garcon_feed_status from moto import mock_aws from feed_ingestion.flows.itunes.util import itunes_helpers @patch('feed_ingestion.flows.itunes.config') def test_check_status(mock_config, monkeypatch): """Test check status method.""" mock_config.feed_name = 'itunes' # Check if status is less than POPULATED_RAW_TABLE. If yes, it should # return True monkeypatch.setattr( garcon_feed_status, 'get_overall_status', MagicMock(return_value='NOT_AVAILABLE')) skip_or_not = itunes_helpers.check_status( '2015-10-05', 'itunes_theorchard', 'POPULATED_RAW_TABLE') assert skip_or_not # Check if status is None. If yes, it should return True monkeypatch.setattr( garcon_feed_status, 'get_overall_status', MagicMock(return_value=None)) skip_or_not = itunes_helpers.check_status( '2015-10-05', 'itunes_theorchard', 'DOWNLOADED') assert skip_or_not # Check if status is less than INGESTED. If yes, it should return True monkeypatch.setattr( garcon_feed_status, 'get_overall_status', MagicMock(return_value='INGESTED')) skip_or_not = itunes_helpers.check_status( '2015-10-05', 'itunes_theorchard', 'POPULATED_RAW_TABLE') assert not skip_or_not @patch('feed_ingestion.flows.itunes.util.itunes_helpers.itunes_reporter') def test_download_raw_file(mock_reporter): """Test download_raw_file.""" mock_reporter_obj = MagicMock() mock_reporter_obj.download_itunes_file.return_value = None mock_reporter.get_reporter.return_value = mock_reporter_obj itunes_helpers.download_raw_file('2015-10-05', 'test') assert mock_reporter.get_reporter.called assert mock_reporter_obj.download_itunes_file.called @mock_aws def test_upload_raw_file_to_s3(monkeypatch, tmp_path): """Test upload_raw_file_to_s3.""" mock_account_details = { 'file_info': { 'file_name': 'some_file_name_{vendor_id}', }, 'vendor_id': 12345, 'destination_s3_path': 's3://a_bucket/some_s3_path_{file_name}_{date:%Y%m%d}' } s3_client = boto3.client('s3') bucket_name = 'a_bucket' s3_client.create_bucket(Bucket=bucket_name) # todo: utilize current_working_directory context and tmp_path cwd = Path('.') filename = cwd / 'some_file_name_12345' try: filename.write_text('file_content') itunes_helpers.upload_raw_file_to_s3( '2015-10-05', mock_account_details) finally: filename.unlink() existing_keys = s3_client.list_objects(Bucket=bucket_name)['Contents'] assert len(existing_keys) == 1 assert (existing_keys[0]['Key'] == 'some_s3_path_some_file_name_12345_20151005') s3_object = s3_client.get_object( Bucket=bucket_name, Key=existing_keys[0]['Key']) content = s3_object['Body'].read() assert content == b'file_content' def test_remove_raw_file_from_local(monkeypatch): """Test remove file.""" monkeypatch.setattr(os, 'remove', MagicMock(return_value=None)) itunes_helpers.remove_raw_file_from_local('some_file_name') os.remove.assert_any_call('./some_file_name')