-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add validation for order-by in saved queries.
- Loading branch information
Showing
5 changed files
with
258 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import copy | ||
|
||
from dbt_semantic_interfaces.implementations.saved_query import ( | ||
PydanticSavedQuery, | ||
PydanticSavedQueryQueryParams, | ||
) | ||
from dbt_semantic_interfaces.implementations.semantic_manifest import ( | ||
PydanticSemanticManifest, | ||
) | ||
from dbt_semantic_interfaces.validations.saved_query import SavedQueryRule | ||
from dbt_semantic_interfaces.validations.semantic_manifest_validator import ( | ||
SemanticManifestValidator, | ||
) | ||
from tests.validations.test_saved_query import check_only_one_error_with_message | ||
|
||
|
||
def test_invalid_order_by_descending_arg( # noqa: D | ||
simple_semantic_manifest__with_primary_transforms: PydanticSemanticManifest, | ||
) -> None: | ||
manifest = copy.deepcopy(simple_semantic_manifest__with_primary_transforms) | ||
manifest.saved_queries = [ | ||
PydanticSavedQuery( | ||
name="Example Saved Query", | ||
description="Example description.", | ||
query_params=PydanticSavedQueryQueryParams( | ||
metrics=["listings"], | ||
group_by=["TimeDimension('metric_time')"], | ||
order_by=["TimeDimension('metric_time').descending(foo)"], | ||
), | ||
), | ||
] | ||
|
||
manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()]) | ||
check_only_one_error_with_message( | ||
manifest_validator.validate_semantic_manifest(manifest), | ||
"An error occurred while parsing a field", | ||
) | ||
|
||
|
||
def test_invalid_order_by_due_to_mismatch( # noqa: D | ||
simple_semantic_manifest__with_primary_transforms: PydanticSemanticManifest, | ||
) -> None: | ||
manifest = copy.deepcopy(simple_semantic_manifest__with_primary_transforms) | ||
manifest.saved_queries = [ | ||
PydanticSavedQuery( | ||
name="Example Saved Query", | ||
description="Example description.", | ||
query_params=PydanticSavedQueryQueryParams( | ||
metrics=["listings"], | ||
group_by=["TimeDimension('metric_time')"], | ||
order_by=["Dimension('listing__country')"], | ||
), | ||
), | ||
] | ||
|
||
manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()]) | ||
check_only_one_error_with_message( | ||
manifest_validator.validate_semantic_manifest(manifest), | ||
"does not match any of the listed metrics or group-by items.", | ||
) | ||
|
||
|
||
def test_order_by_matches_metric( # noqa: D | ||
simple_semantic_manifest__with_primary_transforms: PydanticSemanticManifest, | ||
) -> None: | ||
manifest = copy.deepcopy(simple_semantic_manifest__with_primary_transforms) | ||
manifest.saved_queries = [ | ||
PydanticSavedQuery( | ||
name="Example Saved Query", | ||
description="Example description.", | ||
query_params=PydanticSavedQueryQueryParams( | ||
metrics=["listings"], | ||
group_by=["TimeDimension('metric_time')"], | ||
order_by=["Metric('listings').descending(True)"], | ||
), | ||
), | ||
] | ||
|
||
manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()]) | ||
results = manifest_validator.validate_semantic_manifest(manifest) | ||
assert results.all_issues == () | ||
|
||
|
||
def test_invalid_order_by_due_to_bare_metric( # noqa: D | ||
simple_semantic_manifest__with_primary_transforms: PydanticSemanticManifest, | ||
) -> None: | ||
manifest = copy.deepcopy(simple_semantic_manifest__with_primary_transforms) | ||
manifest.saved_queries = [ | ||
PydanticSavedQuery( | ||
name="Example Saved Query", | ||
description="Example description.", | ||
query_params=PydanticSavedQueryQueryParams( | ||
metrics=["listings"], group_by=["TimeDimension('metric_time')"], order_by=["listings"] | ||
), | ||
), | ||
] | ||
|
||
manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()]) | ||
check_only_one_error_with_message( | ||
manifest_validator.validate_semantic_manifest(manifest), | ||
"An error occurred while parsing a field", | ||
) |
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