-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Unit test support for state:modified
and --defer
#9032
Merged
MichelleArk
merged 8 commits into
unit_testing_feature_branch
from
jerco/8517-unit-test-state-modified
Nov 14, 2023
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2d81994
Add unit tests to state:modified selection
jtcohen6 f70ff8c
Get defer working too yolo
jtcohen6 3d2f988
Refactor per marky suggestion
jtcohen6 0c790a4
Add changelog
jtcohen6 cc94269
Merge branch 'unit_testing_feature_branch' into jerco/8517-unit-test-…
MichelleArk 475e1eb
separate out unit test state tests + fix csv fixture tests
MichelleArk 7e49d3b
formatting
MichelleArk 8c9eb48
detect changes to fixture files with state:modified
MichelleArk 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Unit tests support --defer and state:modified | ||
time: 2023-11-07T23:10:06.376588-05:00 | ||
custom: | ||
Author: jtcohen6 | ||
Issue: "8517" |
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
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
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,135 @@ | ||
import os | ||
import pytest | ||
import shutil | ||
from copy import deepcopy | ||
|
||
from dbt.tests.util import ( | ||
run_dbt, | ||
write_file, | ||
write_config_file, | ||
) | ||
from fixtures import ( | ||
my_model_vars_sql, | ||
my_model_a_sql, | ||
my_model_b_sql, | ||
test_my_model_simple_fixture_yml, | ||
test_my_model_fixture_csv, | ||
test_my_model_b_fixture_csv as test_my_model_fixture_csv_modified, | ||
) | ||
|
||
|
||
class UnitTestState: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model.sql": my_model_vars_sql, | ||
"my_model_a.sql": my_model_a_sql, | ||
"my_model_b.sql": my_model_b_sql, | ||
"test_my_model.yml": test_my_model_simple_fixture_yml, | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def tests(self): | ||
return { | ||
"fixtures": { | ||
"test_my_model_fixture.csv": test_my_model_fixture_csv, | ||
} | ||
} | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"vars": {"my_test": "my_test_var"}} | ||
|
||
def copy_state(self, project_root): | ||
state_path = os.path.join(project_root, "state") | ||
if not os.path.exists(state_path): | ||
os.makedirs(state_path) | ||
shutil.copyfile( | ||
f"{project_root}/target/manifest.json", f"{project_root}/state/manifest.json" | ||
) | ||
shutil.copyfile( | ||
f"{project_root}/target/run_results.json", f"{project_root}/state/run_results.json" | ||
) | ||
|
||
|
||
class TestUnitTestStateModified(UnitTestState): | ||
def test_state_modified(self, project): | ||
run_dbt(["run"]) | ||
run_dbt(["unit-test"], expect_pass=False) | ||
self.copy_state(project.project_root) | ||
|
||
# no changes | ||
results = run_dbt(["unit-test", "--select", "state:modified", "--state", "state"]) | ||
assert len(results) == 0 | ||
|
||
# change underlying fixture file | ||
write_file( | ||
test_my_model_fixture_csv_modified, | ||
project.project_root, | ||
"tests", | ||
"fixtures", | ||
"test_my_model_fixture.csv", | ||
) | ||
# TODO: remove --no-partial-parse as part of https://github.com/dbt-labs/dbt-core/issues/9067 | ||
results = run_dbt( | ||
["--no-partial-parse", "unit-test", "--select", "state:modified", "--state", "state"], | ||
expect_pass=True, | ||
) | ||
assert len(results) == 1 | ||
assert results[0].node.name.endswith("test_depends_on_fixture") | ||
# reset changes | ||
self.copy_state(project.project_root) | ||
|
||
# change unit test definition of a single unit test | ||
with_changes = test_my_model_simple_fixture_yml.replace("{string_c: ab}", "{string_c: bc}") | ||
write_config_file(with_changes, project.project_root, "models", "test_my_model.yml") | ||
results = run_dbt( | ||
["unit-test", "--select", "state:modified", "--state", "state"], expect_pass=False | ||
) | ||
assert len(results) == 1 | ||
assert results[0].node.name.endswith("test_has_string_c_ab") | ||
|
||
# change underlying model logic | ||
write_config_file( | ||
test_my_model_simple_fixture_yml, project.project_root, "models", "test_my_model.yml" | ||
) | ||
write_file( | ||
my_model_vars_sql.replace("a+b as c,", "a + b as c,"), | ||
project.project_root, | ||
"models", | ||
"my_model.sql", | ||
) | ||
results = run_dbt( | ||
["unit-test", "--select", "state:modified", "--state", "state"], expect_pass=False | ||
) | ||
assert len(results) == 4 | ||
|
||
|
||
class TestUnitTestRetry(UnitTestState): | ||
def test_unit_test_retry(self, project): | ||
run_dbt(["run"]) | ||
run_dbt(["unit-test"], expect_pass=False) | ||
self.copy_state(project.project_root) | ||
|
||
results = run_dbt(["retry"], expect_pass=False) | ||
assert len(results) == 1 | ||
|
||
|
||
class TestUnitTestDeferState(UnitTestState): | ||
@pytest.fixture(scope="class") | ||
def other_schema(self, unique_schema): | ||
return unique_schema + "_other" | ||
|
||
@pytest.fixture(scope="class") | ||
def profiles_config_update(self, dbt_profile_target, unique_schema, other_schema): | ||
outputs = {"default": dbt_profile_target, "otherschema": deepcopy(dbt_profile_target)} | ||
outputs["default"]["schema"] = unique_schema | ||
outputs["otherschema"]["schema"] = other_schema | ||
return {"test": {"outputs": outputs, "target": "default"}} | ||
|
||
def test_unit_test_defer_state(self, project): | ||
run_dbt(["run", "--target", "otherschema"]) | ||
self.copy_state(project.project_root) | ||
results = run_dbt(["unit-test", "--defer", "--state", "state"], expect_pass=False) | ||
assert len(results) == 4 | ||
assert sorted([r.status for r in results]) == ["fail", "pass", "pass", "pass"] |
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
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.
👍
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.
After moving the calling of unit tests into the test task, this is going to be harder to do differently like this since running unit tests will hook into the standard runnable flow.
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.
yeah, exactly... 🤔 happy to brainstorm alternative approaches in the test task work