(c: Composition)
| 242 | |
| 243 | |
| 244 | def workflow_copy_to_s3(c: Composition) -> None: |
| 245 | with c.override( |
| 246 | Materialized( |
| 247 | depends_on=["localstack"], |
| 248 | environment_extra=[ |
| 249 | f"AWS_ENDPOINT_URL={AWS_ENDPOINT_URL_MZ}", |
| 250 | f"AWS_ACCESS_KEY_ID={AWS_ACCESS_KEY_ID}", |
| 251 | f"AWS_SECRET_ACCESS_KEY={AWS_SECRET_ACCESS_KEY}", |
| 252 | ], |
| 253 | ) |
| 254 | ): |
| 255 | c.up("localstack", "materialized") |
| 256 | localhost_aws_endpoint_url = f"http://localhost:{c.port('localstack', 4566)}" |
| 257 | s3_client = boto3.client( |
| 258 | "s3", |
| 259 | endpoint_url=localhost_aws_endpoint_url, |
| 260 | region_name=DEFAULT_CLOUD_REGION, |
| 261 | aws_access_key_id=AWS_ACCESS_KEY_ID, |
| 262 | aws_secret_access_key=AWS_SECRET_ACCESS_KEY, |
| 263 | ) |
| 264 | bucket_name = "copy-to-s3" |
| 265 | s3_client.create_bucket(Bucket=bucket_name) |
| 266 | path_prefix = str(uuid.uuid4()) |
| 267 | c.run_testdrive_files( |
| 268 | f"--var=endpoint={AWS_ENDPOINT_URL_MZ}", |
| 269 | f"--var=access-key={AWS_ACCESS_KEY_ID}", |
| 270 | f"--var=secret-key={AWS_SECRET_ACCESS_KEY}", |
| 271 | f"--var=s3-prefix={bucket_name}/{path_prefix}", |
| 272 | f"--var=region={DEFAULT_CLOUD_REGION}", |
| 273 | "--default-timeout=300s", |
| 274 | "copy-to-s3/copy-to-s3.td", |
| 275 | ) |
| 276 | |
| 277 | def validate_upload(upload, expected_output_set): |
| 278 | assert len(upload["Contents"]) > 0 |
| 279 | output_lines = set() |
| 280 | for obj in upload["Contents"]: |
| 281 | assert obj["Key"].endswith(".csv") |
| 282 | key = obj["Key"] |
| 283 | object_response = s3_client.get_object(Bucket=bucket_name, Key=key) |
| 284 | body = object_response["Body"].read().decode("utf-8") |
| 285 | output_lines.update(body.splitlines()) |
| 286 | assert output_lines == expected_output_set |
| 287 | |
| 288 | # asserting the uploaded files |
| 289 | date = c.sql_query("SELECT TO_CHAR(now(), 'YYYY-MM-DD')")[0][0] |
| 290 | expected_output = set(map(lambda x: str(x), range(10))) |
| 291 | first_upload = s3_client.list_objects_v2( |
| 292 | Bucket=bucket_name, Prefix=f"{path_prefix}/1/{date}/" |
| 293 | ) |
| 294 | validate_upload(first_upload, expected_output) |
| 295 | |
| 296 | second_upload = s3_client.list_objects_v2( |
| 297 | Bucket=bucket_name, Prefix=f"{path_prefix}/2/" |
| 298 | ) |
| 299 | validate_upload(second_upload, expected_output) |
| 300 | |
| 301 | third_upload = s3_client.list_objects_v2( |
nothing calls this directly
no test coverage detected