-
Notifications
You must be signed in to change notification settings - Fork 515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Experiment with functional testing #575
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,6 @@ dbt_modules/ | |
dbt_packages/ | ||
logs/ | ||
venv/ | ||
env/ | ||
test.env | ||
__pycache__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pytest | ||
pytest-dotenv | ||
dbt-tests-adapter | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[pytest] | ||
filterwarnings = | ||
ignore:.*'soft_unicode' has been renamed to 'soft_str'*:DeprecationWarning | ||
ignore:unclosed file .*:ResourceWarning | ||
env_files = | ||
test.env | ||
testpaths = | ||
tests/functional |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/bin/bash | ||
VENV="venv/bin/activate" | ||
|
||
if [[ ! -f $VENV ]]; then | ||
python3 -m venv venv | ||
. $VENV | ||
|
||
pip install --upgrade pip setuptools | ||
pip install --pre "dbt-$1" -r dev-requirements.txt | ||
fi | ||
|
||
. $VENV | ||
python3 -m pytest tests/functional --profile $1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once all the setup is sorted, this is really it:
|
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import pytest | ||
import os | ||
|
||
pytest_plugins = ["dbt.tests.fixtures.project"] | ||
|
||
|
||
def pytest_addoption(parser): | ||
parser.addoption("--profile", action="store", default="apache_spark", type=str) | ||
|
||
|
||
# Using @pytest.mark.skip_adapter('apache_spark') uses the 'skip_by_adapter_type' | ||
# autouse fixture below | ||
def pytest_configure(config): | ||
config.addinivalue_line( | ||
"markers", | ||
"skip_profile(profile): skip test for the given profile", | ||
) | ||
config.addinivalue_line( | ||
"markers", | ||
"only_profile(profile): only test the given profile", | ||
) | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def dbt_profile_target(request): | ||
profile_type = request.config.getoption("--profile") | ||
if profile_type == "postgres": | ||
target = postgres_target() | ||
elif profile_type == "redshift": | ||
target = redshift_target() | ||
elif profile_type == "snowflake": | ||
target = snowflake_target() | ||
elif profile_type == "bigquery": | ||
target = bigquery_target() | ||
else: | ||
raise ValueError(f"Invalid profile type '{profile_type}'") | ||
return target | ||
|
||
|
||
def postgres_target(): | ||
return { | ||
"type": "postgres", | ||
"host": os.getenv('POSTGRES_TEST_HOST'), | ||
"user": os.getenv('POSTGRES_TEST_USER'), | ||
"pass": os.getenv('POSTGRES_TEST_PASS'), | ||
"port": int(os.getenv('POSTGRES_TEST_PORT')), | ||
"dbname": os.getenv('POSTGRES_TEST_DBNAME'), | ||
} | ||
|
||
|
||
def redshift_target(): | ||
return { | ||
"type": "redshift", | ||
"host": os.getenv('REDSHIFT_TEST_HOST'), | ||
"user": os.getenv('REDSHIFT_TEST_USER'), | ||
"pass": os.getenv('REDSHIFT_TEST_PASS'), | ||
"port": int(os.getenv('REDSHIFT_TEST_PORT')), | ||
"dbname": os.getenv('REDSHIFT_TEST_DBNAME'), | ||
} | ||
|
||
|
||
def bigquery_target(): | ||
return { | ||
"type": "bigquery", | ||
"method": "service-account", | ||
"keyfile": os.getenv('BIGQUERY_SERVICE_KEY_PATH'), | ||
"project": os.getenv('BIGQUERY_TEST_DATABASE'), | ||
} | ||
|
||
|
||
def snowflake_target(): | ||
return { | ||
"type": "snowflake", | ||
"account": os.getenv('SNOWFLAKE_TEST_ACCOUNT'), | ||
"user": os.getenv('SNOWFLAKE_TEST_USER'), | ||
"password": os.getenv('SNOWFLAKE_TEST_PASSWORD'), | ||
"role": os.getenv('SNOWFLAKE_TEST_ROLE'), | ||
"database": os.getenv('SNOWFLAKE_TEST_DATABASE'), | ||
"warehouse": os.getenv('SNOWFLAKE_TEST_WAREHOUSE'), | ||
} | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def skip_by_profile_type(request): | ||
profile_type = request.config.getoption("--profile") | ||
if request.node.get_closest_marker("skip_profile"): | ||
for skip_profile_type in request.node.get_closest_marker("skip_profile").args: | ||
if skip_profile_type == profile_type: | ||
pytest.skip("skipped on '{profile_type}' profile") | ||
|
||
|
||
@pytest.fixture(autouse=True) | ||
def only_profile_type(request): | ||
profile_type = request.config.getoption("--profile") | ||
if request.node.get_closest_marker("only_profile"): | ||
for only_profile_type in request.node.get_closest_marker("only_profile").args: | ||
if only_profile_type != profile_type: | ||
pytest.skip("skipped on '{profile_type}' profile") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import os | ||
import pytest | ||
from dbt.tests.util import run_dbt | ||
from tests.functional.cross_db_utils.fixture_cross_db_macro import ( | ||
macros__test_assert_equal_sql, | ||
) | ||
|
||
|
||
class BaseCrossDbMacro: | ||
# install this repo as a package! | ||
@pytest.fixture(scope="class") | ||
def packages(self): | ||
return {"packages": [{"local": os.getcwd()}]} | ||
|
||
# setup | ||
@pytest.fixture(scope="class") | ||
def macros(self): | ||
return {"test_assert_equal.sql": macros__test_assert_equal_sql} | ||
|
||
# each child class will reimplement 'models' + 'seeds' | ||
def seeds(self): | ||
return {} | ||
|
||
def models(self): | ||
return {} | ||
|
||
# actual test sequence | ||
def test_build_assert_equal(self, project): | ||
run_dbt(['deps']) | ||
run_dbt(['build']) # seed, model, test -- all handled by dbt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
|
||
# any_value | ||
|
||
seeds__data_any_value_csv = """id,key_name,static_col | ||
1,abc,dbt | ||
2,abc,dbt | ||
3,jkl,dbt | ||
4,jkl,dbt | ||
5,jkl,dbt | ||
6,xyz,test | ||
""" | ||
|
||
|
||
seeds__data_any_value_expected_csv = """key_name,static_col,num_rows | ||
abc,dbt,2 | ||
jkl,dbt,3 | ||
xyz,test,1 | ||
""" | ||
|
||
|
||
models__test_any_value_sql = """ | ||
with data as ( | ||
|
||
select * from {{ ref('data_any_value') }} | ||
|
||
), | ||
|
||
data_output as ( | ||
|
||
select * from {{ ref('data_any_value_expected') }} | ||
|
||
), | ||
|
||
calculate as ( | ||
select | ||
key_name, | ||
{{ dbt_utils.any_value('static_col') }} as static_col, | ||
count(id) as num_rows | ||
from data | ||
group by key_name | ||
) | ||
|
||
select | ||
calculate.num_rows as actual, | ||
data_output.num_rows as expected | ||
from calculate | ||
left join data_output | ||
on calculate.key_name = data_output.key_name | ||
and calculate.static_col = data_output.static_col | ||
""" | ||
|
||
|
||
models__test_any_value_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_any_value | ||
tests: | ||
- assert_equal: | ||
actual: actual | ||
expected: expected | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
|
||
# bool_or | ||
|
||
seeds__data_bool_or_csv = """key,val1,val2 | ||
abc,1,1 | ||
abc,1,0 | ||
def,1,0 | ||
hij,1,1 | ||
hij,1, | ||
klm,1,0 | ||
klm,1, | ||
""" | ||
|
||
|
||
seeds__data_bool_or_expected_csv = """key,value | ||
abc,true | ||
def,false | ||
hij,true | ||
klm,false | ||
""" | ||
|
||
|
||
models__test_bool_or_sql = """ | ||
with data as ( | ||
|
||
select * from {{ ref('data_bool_or') }} | ||
|
||
), | ||
|
||
data_output as ( | ||
|
||
select * from {{ ref('data_bool_or_expected') }} | ||
|
||
), | ||
|
||
calculate as ( | ||
|
||
select | ||
key, | ||
{{ dbt_utils.bool_or('val1 = val2') }} as value | ||
from data | ||
group by key | ||
|
||
) | ||
|
||
select | ||
calculate.value as actual, | ||
data_output.value as expected | ||
from calculate | ||
left join data_output | ||
on calculate.key = data_output.key | ||
""" | ||
|
||
|
||
models__test_bool_or_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_bool_or | ||
tests: | ||
- assert_equal: | ||
actual: actual | ||
expected: expected | ||
""" |
30 changes: 30 additions & 0 deletions
30
tests/functional/cross_db_utils/fixture_cast_bool_to_text.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
|
||
# cast_bool_to_text | ||
|
||
models__test_cast_bool_to_text_sql = """ | ||
with data as ( | ||
|
||
select 0=1 as input, 'false' as expected union all | ||
select 1=1 as input, 'true' as expected union all | ||
select null as input, null as expected | ||
|
||
) | ||
|
||
select | ||
|
||
{{ dbt_utils.cast_bool_to_text("input") }} as actual, | ||
expected | ||
|
||
from data | ||
""" | ||
|
||
|
||
models__test_cast_bool_to_text_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_cast_bool_to_text | ||
tests: | ||
- assert_equal: | ||
actual: actual | ||
expected: expected | ||
""" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Realized this isn't needed! Just installed out of habit