-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support longer unit test names + improve error handling in unit test …
…construction (#9396)
- Loading branch information
1 parent
dc47f6b
commit 0da5dfe
Showing
8 changed files
with
235 additions
and
12 deletions.
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: Fixes | ||
body: Support reasonably long unit test names | ||
time: 2024-01-15T16:53:10.42761-05:00 | ||
custom: | ||
Author: michelleark | ||
Issue: "9015" |
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
69 changes: 69 additions & 0 deletions
69
tests/adapter/dbt/tests/adapter/unit_testing/test_unit_testing.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,69 @@ | ||
import pytest | ||
|
||
from dbt.adapters.postgres.relation_configs import MAX_CHARACTERS_IN_IDENTIFIER | ||
from dbt.tests.util import run_dbt, write_file | ||
|
||
my_model_a_sql = """ | ||
SELECT | ||
1 as a, | ||
1 as id, | ||
2 as not_testing, | ||
'a' as string_a, | ||
DATE '2020-01-02' as date_a | ||
""" | ||
|
||
test_model_a_long_test_name_yml = """ | ||
unit_tests: | ||
- name: {test_name} | ||
model: my_model_a | ||
given: [] | ||
expect: | ||
rows: | ||
- {{a: 1, id: 1, not_testing: 2, string_a: "a", date_a: "2020-01-02"}} | ||
""" | ||
|
||
|
||
class BaseUnitTestLongTestName: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model_a.sql": my_model_a_sql, | ||
"test_model_a.yml": test_model_a_long_test_name_yml, | ||
} | ||
|
||
@pytest.fixture | ||
def max_unit_test_name_length(self) -> int: | ||
return -1 | ||
|
||
def test_long_unit_test_name(self, project, max_unit_test_name_length): | ||
# max test name == passing unit test | ||
write_file( | ||
test_model_a_long_test_name_yml.format(test_name="a" * max_unit_test_name_length), | ||
"models", | ||
"test_model_a.yml", | ||
) | ||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
results = run_dbt(["test"], expect_pass=True) | ||
assert len(results) == 1 | ||
|
||
# max test name == failing command | ||
write_file( | ||
test_model_a_long_test_name_yml.format( | ||
test_name="a" * (max_unit_test_name_length + 1) | ||
), | ||
"models", | ||
"test_model_a.yml", | ||
) | ||
|
||
results = run_dbt(["run"]) | ||
assert len(results) == 1 | ||
|
||
run_dbt(["test"], expect_pass=False) | ||
|
||
|
||
class TestPostgresUnitTestLongTestNames(BaseUnitTestLongTestName): | ||
@pytest.fixture | ||
def max_unit_test_name_length(self) -> int: | ||
return MAX_CHARACTERS_IN_IDENTIFIER |
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,69 @@ | ||
import pytest | ||
|
||
from dbt.tests.util import run_dbt, run_dbt_and_capture | ||
from dbt.exceptions import DuplicateResourceNameError | ||
|
||
from fixtures import ( | ||
my_model_a_sql, | ||
my_model_b_sql, | ||
test_model_a_b_yml, | ||
test_model_a_with_duplicate_test_name_yml, | ||
) | ||
|
||
|
||
class TestUnitTestDuplicateTestNamesAcrossModels: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model_a.sql": my_model_a_sql, | ||
"my_model_b.sql": my_model_b_sql, | ||
"test_model_a_b.yml": test_model_a_b_yml, | ||
} | ||
|
||
def test_duplicate_test_names_across_models(self, project): | ||
results = run_dbt(["run"]) | ||
assert len(results) == 2 | ||
|
||
# Select duplicate tests | ||
results, log_output = run_dbt_and_capture(["test"], expect_pass=True) | ||
assert len(results) == 2 | ||
assert ["my_model_a", "my_model_b"] == sorted([result.node.model for result in results]) | ||
assert "my_model_a::my_test_name" in log_output | ||
assert "my_model_b::my_test_name" in log_output | ||
|
||
# Test select duplicates by by test name | ||
results = run_dbt(["test", "--select", "test_name:my_test_name"]) | ||
assert len(results) == 2 | ||
assert ["my_model_a", "my_model_b"] == sorted([result.node.model for result in results]) | ||
assert "my_model_a::my_test_name" in log_output | ||
assert "my_model_b::my_test_name" in log_output | ||
|
||
results = run_dbt(["test", "--select", "my_model_a,test_name:my_test_name"]) | ||
assert len(results) == 1 | ||
assert results[0].node.model == "my_model_a" | ||
|
||
results = run_dbt(["test", "--select", "my_model_b,test_name:my_test_name"]) | ||
assert len(results) == 1 | ||
assert results[0].node.model == "my_model_b" | ||
|
||
# Test select by model name | ||
results = run_dbt(["test", "--select", "my_model_a"]) | ||
assert len(results) == 1 | ||
assert results[0].node.model == "my_model_a" | ||
|
||
results = run_dbt(["test", "--select", "my_model_b"]) | ||
assert len(results) == 1 | ||
assert results[0].node.model == "my_model_b" | ||
|
||
|
||
class TestUnitTestDuplicateTestNamesWithinModel: | ||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return { | ||
"my_model_a.sql": my_model_a_sql, | ||
"test_model_a.yml": test_model_a_with_duplicate_test_name_yml, | ||
} | ||
|
||
def test_duplicate_test_names_within_model(self, project): | ||
with pytest.raises(DuplicateResourceNameError): | ||
run_dbt(["run"]) |