From 92b65501d25dd21fdcc67057c69f322b9efb6ed2 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Thu, 16 Nov 2017 17:13:33 -0500 Subject: [PATCH 1/4] fix schema tests used in, or defined in packages --- dbt/parser.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/dbt/parser.py b/dbt/parser.py index e2506e59395..a552a366c1e 100644 --- a/dbt/parser.py +++ b/dbt/parser.py @@ -471,14 +471,20 @@ def parse_schema_tests(tests, root_project, projects, macros=None): for config in configs: package_name = test.get('package_name') + test_namespace = 'dbt' split = test_type.split('.') if len(split) > 1: test_type = split[1] package_name = split[0] + test_namespace = package_name to_add = parse_schema_test( - test, model_name, config, test_type, + test, + model_name, + config, + test_namespace, + test_type, root_project, projects.get(package_name), all_projects=projects, @@ -533,8 +539,8 @@ def as_kwarg(key, value): return "{key}={value}".format(key=key, value=formatted_value) -def parse_schema_test(test_base, model_name, test_config, test_type, - root_project_config, package_project_config, +def parse_schema_test(test_base, model_name, test_config, test_namespace, + test_type, root_project_config, package_project_config, all_projects, macros=None): if isinstance(test_config, (basestring, int, float, bool)): @@ -545,13 +551,7 @@ def parse_schema_test(test_base, model_name, test_config, test_type, # sort the dict so the keys are rendered deterministically (for tests) kwargs = [as_kwarg(key, test_args[key]) for key in sorted(test_args)] - macro_name = "test_{}".format(test_type) - - if package_project_config.get('name') != \ - root_project_config.get('name'): - macro_name = "{}.{}".format(package_project_config.get('name'), - macro_name) - + macro_name = "{}.test_{}".format(test_namespace, test_type) raw_sql = "{{{{ {macro}(model=ref('{model}'), {kwargs}) }}}}".format(**{ 'model': model_name, 'macro': macro_name, From dca401af020c02e0ddbd3124d1bab1cad42c5e67 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Thu, 16 Nov 2017 17:20:16 -0500 Subject: [PATCH 2/4] don't hardcode dbt test namespace --- dbt/parser.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/dbt/parser.py b/dbt/parser.py index a552a366c1e..2c68ee08455 100644 --- a/dbt/parser.py +++ b/dbt/parser.py @@ -471,7 +471,7 @@ def parse_schema_tests(tests, root_project, projects, macros=None): for config in configs: package_name = test.get('package_name') - test_namespace = 'dbt' + test_namespace = None split = test_type.split('.') if len(split) > 1: @@ -551,7 +551,11 @@ def parse_schema_test(test_base, model_name, test_config, test_namespace, # sort the dict so the keys are rendered deterministically (for tests) kwargs = [as_kwarg(key, test_args[key]) for key in sorted(test_args)] - macro_name = "{}.test_{}".format(test_namespace, test_type) + if test_namespace is None: + macro_name = "test_{}".format(test_type) + else: + macro_name = "{}.test_{}".format(test_namespace, test_type) + raw_sql = "{{{{ {macro}(model=ref('{model}'), {kwargs}) }}}}".format(**{ 'model': model_name, 'macro': macro_name, From ba552294b7ccfc96b2b129835fc9d2d0785dd89e Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Sun, 19 Nov 2017 11:15:49 -0500 Subject: [PATCH 3/4] fix/actually run tests --- .../008_schema_tests_test/macros/tests.sql | 2 +- .../008_schema_tests_test/models-custom/schema.yml | 6 +++--- .../008_schema_tests_test/test_schema_tests.py | 12 ++++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/test/integration/008_schema_tests_test/macros/tests.sql b/test/integration/008_schema_tests_test/macros/tests.sql index 3e196fee9eb..e962f7896a8 100644 --- a/test/integration/008_schema_tests_test/macros/tests.sql +++ b/test/integration/008_schema_tests_test/macros/tests.sql @@ -19,7 +19,7 @@ from {{ model }} where {{ field }} in ( {% for value in values %} - {{ value }} {% if not loop.last %} , {% endif %} + '{{ value }}' {% if not loop.last %} , {% endif %} {% endfor %} ) diff --git a/test/integration/008_schema_tests_test/models-custom/schema.yml b/test/integration/008_schema_tests_test/models-custom/schema.yml index dab76f25462..15c9b95552a 100644 --- a/test/integration/008_schema_tests_test/models-custom/schema.yml +++ b/test/integration/008_schema_tests_test/models-custom/schema.yml @@ -11,12 +11,12 @@ table_copy: # fails every_value_is_blue: - - color + - favorite_color # passes rejected_values: - - { field: 'color', values: ['orange', 'purple'] } + - { field: 'favorite_color', values: ['orange', 'purple'] } # passes dbt_utils.equality: - - "{{ ref('seed') }}" + - ref('table_copy') diff --git a/test/integration/008_schema_tests_test/test_schema_tests.py b/test/integration/008_schema_tests_test/test_schema_tests.py index 8e986030228..9e730e98dec 100644 --- a/test/integration/008_schema_tests_test/test_schema_tests.py +++ b/test/integration/008_schema_tests_test/test_schema_tests.py @@ -89,10 +89,14 @@ def schema(self): @property def project_config(self): + # dbt-utils containts a schema test (equality) + # dbt-integration-project contains a schema.yml file + # both should work! return { "macro-paths": ["test/integration/008_schema_tests_test/macros"], "repositories": [ - 'https://github.com/fishtown-analytics/dbt-utils' + 'https://github.com/fishtown-analytics/dbt-utils', + 'https://github.com/fishtown-analytics/dbt-integration-project' ] } @@ -113,5 +117,9 @@ def test_schema_tests(self): self.run_dbt() test_results = self.run_schema_validations() + rejected_values_table_copy_color__orange__purple + expected_failures = ['unique', 'every_value_is_blue'] + for result in test_results: - print(result.node, result.errored, result.skipped, result.status) + if result.errored: + self.assertTrue(result.node['name'] in expected_failures) From 943370729cf43900ec1f3feff6902c291b9d093d Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Sun, 19 Nov 2017 16:41:17 -0500 Subject: [PATCH 4/4] rm junk --- test/integration/008_schema_tests_test/test_schema_tests.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/integration/008_schema_tests_test/test_schema_tests.py b/test/integration/008_schema_tests_test/test_schema_tests.py index 9e730e98dec..8ec45103cd7 100644 --- a/test/integration/008_schema_tests_test/test_schema_tests.py +++ b/test/integration/008_schema_tests_test/test_schema_tests.py @@ -117,7 +117,6 @@ def test_schema_tests(self): self.run_dbt() test_results = self.run_schema_validations() - rejected_values_table_copy_color__orange__purple expected_failures = ['unique', 'every_value_is_blue'] for result in test_results: