Skip to content

Commit

Permalink
chore: add unit tests (#63)
Browse files Browse the repository at this point in the history
Closes #50
Closes #68
  • Loading branch information
Jrmyy authored Nov 28, 2022
1 parent f31a863 commit 46c511d
Show file tree
Hide file tree
Showing 16 changed files with 1,051 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ name: ci
on:
pull_request:
push:
branches: [ main ]

jobs:
pre-commit:
Expand All @@ -14,3 +15,22 @@ jobs:
with:
python-version: '3.x'
- uses: pre-commit/[email protected]

unit_test:
name: 'Unit tests with coverage'
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
make install_deps
- name: Test with pytest
run: |
make unit_test
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,14 @@ setup: ## Install all dependencies and setup pre-commit
.env: ## Generate .env file
@cp .env.example $@

test: .env ## Run tests.
test: ## Run tests.
make functional_test
make unit_test

unit_test: ## Run unit tests.
pytest --cov=dbt tests/unit

functional_test: .env ## Run functional tests.
pytest tests/functional

pre-commit: ## check modified and added files (compared to last commit!) with pre-commit.
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ Next, adjust `.env` file by configuring the environment variables to match your
You must have an AWS account with Athena setup in order to launch the tests. You can run the tests using `make`:

```bash
make run_tests
make test
```

### Helpful Resources
Expand Down
6 changes: 4 additions & 2 deletions dbt/adapters/athena/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def convert_datetime_type(cls, agate_table: agate.Table, col_idx: int) -> str:
return "timestamp"

@available
def s3_table_prefix(self, s3_data_dir: str) -> str:
def s3_table_prefix(self, s3_data_dir: Optional[str]) -> str:
"""
Returns the root location for storing tables in S3.
This is `s3_data_dir`, if set, and `s3_staging_dir/tables/` if not.
Expand All @@ -62,7 +62,9 @@ def s3_table_prefix(self, s3_data_dir: str) -> str:
return path.join(creds.s3_staging_dir, "tables")

@available
def s3_table_location(self, s3_data_dir: str, s3_data_naming: str, schema_name: str, table_name: str) -> str:
def s3_table_location(
self, s3_data_dir: Optional[str], s3_data_naming: str, schema_name: str, table_name: str
) -> str:
"""
Returns either a UUID or database/table prefix for storing a table,
depending on the value of s3_table
Expand Down
10 changes: 5 additions & 5 deletions dbt/adapters/athena/query_headers.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ def add(self, sql: str) -> str:
if not self.query_comment:
return sql

# alter statements or vacuum statements seems not to support properly query comments
# let's just exclude them
if any(map(sql.lower().__contains__, ["alter", "vacuum"])):
return sql

cleaned_query_comment = self.query_comment.strip().replace("\n", " ")

if self.append:
Expand All @@ -25,9 +30,4 @@ def add(self, sql: str) -> str:

return f"{sql}\n-- /* {cleaned_query_comment} */"

# alter statements or vacuum statements seems not to support properly query comments
# let's just exclude them
if "alter" or "vacuum" in sql.lower():
return sql

return f"-- /* {cleaned_query_comment} */\n{sql}"
2 changes: 2 additions & 0 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ dbt-tests-adapter~=1.3.0
flake8~=5.0
Flake8-pyproject~=1.1
isort~=5.10
moto~=4.0
pre-commit~=2.20
pytest~=7.2
pytest-cov~=4.0
pyupgrade~=3.2
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ def dbt_profile_target():
"threads": 1,
"num_retries": 0,
"work_group": os.getenv("DBT_TEST_ATHENA_WORK_GROUND"),
"aws_profile_name": os.getenv("DBT_TEST_ATHENA_AWS_PROFILE_NAME"),
"aws_profile_name": os.getenv("DBT_TEST_ATHENA_AWS_PROFILE_NAME") or None,
}
Empty file added tests/unit/__init__.py
Empty file.
5 changes: 5 additions & 0 deletions tests/unit/constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CATALOG_ID = "catalog_id"
DATA_CATALOG_NAME = "awsdatacatalog"
DATABASE_NAME = "test_dbt_athena"
BUCKET = "test-dbt-athena-test-delete-partitions"
AWS_REGION = "eu-west-1"
Loading

0 comments on commit 46c511d

Please sign in to comment.