I have a serverless python code that uses AWS S3, DuckDB API.
The code works fine, the point is the unit tests of this code.
I am using unittest and moto python frameworks in order to test this code.
The issue is to configure duckdb connection to point on the mocked s3 bucket.
This is a code snippet of duckdb configurations:
self.db_conn.execute(query=f"SET s3_region='{os.environ['AWS_REGION']}';")
self.db_conn.execute(query=f"SET s3_access_key_id='{self.tenant_ctx_aws_credentials['Credentials']['AccessKeyId']}';")
self.db_conn.execute(query=f"SET s3_secret_access_key='{self.tenant_ctx_aws_credentials['Credentials']['SecretAccessKey']}';")
self.db_conn.execute(query=f"SET s3_session_token='{self.tenant_ctx_aws_credentials['Credentials']['SessionToken']}';")
self.db_conn.execute(query=f"SET memory_limit='{self.memory_limit}';")
And this is from unit test file of the lambda function that uses moto in order to mock AWS services:
@mock_s3
@mock_glue
@mock_ssm
@mock_sts
@mock_iam
@mock.patch.dict(os.environ)
class TestLambdaFunction(unittest.TestCase):
maxDiff = None
def setUp(self):
try:
# S3 setup:
self.s3_client = boto3.client('s3', region_name=self.aws_region)
self.s3_resource = boto3.resource('s3', region_name=self.aws_region)
self.s3_bucket = self.s3_resource.create_bucket(Bucket=self.dp_s3_bucket_name, CreateBucketConfiguration={
'LocationConstraint': self.aws_region})
So I am trying that this mocked s3 bucket will be accessed by duckdb.
When I run this test I receive the following error:
IO Error: HTTP GET error on 'https://{test_bucket_name}.s3.amazonaws.com/{s3_path}/test_file.parquet' (HTTP 400)
I tried to work with endpoint_url argument of boto3 client but it did not help.
Moreover, as far as I know when moto runs, it runs on localhost:5000 so I also tried to point duckdb configs to this doamin:port but it also did not worked for me.
This is a
pytest
solution that works withmoto[server]
. It's not yet available in https://github.com/dazza-codes/pytest-aiomoto but I would welcome a PR on that if I don't get to adding support forduckdb
in that project (it's not actively maintained).(Caveat, lib imports are needed for ^^)
The
MotoService
is from https://github.com/dazza-codes/pytest-aiomotomoto[server]
.Also use
SET s3_url_style='path';
forduckdb
.