Skip to content

Commit

Permalink
fix schema tests used in, or defined in packages (#599)
Browse files Browse the repository at this point in the history
* fix schema tests used in, or defined in packages

* don't hardcode dbt test namespace

* fix/actually run tests

* rm junk
  • Loading branch information
drewbanin authored Dec 2, 2017
1 parent 1c7f64e commit ed60db0
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 15 deletions.
22 changes: 13 additions & 9 deletions dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)):
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion test/integration/008_schema_tests_test/macros/tests.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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')
11 changes: 9 additions & 2 deletions test/integration/008_schema_tests_test/test_schema_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'
]
}

Expand All @@ -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)

0 comments on commit ed60db0

Please sign in to comment.