Skip to content

Commit

Permalink
/* PR_START p--sq-order-by-limit 03 */ Add validation for the limit i…
Browse files Browse the repository at this point in the history
…n a saved query.
  • Loading branch information
plypaul committed Aug 5, 2024
1 parent 6d7c27a commit e210788
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
23 changes: 23 additions & 0 deletions dbt_semantic_interfaces/validations/saved_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,28 @@ def _check_order_by(saved_query: SavedQuery) -> Sequence[ValidationIssue]:

return validation_issues

@staticmethod
@validate_safely("Validate the order-by field in a saved query.")
def _check_limit(saved_query: SavedQuery) -> Sequence[ValidationIssue]:
validation_issues: List[ValidationIssue] = []
limit = saved_query.query_params.limit
if limit is None:
return validation_issues

if limit < 0:
validation_issues.append(
ValidationError(
message=f"Invalid limit value: {limit} (should be >= 0)",
context=SavedQueryContext(
file_context=FileContext.from_metadata(metadata=saved_query.metadata),
element_type=SavedQueryElementType.LIMIT,
element_value=str(limit),
),
)
)

return validation_issues

@staticmethod
@validate_safely("Validate all saved queries in a semantic manifest.")
def validate_manifest(semantic_manifest: SemanticManifestT) -> Sequence[ValidationIssue]: # noqa: D
Expand All @@ -269,6 +291,7 @@ def validate_manifest(semantic_manifest: SemanticManifestT) -> Sequence[Validati
)
issues += SavedQueryRule._check_where(saved_query)
issues += SavedQueryRule._check_order_by(saved_query)
issues += SavedQueryRule._check_limit(saved_query)
return issues


Expand Down
76 changes: 76 additions & 0 deletions tests/validations/test_saved_query_limit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
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_limit( # 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"],
limit=-1,
),
),
]

manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()])
check_only_one_error_with_message(
manifest_validator.validate_semantic_manifest(manifest),
"Invalid limit value",
)


def test_valid_limit( # 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"],
limit=1,
),
),
]

manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()])
results = manifest_validator.validate_semantic_manifest(manifest)
assert results.all_issues == ()


def test_zero_limit( # 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"],
limit=0,
),
),
]

manifest_validator = SemanticManifestValidator[PydanticSemanticManifest]([SavedQueryRule()])
results = manifest_validator.validate_semantic_manifest(manifest)
assert results.all_issues == ()

0 comments on commit e210788

Please sign in to comment.