"""Tests for logic_utils.py.""" from types import SimpleNamespace from unittest.mock import MagicMock, patch from util import logic_utils import pytest s3_resource = 's3_resource' bucket_name = 'bucket' key = 'key' table_name = 'table_name' backup_key = 'backup_key' source_key = 'source_key' target_key = 'target_key' deletion_key = 'deletion_key' # Local fixture @pytest.fixture def test_print_log(mocker): """Create fixture for logger.""" return mocker.patch('integration_scripts.logger.info') @pytest.fixture def test_get_s3_file_key(mocker): """Create fixture for get_s3_file_key.""" return mocker.patch( 'util.logic_utils.get_s3_file_key') # Working -------------------------------------------------------------------- def test_check_terminal_conditions_pass(mocker): """Test check if any terminal condition has been met.""" # cmd_args = SimpleNamespace(tablenum=1) free_space = SimpleNamespace(free=99999999999999) mock_psutil = mocker.patch('psutil.disk_usage', return_value=free_space) mock_is_file = mocker.patch('os.path.isfile', return_value=False) start_call_count = mock_is_file.call_count end_process, msg = logic_utils.check_terminal_conditions(tablenum=1) assert mock_psutil.call_count == 1 assert mock_is_file.call_count == start_call_count + 1 assert end_process is False assert msg == '' # Working -------------------------------------------------------------------- def test_check_terminal_conditions_fail_space(mocker): """Test check if any terminal condition has been met.""" # cmd_args = SimpleNamespace(tablenum=1) free_space = SimpleNamespace(free=0) mock_psutil = mocker.patch('psutil.disk_usage', return_value=free_space) mock_is_file = mocker.patch('os.path.isfile', return_value=False) start_call_count = mock_is_file.call_count end_process, msg = logic_utils.check_terminal_conditions(tablenum=1) assert mock_psutil.call_count == 1 assert mock_is_file.call_count == start_call_count + 1 assert end_process is True assert msg == 'Not enough free space to continue.' # Working -------------------------------------------------------------------- def test_check_terminal_conditions_fail_stopfile(mocker): """Test check if any terminal condition has been met.""" # cmd_args = SimpleNamespace(tablenum=1) free_space = SimpleNamespace(free=99999999999999) tablenum = 1 stop_file_name = 'asset_ingest_{}.stop'.format(tablenum) mock_psutil = mocker.patch('psutil.disk_usage', return_value=free_space) mock_is_file = mocker.patch('os.path.isfile', return_value=True) start_call_count = mock_is_file.call_count end_process, msg = logic_utils.check_terminal_conditions(tablenum) assert mock_psutil.call_count == 1 assert mock_is_file.call_count == start_call_count + 1 assert end_process is True assert stop_file_name in msg # Working -------------------------------------------------------------------- def test_create_donefile_keys_s3(test_get_s3_file_key): """Test the creation of donefile keys (not objects) for S3.""" upc = '1234567898765' relid = '12345' results = logic_utils.create_donefile_keys(relid, upc, use_local=False) assert test_get_s3_file_key.call_count == 4 assert len(results) == 4 # assert relid in results[0] # assert relid in results[1] and asset_paths.completed in results[1] # assert results[0] in results[2] and bucket_name in results[2] # assert relid in results[3] # assert upc in results[4] # def test_create_donefile_keys_local(test_get_s3_file_key): # """Test the creation of donefile keys (not objects) for S3.""" # upc = '1234567898765' # relid = '12345' # # results = logic_utils.create_donefile_keys(relid, upc, use_local=True) # # assert test_get_s3_file_key.call_count == 4 # assert len(results) == 4 # # assert relid in results[0] # # assert relid in results[1] and asset_paths.completed in results[1] # # assert results[0] in results[2] and bucket_name in results[2] # # assert relid in results[3] # # assert upc in results[4] # Working -------------------------------------------------------------------- def test_start_log(mocker): """Test the creation of the applciation log.""" mock_file_open = mocker.patch('builtins.open') log_date = '1-1-1901' path = '/tmp/path' file_name = logic_utils.start_log(table_name, log_date, path, 'audio') assert mock_file_open.call_count == 1 assert file_name == path + '/bulkassetingest_audio_{}-{}_log.csv'.format( table_name, log_date) # Working -------------------------------------------------------------------- def test_save_image_rgb(mocker): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_image = MagicMock(mode='RGB', width=100, height=100) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 0 assert mock_image.save.call_count == 1 assert action == 'converted' assert do_continue is False assert err is None # Working -------------------------------------------------------------------- def test_save_image_cmyk(mocker): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_image = MagicMock(mode='CMYK', width=100, height=100) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image mocker.patch.object(mock_image, 'convert', return_value=mock_image) action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 1 assert mock_image.save.call_count == 1 assert action == 'converted' assert do_continue is False assert err is None # Working -------------------------------------------------------------------- @patch('logic.ripper_feeder_logic.config') def test_save_image_force_resize(mock_config): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_config.FORCE_SQUARE = True mock_image = MagicMock(mode='RGB', width=300, height=300) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 0 assert mock_image.save.call_count == 1 assert mock_image.resize.call_count == 1 assert action == 'converted' assert do_continue is False assert err is None # Working ---------------------------------------------------------------- @patch('logic.ripper_feeder_logic.config') def test_save_image_no_resize(mock_config): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_config.FORCE_SQUARE = True mock_image = MagicMock(mode='RGB', width=3000, height=3000) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 0 assert mock_image.save.call_count == 1 assert mock_image.resize.call_count == 0 assert action == 'converted' assert do_continue is False assert err is None # Working -------------------------------------------------------------------- @patch('logic.ripper_feeder_logic.config') def test_save_image_force_square(mock_config): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_config.FORCE_SQUARE = True mock_image = MagicMock(mode='RGB', width=100, height=90) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 0 assert mock_image.save.call_count == 1 assert mock_image.resize.call_count == 2 assert action == 'converted' assert do_continue is False assert err is None # Working -------------------------------------------------------------------- def test_save_image_exception(): """Test saving an Image using pillow.""" asset_target_file = 'file' mock_image = MagicMock(mode='RGB', width=100, height=100) mock_image.resize.return_value = mock_image mock_image.convert.return_value = mock_image mock_image.save.return_value = mock_image mock_image.save.side_effect = Exception() action, do_continue, err = logic_utils.save_image( asset_target_file, mock_image) assert mock_image.convert.call_count == 0 assert mock_image.save.call_count == 1 assert action == 'failed' assert do_continue is True assert err is not None # Working ---------------------------------------------------------------- def test_open_image(test_image): """Test opening an image with pillow.""" tmp_target_path = 'file' image, do_continue, action, err = logic_utils.open_image(tmp_target_path) assert test_image.call_count == 1 assert type(image) is not None assert do_continue is False assert action == 'opened' assert err is None # Working ---------------------------------------------------------------- def test_open_image_exception(test_image_exception): """Test opening an image with pillow.""" tmp_target_path = 'file' image, do_continue, action, err = logic_utils.open_image(tmp_target_path) assert test_image_exception.call_count == 1 assert image is None assert do_continue is True assert action == 'failed' assert err is not None # Working ---------------------------------------------------------------- def test_copy_from_local(mocker): """Test copying file from local tmp folder to target path.""" # Ensure asset target exists mock_os = mocker.patch('os.makedirs') # This copy_file() uses a higher buffer for speed mock_copy = mocker.patch( 'util.logic_utils.copy_file') logic_utils.copy_from_local('/tmp/path_source', '/tmp/path_target') assert mock_os.call_count == 1 assert mock_copy.call_count == 1