-
Notifications
You must be signed in to change notification settings - Fork 512
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 #577
Merged
dbeatty10
merged 34 commits into
add-pytest-functional
from
dbeatty/add-pytest-functional
May 12, 2022
Merged
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
b5b23f9
Skip BigQuery dateadd for now
dbeatty10 551c1c8
Split into one fixture and test per cross-database macro
dbeatty10 d59dac1
Move the skip decorator
dbeatty10 da58f27
Scaffolding for fixtures and tests
dbeatty10 3860605
Functional tests for any_value macro
dbeatty10 7bdaa49
Remove TODO
dbeatty10 2f17185
Functional tests for bool_or macro
dbeatty10 171a8e6
Functional tests for concat macro
dbeatty10 ce3bff2
Functional tests for date_trunc macro
dbeatty10 8a9cb50
Functional tests for hash macro
dbeatty10 10c845e
Functional tests for last_day macro
dbeatty10 6f3320c
Functional tests for length macro
dbeatty10 c3f6235
Functional tests for listagg macro
dbeatty10 7204519
Functional tests for position macro
dbeatty10 d421350
Functional tests for replace macro
dbeatty10 1bac8c8
Functional tests for right macro
dbeatty10 2521a6b
Functional tests for safe_cast macro
dbeatty10 0c3d208
Functional tests for split_part macro
dbeatty10 e11ccf0
Missing newline
dbeatty10 71375c4
Conform bool_or macro to actual/expected test pattern
dbeatty10 d4cbc0d
Conform any_value macro to actual/expected test pattern
dbeatty10 7e2ea34
Functional tests for current_timestamp macro
dbeatty10 de6e223
Remove extraneous newline
dbeatty10 1820d76
Functional tests for current_timestamp_in_utc macro
dbeatty10 e702f28
Functional tests for cast_bool_to_text macro
dbeatty10 ade997b
Functional tests for string_literal macro
dbeatty10 6b78b23
Functional tests for escape_single_quotes macro
dbeatty10 1618379
Functional tests for except macro
dbeatty10 2d386a2
Functional tests for intersect macro
dbeatty10 1161066
Fix scaffolded SQL so that tests can pass
dbeatty10 83af300
Postgres does not treat integers as booleans without casting
dbeatty10 fb8b406
Refactor test case override into the base class
dbeatty10 e565c9c
Refactor the two variants for escaping single quotes into two differe…
dbeatty10 3cbe0e6
Fix functional tests for dateadd macro for BigQuery
dbeatty10 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 |
---|---|---|
@@ -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 | ||
""" |
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,36 @@ | ||
|
||
|
||
# concat | ||
|
||
seeds__data_concat_csv = """input_1,input_2,output | ||
a,b,ab | ||
a,,a | ||
,b,b | ||
,, | ||
""" | ||
|
||
|
||
models__test_concat_sql = """ | ||
with data as ( | ||
|
||
select * from {{ ref('data_concat') }} | ||
|
||
) | ||
|
||
select | ||
{{ dbt_utils.concat(['input_1', 'input_2']) }} as actual, | ||
output as expected | ||
|
||
from data | ||
""" | ||
|
||
|
||
models__test_concat_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_concat | ||
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,6 @@ | ||
macros__test_assert_equal_sql = """ | ||
{% test assert_equal(model, actual, expected) %} | ||
select * from {{ model }} where {{ actual }} != {{ expected }} | ||
|
||
{% endtest %} | ||
""" |
20 changes: 20 additions & 0 deletions
20
tests/functional/cross_db_utils/fixture_current_timestamp.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,20 @@ | ||
|
||
# current_timestamp | ||
|
||
# TODO how can we test this better? | ||
models__test_current_timestamp_sql = """ | ||
select | ||
{{ dbt_utils.current_timestamp() }} as actual, | ||
{{ dbt_utils.current_timestamp() }} as expected | ||
""" | ||
|
||
|
||
models__test_current_timestamp_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_current_timestamp | ||
tests: | ||
- assert_equal: | ||
actual: actual | ||
expected: expected | ||
""" |
20 changes: 20 additions & 0 deletions
20
tests/functional/cross_db_utils/fixture_current_timestamp_in_utc.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,20 @@ | ||
|
||
# current_timestamp_in_utc | ||
|
||
# TODO how can we test this better? | ||
models__test_current_timestamp_in_utc_sql = """ | ||
select | ||
{{ dbt_utils.current_timestamp_in_utc() }} as actual, | ||
{{ dbt_utils.current_timestamp_in_utc() }} as expected | ||
""" | ||
|
||
|
||
models__test_current_timestamp_in_utc_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_current_timestamp_in_utc | ||
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,41 @@ | ||
|
||
# date_trunc | ||
|
||
seeds__data_date_trunc_csv = """updated_at,day,month | ||
2018-01-05 12:00:00,2018-01-05,2018-01-01 | ||
,, | ||
""" | ||
|
||
|
||
models__test_date_trunc_sql = """ | ||
with data as ( | ||
|
||
select * from {{ ref('data_date_trunc') }} | ||
|
||
) | ||
|
||
select | ||
cast({{dbt_utils.date_trunc('day', 'updated_at') }} as date) as actual, | ||
day as expected | ||
|
||
from data | ||
|
||
union all | ||
|
||
select | ||
cast({{ dbt_utils.date_trunc('month', 'updated_at') }} as date) as actual, | ||
month as expected | ||
|
||
from data | ||
""" | ||
|
||
|
||
models__test_date_trunc_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_date_trunc | ||
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,41 @@ | ||
|
||
# dateadd | ||
|
||
seeds__data_dateadd_csv = """from_time,interval_length,datepart,result | ||
2018-01-01 01:00:00,1,day,2018-01-02 01:00:00 | ||
2018-01-01 01:00:00,1,month,2018-02-01 01:00:00 | ||
2018-01-01 01:00:00,1,year,2019-01-01 01:00:00 | ||
2018-01-01 01:00:00,1,hour,2018-01-01 02:00:00 | ||
,1,day, | ||
""" | ||
|
||
|
||
models__test_dateadd_sql = """ | ||
with data as ( | ||
|
||
select * from {{ ref('data_dateadd') }} | ||
|
||
) | ||
|
||
select | ||
case | ||
when datepart = 'hour' then cast({{ dbt_utils.dateadd('hour', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}}) | ||
when datepart = 'day' then cast({{ dbt_utils.dateadd('day', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}}) | ||
when datepart = 'month' then cast({{ dbt_utils.dateadd('month', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}}) | ||
when datepart = 'year' then cast({{ dbt_utils.dateadd('year', 'interval_length', 'from_time') }} as {{dbt_utils.type_timestamp()}}) | ||
else null | ||
end as actual, | ||
result as expected | ||
|
||
from data | ||
""" | ||
|
||
models__test_dateadd_yml = """ | ||
version: 2 | ||
models: | ||
- name: test_dateadd | ||
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.
Hm. Juice might not be worth the squeeze on this one.
We could use Python/Jinja
{{ datetime.datetime.now() }}
, and perform a comparison that shaves off seconds? To be clear, I think this is a bad idea:Worth calling out that
dbt_utils.current_timestamp
is actually duplicative with:date_now
, which is required for all adapters but actually unused / untested (lol): (docs, abstractmethod, Postgres implementation)snapshot_get_time
, which is indeed a macro (not method) — not tested directly, but well tested insofar as snapshots are well tested, including their "right now" behavior (the tests for which can be flakey)