Skip to content

Commit

Permalink
Merge pull request #211 from dbt-labs/dc/more-jinja-block-fun
Browse files Browse the repository at this point in the history
Fix for custom generic tests
  • Loading branch information
dave-connors-3 authored Jul 10, 2024
2 parents c6ae767 + 13ee8a5 commit 9a1efa1
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
15 changes: 13 additions & 2 deletions dbt_meshify/dbt_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def models(self) -> Dict[str, ModelNode]:
return self._models

self._models = {
node_name: node
node_name: node # type: ignore
for node_name, node in self.manifest.nodes.items()
if node.resource_type == "model" and isinstance(node, ModelNode)
}
Expand Down Expand Up @@ -328,11 +328,22 @@ def find_jinja_blocks(self) -> Dict[str, JinjaBlock]:
)

for unique_id, macro in self.manifest.macros.items():
block_type = "macro"
name = macro.name
top_level_folder = macro.path.split("/")[0]

if macro.package_name != self.name:
continue
if (
top_level_folder in self.project.test_paths
if self.project.test_paths
else ["tests"]
):
block_type = "test"
name = macro.name[5:]

blocks[unique_id] = JinjaBlock.from_file(
path=self.path / macro.original_file_path, block_type="macro", name=macro.name
path=self.path / macro.original_file_path, block_type=block_type, name=name
)

return blocks
Expand Down
1 change: 1 addition & 0 deletions test-projects/split/split_proj/models/marts/__models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ models:
expression: count_food_items + count_drink_items = count_items
- dbt_utils.expression_is_true:
expression: subtotal_food_items + subtotal_drink_items = subtotal
- custom_generic_test
columns:
- name: order_id
description: The unique key of the orders mart.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{% test custom_generic_test(model) %}
select true where false
{% endtest %}
12 changes: 12 additions & 0 deletions tests/unit/test_jinja_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@
{% endmacro %}
"""

simple_custom_generic_test = """\
{% test my_custom_test(model) %}
select * from {{ model }} where false
{% endtest %}
"""

simple_macro_no_spaces = """\
Expand Down Expand Up @@ -176,3 +184,7 @@ def test_from_file_extracts_content_in_files_with_multiple_blocks(self):
content
== "{% docs potato_name %}\nThe name of the customer's favorite potato dish.\n{% enddocs %}"
)

def test_from_file_extracts_custom_generic_test(self):
range = JinjaBlock.find_block_range(simple_custom_generic_test, "test", "my_custom_test")
assert range == (2, 88)

0 comments on commit 9a1efa1

Please sign in to comment.