Skip to content

Commit

Permalink
added logic to treat generic tests like macro tests
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoop committed Oct 20, 2021
1 parent 097686c commit 2017b1d
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 5 deletions.
14 changes: 9 additions & 5 deletions core/dbt/parser/partial.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
ParseFileType.Seed,
ParseFileType.Snapshot,
ParseFileType.Analysis,
ParseFileType.GenericTest,
ParseFileType.SingularTest,
)

mg_files = (
ParseFileType.Macro,
ParseFileType.GenericTest,
)


key_to_prefix = {
'models': 'model',
Expand Down Expand Up @@ -89,7 +93,7 @@ def build_file_diff(self):
if self.saved_files[file_id].parse_file_type == ParseFileType.Schema:
deleted_schema_files.append(file_id)
else:
if self.saved_files[file_id].parse_file_type == ParseFileType.Macro:
if self.saved_files[file_id].parse_file_type in mg_files:
changed_or_deleted_macro_file = True
deleted.append(file_id)

Expand All @@ -107,7 +111,7 @@ def build_file_diff(self):
raise Exception(f"Serialization failure for {file_id}")
changed_schema_files.append(file_id)
else:
if self.saved_files[file_id].parse_file_type == ParseFileType.Macro:
if self.saved_files[file_id].parse_file_type in mg_files:
changed_or_deleted_macro_file = True
changed.append(file_id)
file_diff = {
Expand Down Expand Up @@ -214,7 +218,7 @@ def delete_from_saved(self, file_id):
self.deleted_manifest.files[file_id] = self.saved_manifest.files.pop(file_id)

# macros
if saved_source_file.parse_file_type == ParseFileType.Macro:
if saved_source_file.parse_file_type in mg_files:
self.delete_macro_file(saved_source_file, follow_references=True)

# docs
Expand All @@ -230,7 +234,7 @@ def update_in_saved(self, file_id):

if new_source_file.parse_file_type in mssat_files:
self.update_mssat_in_saved(new_source_file, old_source_file)
elif new_source_file.parse_file_type == ParseFileType.Macro:
elif new_source_file.parse_file_type in mg_files:
self.update_macro_in_saved(new_source_file, old_source_file)
elif new_source_file.parse_file_type == ParseFileType.Documentation:
self.update_doc_in_saved(new_source_file, old_source_file)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: 2

models:
- name: orders
description: "Some order data"
columns:
- name: id
tests:
- unique
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% test is_odd(model, column_name) %}

with validation as (

select
{{ column_name }} as odd_field

from {{ model }}

),

validation_errors as (

select
odd_field

from validation
-- if this is true, then odd_field is actually even!
where (odd_field % 2) = 0

)

select *
from validation_errors

{% endtest %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{% test is_odd(model, column_name) %}

with validation as (

select
{{ column_name }} as odd_field2

from {{ model }}

),

validation_errors as (

select
odd_field2

from validation
-- if this is true, then odd_field is actually even!
where (odd_field2 % 2) = 0

)

select *
from validation_errors

{% endtest %}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2

models:
- name: orders
description: "Some order data"
columns:
- name: id
tests:
- unique
- is_odd
34 changes: 34 additions & 0 deletions test/integration/068_partial_parsing_tests/test_partial_parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def setup_directories(self):
# delete files in this directory without tests interfering with each other.
os.mkdir(os.path.join(self.test_root_dir, 'models'))
os.mkdir(os.path.join(self.test_root_dir, 'tests'))
os.mkdir(os.path.join(self.test_root_dir, 'tests/generic'))
os.mkdir(os.path.join(self.test_root_dir, 'seeds'))
os.mkdir(os.path.join(self.test_root_dir, 'macros'))
os.mkdir(os.path.join(self.test_root_dir, 'analyses'))
Expand Down Expand Up @@ -476,6 +477,7 @@ def test_postgres_skip_macros(self):
results, log_output = self.run_dbt_and_capture(['--partial-parse', 'run'])
self.assertTrue('Starting full parse.' in log_output)


class TestSnapshots(BasePPTest):

@use_profile('postgres')
Expand Down Expand Up @@ -508,3 +510,35 @@ def test_postgres_pp_snapshots(self):
self.rm_file(normalize('snapshots/snapshot.sql'))
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 1)


class TestTests(BasePPTest):

@use_profile('postgres')
def test_postgres_pp_generic_tests(self):

# initial run
self.setup_directories()
self.copy_file('test-files/orders.sql', 'models/orders.sql')
self.copy_file('test-files/generic_schema.yml', 'models/schema.yml')
results = self.run_dbt()
self.assertEqual(len(results), 1)

# add generic test in test-path
self.copy_file('test-files/generic_test.sql', 'tests/generic/generic_test.sql')
self.copy_file('test-files/generic_test_schema.yml', 'models/schema.yml')
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 1)
manifest = get_manifest()
test_id = 'test.test.is_odd_orders_id.82834fdc5b'
self.assertIn(test_id, manifest.nodes)
self.assertEqual(len(manifest.nodes), 3)

# edit generic test in test-path
self.copy_file('test-files/generic_test_edited.sql', 'tests/generic/generic_test.sql')
results = self.run_dbt(["--partial-parse", "run"])
self.assertEqual(len(results), 1)
manifest = get_manifest()
test_id = 'test.test.is_odd_orders_id.82834fdc5b'
self.assertIn(test_id, manifest.nodes)
self.assertEqual(len(manifest.nodes), 3)

0 comments on commit 2017b1d

Please sign in to comment.