Skip to content

Commit d4bfa3d

Browse files
committed
fix(s3): Update tests and list_keys calls to handle full path key
1 parent 7b90dad commit d4bfa3d

File tree

2 files changed

+19
-25
lines changed

2 files changed

+19
-25
lines changed

airflow_dbt_python/hooks/backends/s3.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def push_many(
121121
all_files = Path(source).glob("**/*")
122122

123123
if delete_before:
124-
keys = self.hook.list_keys(bucket_name, destination)
124+
keys = self.hook.list_keys(bucket_name)
125125
self.hook.delete_objects(bucket_name, keys)
126126

127127
if key.endswith(".zip"):

tests/hooks/dbt/backends/test_dbt_s3_backend.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -294,10 +294,7 @@ def test_push_dbt_project_to_files(s3_bucket, s3_hook, tmpdir, test_files):
294294
backend = DbtS3Backend()
295295
backend.push_dbt_project(test_files[0].parent.parent, prefix)
296296

297-
keys = s3_hook.list_keys(
298-
s3_bucket,
299-
prefix,
300-
)
297+
keys = s3_hook.list_keys(bucket_name=s3_bucket)
301298
assert len(keys) == 4
302299

303300

@@ -323,7 +320,7 @@ def test_push_dbt_project_with_no_replace(s3_bucket, s3_hook, tmpdir, test_files
323320
with open(_file) as f:
324321
file_content = f.read()
325322

326-
key = f"s3://{s3_bucket}/project/{_file.relative_to(project_dir)}"
323+
key = f"project/{_file.relative_to(project_dir)}"
327324
bucket.put_object(Key=key, Body=file_content.encode())
328325
obj = s3_hook.get_key(
329326
key,
@@ -339,10 +336,7 @@ def test_push_dbt_project_with_no_replace(s3_bucket, s3_hook, tmpdir, test_files
339336
project_dir, f"s3://{s3_bucket}/project/", replace=False
340337
)
341338

342-
keys = s3_hook.list_keys(
343-
s3_bucket,
344-
f"s3://{s3_bucket}/project/",
345-
)
339+
keys = s3_hook.list_keys(bucket_name=s3_bucket)
346340
assert len(keys) == 4, keys
347341

348342
last_modified_result = {}
@@ -376,7 +370,7 @@ def test_push_dbt_project_with_partial_replace(s3_bucket, s3_hook, tmpdir, test_
376370
with open(_file) as f:
377371
file_content = f.read()
378372

379-
key = f"s3://{s3_bucket}/project/{_file.relative_to(project_dir)}"
373+
key = f"project/{_file.relative_to(project_dir)}"
380374
bucket.put_object(Key=key, Body=file_content.encode())
381375
obj = s3_hook.get_key(
382376
key,
@@ -399,10 +393,7 @@ def test_push_dbt_project_with_partial_replace(s3_bucket, s3_hook, tmpdir, test_
399393
project_dir, f"s3://{s3_bucket}/project/", replace=False
400394
)
401395

402-
keys = s3_hook.list_keys(
403-
s3_bucket,
404-
f"s3://{s3_bucket}/project/",
405-
)
396+
keys = s3_hook.list_keys(bucket_name=s3_bucket)
406397
assert len(keys) == 4
407398

408399
last_modified_result = {}
@@ -433,7 +424,7 @@ def test_push_dbt_project_with_delete_before(s3_bucket, s3_hook, tmpdir, test_fi
433424

434425
with freezegun.freeze_time("2022-01-01"):
435426
# delete_before = True should delete this random file not part of the project
436-
bucket.put_object(Key=f"{prefix}file_to_be_deleted", Body="content".encode())
427+
bucket.put_object(Key=f"project/file_to_be_deleted", Body="content".encode())
437428

438429
for _file in project_dir.glob("**/*"):
439430
if _file.is_dir():
@@ -442,29 +433,23 @@ def test_push_dbt_project_with_delete_before(s3_bucket, s3_hook, tmpdir, test_fi
442433
with open(_file) as f:
443434
file_content = f.read()
444435

445-
key = f"s3://{s3_bucket}/project/{_file.relative_to(project_dir)}"
436+
key = f"project/{_file.relative_to(project_dir)}"
446437
bucket.put_object(Key=key, Body=file_content.encode())
447438
obj = s3_hook.get_key(
448439
key,
449440
s3_bucket,
450441
)
451442
last_modified_expected[key] = obj.last_modified
452443

453-
keys = s3_hook.list_keys(
454-
s3_bucket,
455-
prefix,
456-
)
444+
keys = s3_hook.list_keys(bucket_name=s3_bucket)
457445
assert len(keys) == 5
458446

459447
backend = DbtS3Backend()
460448
with freezegun.freeze_time("2022-02-02"):
461449
# Try to push the same files, a month after.
462450
backend.push_dbt_project(project_dir, prefix, delete_before=True)
463451

464-
keys = s3_hook.list_keys(
465-
s3_bucket,
466-
prefix,
467-
)
452+
keys = s3_hook.list_keys(bucket_name=s3_bucket)
468453
assert len(keys) == 4, keys
469454

470455
last_modified_result = {}
@@ -484,6 +469,15 @@ class FakeHook:
484469
def load_file(*args, **kwargs):
485470
raise ValueError()
486471

472+
def parse_s3_url(self, key):
473+
try:
474+
from airflow.providers.amazon.aws.hooks.s3 import S3Hook
475+
except ImportError:
476+
from airflow.hooks.S3_hook import S3Hook
477+
478+
hook = S3Hook()
479+
return hook.parse_s3_url(key)
480+
487481

488482
def test_load_file_handle_replace_error_returns_false_on_valueerror():
489483
"""Test function returns False when underlying hook raises ValueError.

0 commit comments

Comments
 (0)