diff --git a/CHANGELOG.md b/CHANGELOG.md index 6e477775ec1..ca5635bc212 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Fix multiple disabled nodes ([#4013](https://github.com/dbt-labs/dbt/issues/4013), [#4018](https://github.com/dbt-labs/dbt/pull/4018)) - Fix multiple partial parsing errors ([#3996](https://github.com/dbt-labs/dbt/issues/3006), [#4020](https://github.com/dbt-labs/dbt/pull/4018)) - Return an error instead of a warning when runing with `--warn-error` and no models are selected ([#4006](https://github.com/dbt-labs/dbt/issues/4006), [#4019](https://github.com/dbt-labs/dbt/pull/4019)) +- Fixed bug with `error_if` test option ([#4070](https://github.com/dbt-labs/dbt-core/pull/4070)) ### Under the hood diff --git a/core/dbt/parser/generic_test_builders.py b/core/dbt/parser/generic_test_builders.py index b1b8df7537d..bb244c73c70 100644 --- a/core/dbt/parser/generic_test_builders.py +++ b/core/dbt/parser/generic_test_builders.py @@ -358,7 +358,7 @@ def get_static_config(self): if self.warn_if is not None: config['warn_if'] = self.warn_if if self.error_if is not None: - config['error_id'] = self.error_if + config['error_if'] = self.error_if if self.fail_calc is not None: config['fail_calc'] = self.fail_calc if self.store_failures is not None: @@ -369,8 +369,6 @@ def get_static_config(self): config['database'] = self.database if self.schema is not None: config['schema'] = self.schema - if self.alias is not None: - config['alias'] = self.alias return config def tags(self) -> List[str]: diff --git a/test/integration/008_schema_tests_test/models-v2/limit_null/schema.yml b/test/integration/008_schema_tests_test/models-v2/limit_null/schema.yml new file mode 100644 index 00000000000..f943e211a9d --- /dev/null +++ b/test/integration/008_schema_tests_test/models-v2/limit_null/schema.yml @@ -0,0 +1,37 @@ +version: 2 + +models: + - name: table_limit_null + description: "The table has 1 null values, and we're okay with that, until it's more than 1." + columns: + - name: favorite_color_full_list + description: "The favorite color" + - name: count + description: "The number of responses for this favorite color - purple will be null" + tests: + - not_null: + error_if: '>1' + warn_if: '>1' + + - name: table_warning_limit_null + description: "The table has 1 null value, and we're okay with 1, but want to know of any." + columns: + - name: favorite_color_full_list + description: "The favorite color" + - name: count + description: "The number of responses for this favorite color - purple will be null" + tests: + - not_null: + error_if: '>1' + + - name: table_failure_limit_null + description: "The table has some 2 null values, and that's not ok. Warn and error." + columns: + - name: favorite_color_full_list + description: "The favorite color" + - name: count + description: "The number of responses for this favorite color - purple will be null" + tests: + - not_null: + error_if: '>1' + \ No newline at end of file diff --git a/test/integration/008_schema_tests_test/models-v2/limit_null/table_failure_limit_null.sql b/test/integration/008_schema_tests_test/models-v2/limit_null/table_failure_limit_null.sql new file mode 100644 index 00000000000..26d62541055 --- /dev/null +++ b/test/integration/008_schema_tests_test/models-v2/limit_null/table_failure_limit_null.sql @@ -0,0 +1,11 @@ +{{ + config( + materialized='table' + ) +}} + +select * from {{ref('table_limit_null')}} + +UNION ALL + +select 'magenta' as favorite_color_full_list, null as count \ No newline at end of file diff --git a/test/integration/008_schema_tests_test/models-v2/limit_null/table_limit_null.sql b/test/integration/008_schema_tests_test/models-v2/limit_null/table_limit_null.sql new file mode 100644 index 00000000000..4fe9d36a906 --- /dev/null +++ b/test/integration/008_schema_tests_test/models-v2/limit_null/table_limit_null.sql @@ -0,0 +1,13 @@ +{{ + config( + materialized='table' + ) +}} + +select favorite_color as favorite_color_full_list, count(*) as count +from {{ this.schema }}.seed +group by 1 + +UNION ALL + +select 'purple' as favorite_color_full_list, null as count \ No newline at end of file diff --git a/test/integration/008_schema_tests_test/models-v2/limit_null/table_warning_limit_null.sql b/test/integration/008_schema_tests_test/models-v2/limit_null/table_warning_limit_null.sql new file mode 100644 index 00000000000..48991dc08d8 --- /dev/null +++ b/test/integration/008_schema_tests_test/models-v2/limit_null/table_warning_limit_null.sql @@ -0,0 +1,7 @@ +{{ + config( + materialized='table' + ) +}} + +select * from {{ref('table_limit_null')}} \ No newline at end of file diff --git a/test/integration/008_schema_tests_test/test_schema_v2_tests.py b/test/integration/008_schema_tests_test/test_schema_v2_tests.py index eb926ba1808..1187c0602e1 100644 --- a/test/integration/008_schema_tests_test/test_schema_v2_tests.py +++ b/test/integration/008_schema_tests_test/test_schema_v2_tests.py @@ -95,6 +95,69 @@ def test_postgres_schema_test_exclude_failures(self): for result in test_results: self.assertTestFailed(result) +class TestLimitedSchemaTests(DBTIntegrationTest): + + def setUp(self): + DBTIntegrationTest.setUp(self) + self.run_sql_file("seed.sql") + + @property + def schema(self): + return "schema_tests_008" + + @property + def models(self): + return "models-v2/limit_null" + + def run_schema_validations(self): + args = FakeArgs() + test_task = TestTask(args, self.config) + return test_task.run() + + def assertTestFailed(self, result): + self.assertEqual(result.status, "fail") + self.assertFalse(result.skipped) + self.assertTrue( + result.failures > 0, + 'test {} did not fail'.format(result.node.name) + ) + + def assertTestWarn(self, result): + self.assertEqual(result.status, "warn") + self.assertFalse(result.skipped) + self.assertTrue( + result.failures > 0, + 'test {} passed without expected warning'.format(result.node.name) + ) + + def assertTestPassed(self, result): + self.assertEqual(result.status, "pass") + self.assertFalse(result.skipped) + self.assertEqual( + result.failures, 0, + 'test {} failed'.format(result.node.name) + ) + + @use_profile('postgres') + def test_postgres_limit_schema_tests(self): + results = self.run_dbt() + self.assertEqual(len(results), 3) + test_results = self.run_schema_validations() + self.assertEqual(len(test_results), 3) + + for result in test_results: + # assert that all deliberately failing tests actually fail + if 'failure' in result.node.name: + self.assertTestFailed(result) + # assert that tests with warnings have them + elif 'warning' in result.node.name: + self.assertTestWarn(result) + # assert that actual tests pass + else: + self.assertTestPassed(result) + # warnings are also marked as failures + self.assertEqual(sum(x.failures for x in test_results), 3) + class TestMalformedSchemaTests(DBTIntegrationTest): diff --git a/test/integration/012_deprecation_tests/test_deprecations.py b/test/integration/012_deprecation_tests/test_deprecations.py index fdd6a4a9fb7..5622d2e3845 100644 --- a/test/integration/012_deprecation_tests/test_deprecations.py +++ b/test/integration/012_deprecation_tests/test_deprecations.py @@ -7,7 +7,6 @@ class BaseTestDeprecations(DBTIntegrationTest): def setUp(self): super().setUp() - # breakpoint() deprecations.reset_deprecations() @property