From ed60db0d3b52ef5e66633285a8dda7ed600fdf3a Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Sat, 2 Dec 2017 17:23:39 -0500 Subject: [PATCH] fix schema tests used in, or defined in packages (#599) * fix schema tests used in, or defined in packages * don't hardcode dbt test namespace * fix/actually run tests * rm junk --- dbt/parser.py | 22 +++++++++++-------- .../008_schema_tests_test/macros/tests.sql | 2 +- .../models-custom/schema.yml | 6 ++--- .../test_schema_tests.py | 11 ++++++++-- 4 files changed, 26 insertions(+), 15 deletions(-) diff --git a/dbt/parser.py b/dbt/parser.py index bbc43aa793a..b2404256ebf 100644 --- a/dbt/parser.py +++ b/dbt/parser.py @@ -489,14 +489,20 @@ def parse_schema_tests(tests, root_project, projects, macros=None): for config in configs: package_name = test.get('package_name') + test_namespace = None 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, @@ -551,8 +557,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)): @@ -563,12 +569,10 @@ 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) + 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, 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..8ec45103cd7 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,8 @@ def test_schema_tests(self): self.run_dbt() test_results = self.run_schema_validations() + 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)