"""Unit tests for the FTP related tasks.""" from unittest.mock import call from unittest.mock import MagicMock from garcon_contrib.ftp import garcon_ftp import pytest from feed_ingestion.tasks import ftp_tasks @pytest.fixture() def ftp_conf_mock(): """Mock ftp configuration.""" return { 'host': 'xbox_host', 'username': 'xbox_username', 'password': 'xbox_password', 'port': 22, 'path': '/path/to/xbox' } def test_fetch_from_drop_location(monkeypatch, ftp_conf_mock): """Test fetch_from_drop_location task.""" date = '2017-12-04' s3_archive_path = 's3://nasty_bucket/awful_path' source_files_dict = { 'files': [ {'file_name': 'awesome_file'}, {'file_name': 'gorgeous_file'}, {'file_name': 'sublime_file'}, ] } def copy_side_effect( activity, ftp_conf, ftp_path, src_file_name, s3_path, dst_file_name): return { 'awesome_file': { 'file_name': 'awesome_file', 'status': True, 'file_size': 1000 }, 'gorgeous_file': { 'file_name': 'gorgeous_file', 'status': True, 'file_size': 2000 }, 'sublime_file': { 'file_name': 'sublime_file', 'status': True, 'file_size': 3000 } }[src_file_name] copy_from_ftp_mock = MagicMock() copy_from_ftp_mock.side_effect = copy_side_effect activity_mock = MagicMock() monkeypatch.setattr( garcon_ftp, 'copy_file_from_ftp_to_s3', value=copy_from_ftp_mock) response = ftp_tasks.fetch_from_drop_location( activity_mock, date, source_files_dict, s3_archive_path, ftp_conf_mock) calls = [ call( activity_mock, ftp_conf_mock, ftp_conf_mock['path'], file_dict['file_name'], s3_archive_path, file_dict['file_name']) for file_dict in source_files_dict['files'] ] copy_from_ftp_mock.assert_has_calls(calls, any_order=True) assert response == dict( source_files_dict=dict( files=[ { 'file_name': 'awesome_file', 'found': True, 'file_size': 1000 }, { 'file_name': 'gorgeous_file', 'found': True, 'file_size': 2000 }, { 'file_name': 'sublime_file', 'file_size': 3000, 'found': True } ] ) ) def test_fetch_from_drop_location_no_req_file(monkeypatch, ftp_conf_mock): """Test fetch_from_drop_location task when not all req files found.""" date = '2017-12-04' s3_archive_path = 's3://nasty_bucket/awful_path' source_files_dict = { 'files': [ {'file_name': 'awesome_file'}, {'file_name': 'gorgeous_file'}, {'file_name': 'sublime_file'}, ] } def copy_side_effect( activity, ftp_conf, ftp_path, src_file_name, s3_path, dst_file_name): return { 'awesome_file': { 'file_name': 'awesome_file', 'status': True, 'file_size': 1000 }, 'gorgeous_file': { 'file_name': 'gorgeous_file', 'status': True, 'file_size': 2000 }, 'sublime_file': { 'file_name': 'sublime_file', 'status': False, } }[src_file_name] copy_from_ftp_mock = MagicMock() copy_from_ftp_mock.side_effect = copy_side_effect activity_mock = MagicMock() monkeypatch.setattr( garcon_ftp, 'copy_file_from_ftp_to_s3', value=copy_from_ftp_mock) resp = ftp_tasks.fetch_from_drop_location( activity_mock, date, source_files_dict, s3_archive_path, ftp_conf_mock) assert resp == {'stop': True} def test_fetch_from_drop_location_req_attr(monkeypatch, ftp_conf_mock): """Test fetch_from_drop_location task when not all req files found.""" date = '2017-12-04' s3_archive_path = 's3://nasty_bucket/awful_path' source_files_dict = { 'files': [ { 'file_name': 'awesome_file', 'required': True }, {'file_name': 'gorgeous_file'}, {'file_name': 'sublime_file'}, ] } def copy_side_effect( activity, ftp_conf, ftp_path, src_file_name, s3_path, dst_file_name): return { 'awesome_file': { 'file_name': 'awesome_file', 'status': False, }, 'gorgeous_file': { 'file_name': 'gorgeous_file', 'status': True, 'file_size': 2000 }, 'sublime_file': { 'file_name': 'sublime_file', 'status': False, } }[src_file_name] copy_from_ftp_mock = MagicMock() copy_from_ftp_mock.side_effect = copy_side_effect activity_mock = MagicMock() monkeypatch.setattr( garcon_ftp, 'copy_file_from_ftp_to_s3', value=copy_from_ftp_mock) resp = ftp_tasks.fetch_from_drop_location( activity_mock, date, source_files_dict, s3_archive_path, ftp_conf_mock) assert resp == {'stop': True}