Skip to content

Commit

Permalink
Throw exceptions on invalid entity names in filter and group by expre…
Browse files Browse the repository at this point in the history
…ssions (#203)

The previous logic was simply calling an error string formatting
function,
and one which would return an incorrect error message at that. This
raises
an exception with an entity-specific name formatting error message.
  • Loading branch information
tlento authored Oct 31, 2023
1 parent 2b4790e commit 7bd1c7a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
7 changes: 7 additions & 0 deletions .changes/unreleased/Fixes-20231009-211057.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
kind: Fixes
body: Throw an exception when invalid entity names are encountered in filter/group
by parameters
time: 2023-10-09T21:10:57.456012-07:00
custom:
Author: tlento
Issue: "172"
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def create_entity(entity_name: str, entity_path: Sequence[str] = ()) -> EntityCa
"""Gets called by Jinja when rendering {{ Entity(...) }}."""
group_by_item_name = DunderedNameFormatter.parse_name(entity_name)
if len(group_by_item_name.entity_links) > 0 or group_by_item_name.time_granularity is not None:
ParameterSetFactory._exception_message_for_incorrect_format(entity_name)
raise ParseWhereFilterException(
f"Entity name is in an incorrect format: '{entity_name}'. "
f"It should not contain any dunders (double underscores, or __)."
)

return EntityCallParameterSet(
entity_path=tuple(EntityReference(element_name=arg) for arg in entity_path),
Expand Down
11 changes: 11 additions & 0 deletions tests/implementations/where_filter/test_parse_calls.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import logging

import pytest

from dbt_semantic_interfaces.call_parameter_sets import (
DimensionCallParameterSet,
EntityCallParameterSet,
FilterCallParameterSets,
ParseWhereFilterException,
TimeDimensionCallParameterSet,
)
from dbt_semantic_interfaces.implementations.filters.where_filter import (
Expand Down Expand Up @@ -126,3 +129,11 @@ def test_extract_entity_call_parameter_sets() -> None: # noqa: D
),
),
)


def test_invalid_entity_name_error() -> None:
"""Test to ensure we throw an error if an entity name is invalid."""
bad_entity_filter = PydanticWhereFilter(where_sql_template="{{ Entity('order_id__is_food_order' )}}")

with pytest.raises(ParseWhereFilterException, match="Entity name is in an incorrect format"):
bad_entity_filter.call_parameter_sets

0 comments on commit 7bd1c7a

Please sign in to comment.