Skip to content

Commit

Permalink
feat(test): Clean mocked S3 bucket after every use
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasfarias committed Jan 22, 2022
1 parent 4ba0fac commit 63984b7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
32 changes: 29 additions & 3 deletions tests/conftest.py
Expand Up @@ -9,6 +9,7 @@
from pytest_postgresql.janitor import DatabaseJanitor

from airflow_dbt_python.hooks.dbt import DbtHook
from airflow_dbt_python.hooks.s3 import DbtS3Hook

PROFILES = """
default:
Expand Down Expand Up @@ -234,11 +235,36 @@ def mocked_s3_res():


@pytest.fixture
def s3_bucket(mocked_s3_res):
"""Return a mocked s3 bucket for testing."""
def s3_hook():
"""Provide a DbtHook."""
return DbtS3Hook()


@pytest.fixture
def s3_bucket(mocked_s3_res, s3_hook):
"""Return a mocked s3 bucket for testing.
Bucket is cleaned after every use.
"""
bucket = "airflow-dbt-test-s3-bucket"
mocked_s3_res.create_bucket(Bucket=bucket)
return bucket

yield bucket

keys = s3_hook.list_keys(
bucket,
"",
)
if keys is not None and len(keys) > 0:
s3_hook.delete_objects(
bucket,
keys,
)
keys = s3_hook.list_keys(
bucket,
"",
)
assert keys is None or len(keys) == 0


BROKEN_SQL = """
Expand Down
21 changes: 0 additions & 21 deletions tests/hooks/s3/test_dbt_s3_hook.py
Expand Up @@ -311,29 +311,10 @@ def test_push_dbt_project_to_zip_file(s3_bucket, tmpdir, test_files):
assert key is True


def clean_s3_prefix(hook: DbtS3Hook, prefix: str, s3_bucket: str):
"""Ensure we are working with an empty S3 prefix."""
keys = hook.list_keys(
s3_bucket,
prefix,
)
if keys is not None and len(keys) > 0:
hook.delete_objects(
s3_bucket,
keys,
)
keys = hook.list_keys(
s3_bucket,
prefix,
)
assert keys is None or len(keys) == 0


def test_push_dbt_project_to_files(s3_bucket, tmpdir, test_files):
"""Test pushing a dbt project to a S3 path."""
hook = DbtS3Hook()
prefix = f"s3://{s3_bucket}/project/"
clean_s3_prefix(hook, prefix, s3_bucket)

hook.push_dbt_project(f"s3://{s3_bucket}/project/", test_files[0].parent.parent)
keys = hook.list_keys(
Expand All @@ -352,7 +333,6 @@ def test_push_dbt_project_with_no_replace(s3_bucket, tmpdir, test_files):

hook = DbtS3Hook()
prefix = f"s3://{s3_bucket}/project/"
clean_s3_prefix(hook, prefix, s3_bucket)
bucket = hook.get_bucket(s3_bucket)

last_modified_expected = {}
Expand Down Expand Up @@ -408,7 +388,6 @@ def test_push_dbt_project_with_partial_replace(s3_bucket, tmpdir, test_files):

hook = DbtS3Hook()
prefix = f"s3://{s3_bucket}/project/"
clean_s3_prefix(hook, prefix, s3_bucket)
bucket = hook.get_bucket(s3_bucket)

last_modified_expected = {}
Expand Down

0 comments on commit 63984b7

Please sign in to comment.