"""Unit tests for helper functions for workflows.""" import boto3 from moto import mock_aws import pytest from feed_ingestion.flows import helpers @pytest.fixture def s3_client(): with mock_aws(): yield boto3.client('s3') @pytest.fixture def s3_bucket(s3_client): bucket = 'abucket' s3_client.create_bucket(Bucket=bucket) yield bucket @pytest.fixture def s3_key(s3_client, s3_bucket): key = 'path/file' s3_client.put_object( Body='12345678', Bucket=s3_bucket, Key=key) yield key def test_get_file_size(s3_bucket, s3_key): """Test get filesize.""" result = helpers.get_filesize(s3_bucket, s3_key, '437795906767') assert result == 8 def test_check_s3_key_exist(s3_key, s3_bucket): """Test check_s3_key_exist.""" s3_url = f's3://{s3_bucket}/{s3_key}' resp = helpers.check_s3_key_exist(s3_url, '437795906767') assert resp is True s3_url = f'{s3_url}.notexist' resp = helpers.check_s3_key_exist(s3_url, '437795906767') assert resp is False def test_download(s3_key, s3_bucket, tmp_path): """Test download helper (from S3).""" s3_url = f's3://{s3_bucket}/{s3_key}' file_path = tmp_path / 'file' helpers.download(s3_url, str(file_path)) assert file_path.exists() assert file_path.read_text() == '12345678' def test_upload_raw_file_to_s3(s3_client, s3_bucket, tmp_path): """Test upload_raw_file_to_s3 helper.""" local_file_path = tmp_path / 'test_file' local_file_path.write_text('1234567890') path = 'destination_s3_path' s3_url = f's3://{s3_bucket}/{path}' helpers.upload_raw_file_to_s3(str(local_file_path), s3_url) assert s3_client.get_object( Bucket=s3_bucket, Key=path)['Body'].read() == b'1234567890' def test_decode_snowflake_key(): """Test decode_snowflake_key.""" key = 'a2V5' result = helpers.decode_snowflake_key(key) assert result == b'key'