-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add generic tests to test-paths (#4052)
* removed overlooked breakpoint * first pass * save progress - singualr tests broken * fixed to work with both generic and singular tests * fixed formatting * added a comment * change to use /generic subfolder * fix formatting issues * fixed bug on code consolidation * fixed typo * added test for generic tests * added changelog entry * added logic to treat generic tests like macro tests * add generic test to macro_edges * fixed generic tests to match unique_ids * fixed test
- Loading branch information
Showing
16 changed files
with
304 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
from typing import Iterable, List | ||
|
||
import jinja2 | ||
|
||
from dbt.exceptions import CompilationException | ||
from dbt.clients import jinja | ||
from dbt.contracts.graph.parsed import ParsedGenericTestNode | ||
from dbt.contracts.graph.unparsed import UnparsedMacro | ||
from dbt.contracts.graph.parsed import ParsedMacro | ||
from dbt.contracts.files import SourceFile | ||
from dbt.logger import GLOBAL_LOGGER as logger | ||
from dbt.node_types import NodeType | ||
from dbt.parser.base import BaseParser | ||
from dbt.parser.search import FileBlock | ||
from dbt.utils import MACRO_PREFIX | ||
|
||
|
||
class GenericTestParser(BaseParser[ParsedGenericTestNode]): | ||
|
||
@property | ||
def resource_type(self) -> NodeType: | ||
return NodeType.Macro | ||
|
||
@classmethod | ||
def get_compiled_path(cls, block: FileBlock): | ||
return block.path.relative_path | ||
|
||
def parse_generic_test( | ||
self, block: jinja.BlockTag, base_node: UnparsedMacro, name: str | ||
) -> ParsedMacro: | ||
unique_id = self.generate_unique_id(name) | ||
|
||
return ParsedMacro( | ||
path=base_node.path, | ||
macro_sql=block.full_block, | ||
original_file_path=base_node.original_file_path, | ||
package_name=base_node.package_name, | ||
root_path=base_node.root_path, | ||
resource_type=base_node.resource_type, | ||
name=name, | ||
unique_id=unique_id, | ||
) | ||
|
||
def parse_unparsed_generic_test( | ||
self, base_node: UnparsedMacro | ||
) -> Iterable[ParsedMacro]: | ||
try: | ||
blocks: List[jinja.BlockTag] = [ | ||
t for t in | ||
jinja.extract_toplevel_blocks( | ||
base_node.raw_sql, | ||
allowed_blocks={'test'}, | ||
collect_raw_data=False, | ||
) | ||
if isinstance(t, jinja.BlockTag) | ||
] | ||
except CompilationException as exc: | ||
exc.add_node(base_node) | ||
raise | ||
|
||
for block in blocks: | ||
try: | ||
ast = jinja.parse(block.full_block) | ||
except CompilationException as e: | ||
e.add_node(base_node) | ||
raise | ||
|
||
# generic tests are structured as macros so we want to count the number of macro blocks | ||
generic_test_nodes = list(ast.find_all(jinja2.nodes.Macro)) | ||
|
||
if len(generic_test_nodes) != 1: | ||
# things have gone disastrously wrong, we thought we only | ||
# parsed one block! | ||
raise CompilationException( | ||
f'Found multiple generic tests in {block.full_block}, expected 1', | ||
node=base_node | ||
) | ||
|
||
generic_test_name = generic_test_nodes[0].name | ||
|
||
if not generic_test_name.startswith(MACRO_PREFIX): | ||
continue | ||
|
||
name: str = generic_test_name.replace(MACRO_PREFIX, '') | ||
node = self.parse_generic_test(block, base_node, name) | ||
yield node | ||
|
||
def parse_file(self, block: FileBlock): | ||
assert isinstance(block.file, SourceFile) | ||
source_file = block.file | ||
assert isinstance(source_file.contents, str) | ||
original_file_path = source_file.path.original_file_path | ||
logger.debug("Parsing {}".format(original_file_path)) | ||
|
||
# this is really only used for error messages | ||
base_node = UnparsedMacro( | ||
path=original_file_path, | ||
original_file_path=original_file_path, | ||
package_name=self.project.project_name, | ||
raw_sql=source_file.contents, | ||
root_path=self.project.project_root, | ||
resource_type=NodeType.Macro, | ||
) | ||
|
||
for node in self.parse_unparsed_generic_test(base_node): | ||
self.manifest.add_macro(block.file, node) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
test/integration/068_partial_parsing_tests/test-files/generic_schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
26 changes: 26 additions & 0 deletions
26
test/integration/068_partial_parsing_tests/test-files/generic_test.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
26 changes: 26 additions & 0 deletions
26
test/integration/068_partial_parsing_tests/test-files/generic_test_edited.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
10 changes: 10 additions & 0 deletions
10
test/integration/068_partial_parsing_tests/test-files/generic_test_schema.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.