Skip to content

Commit 46c511d

Browse files
authored
chore: add unit tests (#63)
Closes #50 Closes #68
1 parent f31a863 commit 46c511d

16 files changed

+1051
-10
lines changed

.github/workflows/ci.yml

+20
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ name: ci
33
on:
44
pull_request:
55
push:
6+
branches: [ main ]
67

78
jobs:
89
pre-commit:
@@ -14,3 +15,22 @@ jobs:
1415
with:
1516
python-version: '3.x'
1617
- uses: pre-commit/[email protected]
18+
19+
unit_test:
20+
name: 'Unit tests with coverage'
21+
runs-on: ubuntu-latest
22+
strategy:
23+
matrix:
24+
python-version: [ "3.7", "3.8", "3.9", "3.10" ]
25+
steps:
26+
- uses: actions/checkout@v3
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@v4
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
- name: Install dependencies
32+
run: |
33+
make install_deps
34+
- name: Test with pytest
35+
run: |
36+
make unit_test

Makefile

+8-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,14 @@ setup: ## Install all dependencies and setup pre-commit
1919
.env: ## Generate .env file
2020
@cp .env.example $@
2121

22-
test: .env ## Run tests.
22+
test: ## Run tests.
23+
make functional_test
24+
make unit_test
25+
26+
unit_test: ## Run unit tests.
27+
pytest --cov=dbt tests/unit
28+
29+
functional_test: .env ## Run functional tests.
2330
pytest tests/functional
2431

2532
pre-commit: ## check modified and added files (compared to last commit!) with pre-commit.

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ Next, adjust `.env` file by configuring the environment variables to match your
232232
You must have an AWS account with Athena setup in order to launch the tests. You can run the tests using `make`:
233233

234234
```bash
235-
make run_tests
235+
make test
236236
```
237237

238238
### Helpful Resources

dbt/adapters/athena/impl.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def convert_datetime_type(cls, agate_table: agate.Table, col_idx: int) -> str:
4747
return "timestamp"
4848

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

6464
@available
65-
def s3_table_location(self, s3_data_dir: str, s3_data_naming: str, schema_name: str, table_name: str) -> str:
65+
def s3_table_location(
66+
self, s3_data_dir: Optional[str], s3_data_naming: str, schema_name: str, table_name: str
67+
) -> str:
6668
"""
6769
Returns either a UUID or database/table prefix for storing a table,
6870
depending on the value of s3_table

dbt/adapters/athena/query_headers.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ def add(self, sql: str) -> str:
1414
if not self.query_comment:
1515
return sql
1616

17+
# alter statements or vacuum statements seems not to support properly query comments
18+
# let's just exclude them
19+
if any(map(sql.lower().__contains__, ["alter", "vacuum"])):
20+
return sql
21+
1722
cleaned_query_comment = self.query_comment.strip().replace("\n", " ")
1823

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

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

28-
# alter statements or vacuum statements seems not to support properly query comments
29-
# let's just exclude them
30-
if "alter" or "vacuum" in sql.lower():
31-
return sql
32-
3333
return f"-- /* {cleaned_query_comment} */\n{sql}"

dev-requirements.txt

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ dbt-tests-adapter~=1.3.0
44
flake8~=5.0
55
Flake8-pyproject~=1.1
66
isort~=5.10
7+
moto~=4.0
78
pre-commit~=2.20
89
pytest~=7.2
10+
pytest-cov~=4.0
911
pyupgrade~=3.2

tests/conftest.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ def dbt_profile_target():
2020
"threads": 1,
2121
"num_retries": 0,
2222
"work_group": os.getenv("DBT_TEST_ATHENA_WORK_GROUND"),
23-
"aws_profile_name": os.getenv("DBT_TEST_ATHENA_AWS_PROFILE_NAME"),
23+
"aws_profile_name": os.getenv("DBT_TEST_ATHENA_AWS_PROFILE_NAME") or None,
2424
}

tests/unit/__init__.py

Whitespace-only changes.

tests/unit/constants.py

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CATALOG_ID = "catalog_id"
2+
DATA_CATALOG_NAME = "awsdatacatalog"
3+
DATABASE_NAME = "test_dbt_athena"
4+
BUCKET = "test-dbt-athena-test-delete-partitions"
5+
AWS_REGION = "eu-west-1"

0 commit comments

Comments
 (0)