Skip to content

Commit

Permalink
Use SchemaParser render context to render test configs (#3646) (#3650)
Browse files Browse the repository at this point in the history
* use available context when rendering test configs

* add test

* update changelog
  • Loading branch information
Kyle Wigley authored Jul 29, 2021
1 parent 1dcdd4c commit f0d7e58
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
- Partial parsing: handle source tests when changing test macro ([#3584](https://github.com/dbt-labs/dbt/issues/3584), [#3620](https://github.com/dbt-labs/dbt/pull/3620))
- Fix `dbt deps` version comparison logic which was causing incorrect pre-release package versions to be installed. ([#3578](https://github.com/dbt-labs/dbt/issues/3578), [#3609](https://github.com/dbt-labs/dbt/issues/3609))
- Partial parsing: schedule new macro file for parsing when macro patching ([#3627](https://github.com/dbt-labs/dbt/issues/3627), [#3627](https://github.com/dbt-labs/dbt/pull/3627))
- Use `SchemaParser`'s render context to render test configs in order to support `var()` configured at the project level and passed in from the cli ([#3564](https://github.com/dbt-labs/dbt/issues/3564). [#3646](https://github.com/dbt-labs/dbt/pull/3646))

### Docs
- Fix docs site crash if `relationships` test has one dependency instead of two ([docs#207](https://github.com/dbt-labs/dbt-docs/issues/207), ([docs#208](https://github.com/dbt-labs/dbt-docs/issues/208)))
Expand Down
12 changes: 4 additions & 8 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ def __init__(
self.project.config_version == 2
)
if all_v_2:
ctx = generate_schema_yml(
self.render_ctx = generate_schema_yml(
self.root_project, self.project.project_name
)
else:
ctx = generate_target_context(
self.render_ctx = generate_target_context(
self.root_project, self.root_project.cli_vars
)

self.raw_renderer = SchemaYamlRenderer(ctx)
self.raw_renderer = SchemaYamlRenderer(self.render_ctx)

internal_package_names = get_adapter_package_names(
self.root_project.credentials.type
Expand Down Expand Up @@ -287,17 +287,13 @@ def _parse_generic_test(
tags: List[str],
column_name: Optional[str],
) -> ParsedSchemaTestNode:

render_ctx = generate_target_context(
self.root_project, self.root_project.cli_vars
)
try:
builder = TestBuilder(
test=test,
target=target,
column_name=column_name,
package_name=target.package_name,
render_ctx=render_ctx,
render_ctx=self.render_ctx,
)
except CompilationException as exc:
context = _trimmed(str(target))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1 as id
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
version: 2

models:
- name: model
tests:
- equivalent:
value: "{{ var('myvar', 'baz') }}-bar"
30 changes: 28 additions & 2 deletions test/integration/008_schema_tests_test/test_schema_v2_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,14 +331,14 @@ def test_postgres_quote_required_column(self):
self.assertEqual(len(results), 2)


class TestVarsSchemaTests(DBTIntegrationTest):
class TestCliVarsSchemaTests(DBTIntegrationTest):
@property
def schema(self):
return "schema_tests_008"

@property
def models(self):
return "models-v2/render_test_arg_models"
return "models-v2/render_test_cli_arg_models"

@property
def project_config(self):
Expand All @@ -356,6 +356,32 @@ def test_postgres_argument_rendering(self):
self.run_dbt(['test'], expect_pass=False)


class TestConfiguredVarsSchemaTests(DBTIntegrationTest):
@property
def schema(self):
return "schema_tests_008"

@property
def models(self):
return "models-v2/render_test_configured_arg_models"

@property
def project_config(self):
return {
'config-version': 2,
"macro-paths": ["macros-v2/macros"],
'vars': {
'myvar': 'foo'
}
}

@use_profile('postgres')
def test_postgres_argument_rendering(self):
results = self.run_dbt()
self.assertEqual(len(results), 1)
results = self.run_dbt(['test'])
self.assertEqual(len(results), 1)

class TestSchemaCaseInsensitive(DBTIntegrationTest):
@property
def schema(self):
Expand Down

0 comments on commit f0d7e58

Please sign in to comment.