from unittest import mock import pytest from botocore.exceptions import ClientError from slz_downloader.dsp.entities import S3Path from slz_downloader.s3_service import S3Service ARCHIVE_BUCKET_TEST = 'bucket-archive_test' DECOMPRESSED_BUCKET_TEST = 'bucket-decompressed_test' @pytest.fixture def s3_service(s3_client) -> S3Service: yield S3Service(mock.Mock(), s3_client) def test_copy(s3_client, s3_service): s3_client.put_object( Body=b'data', Bucket=ARCHIVE_BUCKET_TEST, Key='apple/2019-01-02/report.txt.gz' ) source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') s3_service.copy(source, destination) response = s3_client.get_object(Bucket=source.bucket, Key=source.key) assert b'data' == response['Body'].read() response = s3_client.get_object(Bucket=destination.bucket, Key=destination.key) assert b'data' == response['Body'].read() def test_copy_source_not_exist_silent(s3_service): source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') s3_service.copy(source, destination) def test_copy_source_not_exist_raise_exc(s3_service): source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') with pytest.raises(ClientError): s3_service.copy(source, destination, raise_exc=True) def test_copy_source_to_itself(s3_client, s3_service): s3_client.put_object( Body=b'data', Bucket=ARCHIVE_BUCKET_TEST, Key='apple/2019-01-02/report.txt.gz' ) source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') s3_service.copy(source, source) response = s3_client.get_object(Bucket=source.bucket, Key=source.key) assert b'data' == response['Body'].read() def test_move(s3_client, s3_service): s3_client.put_object( Body=b'data', Bucket=ARCHIVE_BUCKET_TEST, Key='apple/2019-01-02/report.txt.gz' ) source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') s3_service.move(source, destination) with pytest.raises(ClientError): s3_client.get_object( Bucket=source.bucket, Key=source.key, ) response = s3_client.get_object(Bucket=destination.bucket, Key=destination.key) assert b'data' == response['Body'].read() def test_move_source_not_exist_silent(s3_service): source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') s3_service.move(source, destination) def test_move_source_not_exist_raise_exc(s3_service): source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') destination = S3Path(DECOMPRESSED_BUCKET_TEST, 'apple/2019-01-02', 'report.txt') with pytest.raises(ClientError): s3_service.move(source, destination, raise_exc=True) def test_move_source_to_itself(s3_client, s3_service): s3_client.put_object( Body=b'data', Bucket=ARCHIVE_BUCKET_TEST, Key='apple/2019-01-02/report.txt.gz' ) source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') s3_service.move(source, source) response = s3_client.get_object(Bucket=source.bucket, Key=source.key) assert b'data' == response['Body'].read() def test_delete(s3_client, s3_service): s3_client.put_object( Body=b'data', Bucket=ARCHIVE_BUCKET_TEST, Key='apple/2019-01-02/report.txt.gz' ) source = S3Path(ARCHIVE_BUCKET_TEST, 'apple/2019-01-02', 'report.txt.gz') response = s3_client.get_object(Bucket=source.bucket, Key=source.key) assert b'data' == response['Body'].read() s3_service.delete(source) with pytest.raises(ClientError): s3_client.get_object( Bucket=source.bucket, Key=source.key, )