Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix schema tests used in, or defined in packages #599

Merged
merged 4 commits into from
Dec 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions dbt/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 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 @@ -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)):
Expand All @@ -545,12 +551,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is totally reasonable.

taking a higher view: i don't like that the path has to be manipulated like this to inject test_, i think this would all be a lot cleaner if we just called a macro by its name instead of by its name, plus test_. then we don't have to do this manipulation, we just pass the name through.


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)