Skip to content

Commit

Permalink
Add test and fix for schema configs with env_var
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Nov 3, 2021
1 parent eb0e340 commit c57fb1d
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 9 deletions.
9 changes: 6 additions & 3 deletions core/dbt/parser/schemas.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ def store_env_vars(self, target, schema_file_id, env_vars):
if '.' in search_name: # source file definitions
(search_name, _) = search_name.split('.')
else:
search_name = target.search_name
search_name = target.name
yaml_key = target.yaml_key
for var in env_vars.keys():
schema_file.add_env_var(var, yaml_key, search_name)
Expand Down Expand Up @@ -886,8 +886,11 @@ def parse_patch(
# re-application of the patch in partial parsing.
node.patch_path = source_file.file_id
else:
# Should we issue a warning message here?
return
raise ParsingException(
f"Did not find matching node for patch with name '{patch.name}' "
f"in the '{patch.yaml_key}' section of "
f"file '{source_file.path.original_file_path}'"
)

# patches can't be overwritten
node = self.manifest.nodes.get(unique_id)
Expand Down
3 changes: 3 additions & 0 deletions test/integration/060_persist_docs_tests/seeds/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name
1,Alice
2,Bob
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 'blue' as fun
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
version: 2
models:
- name: model_color
columns:
- name: fun
tests:
- unique:
enabled: "{{ env_var('ENV_VAR_ENABLED', True) }}"
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

select *
from {{ model }}
where {{ column_name }} == '{{ color }}'
where {{ column_name }} = '{{ color }}'

{% endtest %}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from dbt.exceptions import CompilationException
from dbt.exceptions import CompilationException, ParsingException
from dbt.contracts.graph.manifest import Manifest
from dbt.contracts.files import ParseFileType
from dbt.contracts.results import TestStatus
Expand All @@ -25,7 +25,7 @@ def models(self):
def project_config(self):
return {
'config-version': 2,
'data-paths': ['seeds'],
'seed-paths': ['seeds'],
'test-paths': ['tests'],
'macro-paths': ['macros'],
'analysis-paths': ['analyses'],
Expand Down Expand Up @@ -143,7 +143,7 @@ def test_postgres_pp_models(self):
# referred to in schema file
self.copy_file('test-files/models-schema2.yml', 'models/schema.yml')
self.rm_file('models/model_three.sql')
with self.assertRaises(CompilationException):
with self.assertRaises(ParsingException):
results = self.run_dbt(["--partial-parse", "--warn-error", "run"])

# Put model back again
Expand Down Expand Up @@ -312,7 +312,7 @@ def test_postgres_pp_sources(self):

# Change seed name to wrong name
self.copy_file('test-files/schema-sources5.yml', 'models/sources.yml')
with self.assertRaises(CompilationException):
with self.assertRaises(ParsingException):
results = self.run_dbt(["--partial-parse", "--warn-error", "run"])

# Put back seed name to right name
Expand Down
13 changes: 12 additions & 1 deletion test/integration/068_partial_parsing_tests/test_pp_vars.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def test_postgres_env_vars_models(self):

# set an env_var in a schema file
self.copy_file('test-files/env_var_schema.yml', 'models/schema.yml')
self.copy_file('test-files/env_var_model_one.sql', 'models/model_one.sql')
with self.assertRaises(ParsingException):
results = self.run_dbt(["--partial-parse", "run"])

Expand All @@ -99,7 +100,7 @@ def test_postgres_env_vars_models(self):
self.copy_file('test-files/env_var-sources.yml', 'models/sources.yml')
self.run_dbt(['--partial-parse', 'seed'])
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 2)
self.assertEqual(len(results), 3)
manifest = get_manifest()
expected_env_vars = {"ENV_VAR_TEST": "second", "TEST_SCHEMA_VAR": "view", "ENV_VAR_DATABASE": "dbt", "ENV_VAR_SEVERITY": "warn"}
self.assertEqual(expected_env_vars, manifest.env_vars)
Expand Down Expand Up @@ -210,6 +211,16 @@ def test_postgres_env_vars_models(self):
macro = manifest.macros[macro_id]
self.assertEqual(macro.meta, {"some_key": "dumdedum"})

# Add a schema file with a test on model_color and env_var in test enabled config
self.copy_file('test-files/env_var_model_test.yml', 'models/schema.yml')
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 3)
manifest = get_manifest()
model_color = manifest.nodes['model.test.model_color']
schema_file = manifest.files[model_color.patch_path]
expected_env_vars = {'models': {'model_one': ['TEST_SCHEMA_VAR', 'ENV_VAR_COLOR'], 'model_color': ['ENV_VAR_ENABLED']}, 'exposures': {'proxy_for_dashboard': ['ENV_VAR_OWNER']}}
self.assertEqual(expected_env_vars, schema_file.env_vars)


# delete the env vars to cleanup
del os.environ['ENV_VAR_TEST']
Expand Down

0 comments on commit c57fb1d

Please sign in to comment.