import unittest from unittest.mock import Mock import boto3 import smart_open from bulk_copy_task import App from bulk_copy_task.handler import process_file from moto import mock_s3 @mock_s3 class HandlerTestCase(unittest.TestCase): def setUp(self): boto3.setup_default_session() self.conn = boto3.resource('s3', region_name='us-east-1') self.source_bucket = 'dev-test-source-bucket' self.dest_bucket = 'dev-test-dest-bucket' self.source_key = 'test_folder/initial_test_file.txt' self.conn.create_bucket(Bucket=self.source_bucket) self.conn.create_bucket(Bucket=self.dest_bucket) expected = 'Some bytes data' self.data = bytes(expected, 'utf-8') self.s3_path = f's3://{self.source_bucket}/initial_test_file.txt' with smart_open.open(f's3://{self.source_bucket}/{self.source_key}', 'wb') as fout: fout.write(self.data) def test_copy_file_ok(self): config = App( bucket_source=self.source_bucket, s3_keys=[self.source_key, ], bucket_destination=self.dest_bucket, ) is_ok, ctx = process_file(Mock(), config, self.source_key) self.assertTrue(is_ok) copied = self.conn.Object(self.dest_bucket, self.source_key).get()['Body'].read() self.assertEqual(self.data, copied)