Edit on GitHub

sqlmesh.core.test.discovery

 1from __future__ import annotations
 2
 3import fnmatch
 4import itertools
 5import pathlib
 6import typing as t
 7
 8import ruamel
 9
10from sqlmesh.utils import unique
11from sqlmesh.utils.pydantic import PydanticModel
12
13
14class ModelTestMetadata(PydanticModel):
15    path: pathlib.Path
16    test_name: str
17    body: t.Union[t.Dict, ruamel.yaml.comments.CommentedMap]
18
19    @property
20    def fully_qualified_test_name(self) -> str:
21        return f"{self.path}::{self.test_name}"
22
23    @property
24    def model_name(self) -> str:
25        return self.body.get("model", "")
26
27    def __hash__(self) -> int:
28        return self.fully_qualified_test_name.__hash__()
29
30
31def filter_tests_by_patterns(
32    tests: list[ModelTestMetadata], patterns: list[str]
33) -> list[ModelTestMetadata]:
34    """Filter out tests whose filename or name does not match a pattern.
35
36    Args:
37        tests: A list of ModelTestMetadata named tuples to match.
38        patterns: A list of patterns to match against.
39
40    Returns:
41        A list of ModelTestMetadata named tuples.
42    """
43    return unique(
44        test
45        for test, pattern in itertools.product(tests, patterns)
46        if ("*" in pattern and fnmatch.fnmatchcase(test.fully_qualified_test_name, pattern))
47        or pattern in test.fully_qualified_test_name
48    )
class ModelTestMetadata(sqlmesh.utils.pydantic.PydanticModel):
15class ModelTestMetadata(PydanticModel):
16    path: pathlib.Path
17    test_name: str
18    body: t.Union[t.Dict, ruamel.yaml.comments.CommentedMap]
19
20    @property
21    def fully_qualified_test_name(self) -> str:
22        return f"{self.path}::{self.test_name}"
23
24    @property
25    def model_name(self) -> str:
26        return self.body.get("model", "")
27
28    def __hash__(self) -> int:
29        return self.fully_qualified_test_name.__hash__()

!!! abstract "Usage Documentation" Models

A base class for creating Pydantic models.

Attributes:
  • __class_vars__: The names of the class variables defined on the model.
  • __private_attributes__: Metadata about the private attributes of the model.
  • __signature__: The synthesized __init__ [Signature][inspect.Signature] of the model.
  • __pydantic_complete__: Whether model building is completed, or if there are still undefined fields.
  • __pydantic_core_schema__: The core schema of the model.
  • __pydantic_custom_init__: Whether the model has a custom __init__ function.
  • __pydantic_decorators__: Metadata containing the decorators defined on the model. This replaces Model.__validators__ and Model.__root_validators__ from Pydantic V1.
  • __pydantic_generic_metadata__: Metadata for generic models; contains data used for a similar purpose to __args__, __origin__, __parameters__ in typing-module generics. May eventually be replaced by these.
  • __pydantic_parent_namespace__: Parent namespace of the model, used for automatic rebuilding of models.
  • __pydantic_post_init__: The name of the post-init method for the model, if defined.
  • __pydantic_root_model__: Whether the model is a [RootModel][pydantic.root_model.RootModel].
  • __pydantic_serializer__: The pydantic-core SchemaSerializer used to dump instances of the model.
  • __pydantic_validator__: The pydantic-core SchemaValidator used to validate instances of the model.
  • __pydantic_fields__: A dictionary of field names and their corresponding [FieldInfo][pydantic.fields.FieldInfo] objects.
  • __pydantic_computed_fields__: A dictionary of computed field names and their corresponding [ComputedFieldInfo][pydantic.fields.ComputedFieldInfo] objects.
  • __pydantic_extra__: A dictionary containing extra values, if [extra][pydantic.config.ConfigDict.extra] is set to 'allow'.
  • __pydantic_fields_set__: The names of fields explicitly set during instantiation.
  • __pydantic_private__: Values of private attributes set on the model instance.
path: pathlib.Path
test_name: str
body: Union[Dict, ruamel.yaml.comments.CommentedMap]
fully_qualified_test_name: str
20    @property
21    def fully_qualified_test_name(self) -> str:
22        return f"{self.path}::{self.test_name}"
model_name: str
24    @property
25    def model_name(self) -> str:
26        return self.body.get("model", "")
model_config = {'json_encoders': {<class 'sqlglot.expressions.core.Expr'>: <function _expression_encoder>, <class 'sqlglot.expressions.datatypes.DataType'>: <function _expression_encoder>, <class 'sqlglot.expressions.query.Tuple'>: <function _expression_encoder>, typing.Union[sqlglot.expressions.query.Query, sqlmesh.core.dialect.JinjaQuery]: <function _expression_encoder>, typing.Union[sqlglot.expressions.query.Query, sqlmesh.core.dialect.JinjaQuery, sqlmesh.core.dialect.MacroFunc]: <function _expression_encoder>, <class 'datetime.tzinfo'>: <function PydanticModel.<lambda>>}, 'arbitrary_types_allowed': True, 'extra': 'forbid', 'protected_namespaces': ()}

Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].

Inherited Members
pydantic.main.BaseModel
BaseModel
model_fields
model_computed_fields
model_extra
model_fields_set
model_construct
model_copy
model_dump
model_dump_json
model_json_schema
model_parametrized_name
model_post_init
model_rebuild
model_validate
model_validate_json
model_validate_strings
parse_file
from_orm
construct
schema
schema_json
validate
update_forward_refs
sqlmesh.utils.pydantic.PydanticModel
dict
json
copy
fields_set
parse_obj
parse_raw
missing_required_fields
extra_fields
all_fields
all_field_infos
required_fields
def filter_tests_by_patterns( tests: list[ModelTestMetadata], patterns: list[str]) -> list[ModelTestMetadata]:
32def filter_tests_by_patterns(
33    tests: list[ModelTestMetadata], patterns: list[str]
34) -> list[ModelTestMetadata]:
35    """Filter out tests whose filename or name does not match a pattern.
36
37    Args:
38        tests: A list of ModelTestMetadata named tuples to match.
39        patterns: A list of patterns to match against.
40
41    Returns:
42        A list of ModelTestMetadata named tuples.
43    """
44    return unique(
45        test
46        for test, pattern in itertools.product(tests, patterns)
47        if ("*" in pattern and fnmatch.fnmatchcase(test.fully_qualified_test_name, pattern))
48        or pattern in test.fully_qualified_test_name
49    )

Filter out tests whose filename or name does not match a pattern.

Arguments:
  • tests: A list of ModelTestMetadata named tuples to match.
  • patterns: A list of patterns to match against.
Returns:

A list of ModelTestMetadata named tuples.