-
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.
Merge pull request #315 from dbt-labs/plypaul--64--serializable-datac…
…lass-updates Test serializability of `SerializableDataclass` subclasses
- Loading branch information
Showing
6 changed files
with
152 additions
and
10 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
Empty file.
38 changes: 38 additions & 0 deletions
38
dbt_semantic_interfaces/test_helpers/dataclass_serialization.py
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,38 @@ | ||
from typing import Iterable, Sequence, Type | ||
|
||
from dbt_semantic_interfaces.dataclass_serialization import ( | ||
DataClassDeserializer, | ||
DataclassSerializer, | ||
SerializableDataclass, | ||
) | ||
|
||
|
||
def assert_includes_all_serializable_dataclass_types( | ||
instances: Sequence[SerializableDataclass], excluded_classes: Iterable[Type[SerializableDataclass]] | ||
) -> None: | ||
"""Verify that the given instances include at least one instance of the known subclasses.""" | ||
instance_types = {type(instance) for instance in instances} | ||
missing_instance_types = ( | ||
set(SerializableDataclass.concrete_subclasses_for_testing()) | ||
.difference(instance_types) | ||
.difference(excluded_classes) | ||
) | ||
missing_type_names = sorted(instance_type.__name__ for instance_type in missing_instance_types) | ||
assert ( | ||
len(missing_type_names) == 0 | ||
), f"Missing instances of the following classes: {missing_type_names}. Please add them." | ||
|
||
|
||
def assert_serializable(instances: Sequence[SerializableDataclass]) -> None: | ||
"""Verify that the given instances are actually serializable.""" | ||
serializer = DataclassSerializer() | ||
deserializer = DataClassDeserializer() | ||
|
||
for instance in instances: | ||
try: | ||
serialized_output = serializer.pydantic_serialize(instance) | ||
deserialized_instance = deserializer.pydantic_deserialize(type(instance), serialized_output) | ||
except Exception as e: | ||
raise AssertionError(f"Error serializing {instance=}") from e | ||
|
||
assert instance == deserialized_instance |
Empty file.
76 changes: 76 additions & 0 deletions
76
tests/serialization/test_serializable_dataclass_subclasses.py
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,76 @@ | ||
import itertools | ||
import logging | ||
|
||
from dbt_semantic_interfaces.references import ( | ||
DimensionReference, | ||
ElementReference, | ||
EntityReference, | ||
GroupByMetricReference, | ||
LinkableElementReference, | ||
MeasureReference, | ||
MetricModelReference, | ||
MetricReference, | ||
ModelReference, | ||
SemanticModelElementReference, | ||
SemanticModelReference, | ||
TimeDimensionReference, | ||
) | ||
from dbt_semantic_interfaces.test_helpers.dataclass_serialization import ( | ||
assert_includes_all_serializable_dataclass_types, | ||
assert_serializable, | ||
) | ||
from tests.test_dataclass_serialization import ( | ||
DataclassWithDataclassDefault, | ||
DataclassWithDefaultTuple, | ||
DataclassWithOptional, | ||
DataclassWithPrimitiveTypes, | ||
DataclassWithTuple, | ||
DeeplyNestedDataclass, | ||
NestedDataclass, | ||
NestedDataclassWithProtocol, | ||
SimpleClassWithProtocol, | ||
SimpleDataclass, | ||
) | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def test_serializable_dataclass_subclasses() -> None: | ||
"""Verify that all subclasses of `SerializableDataclass` are serializable.""" | ||
counter = itertools.count(start=0) | ||
|
||
def _get_next_field_str() -> str: | ||
return f"field_{next(counter)}" | ||
|
||
instances = [ | ||
LinkableElementReference(_get_next_field_str()), | ||
ElementReference(_get_next_field_str()), | ||
SemanticModelElementReference(_get_next_field_str(), _get_next_field_str()), | ||
EntityReference(_get_next_field_str()), | ||
SemanticModelReference(_get_next_field_str()), | ||
TimeDimensionReference(_get_next_field_str()), | ||
MetricReference(_get_next_field_str()), | ||
GroupByMetricReference(_get_next_field_str()), | ||
MetricModelReference(_get_next_field_str()), | ||
DimensionReference(_get_next_field_str()), | ||
MeasureReference(_get_next_field_str()), | ||
ModelReference(), | ||
] | ||
|
||
assert_includes_all_serializable_dataclass_types( | ||
instances=instances, | ||
# These are classes defined and used in a separate test. | ||
excluded_classes=[ | ||
DataclassWithDataclassDefault, | ||
DataclassWithDefaultTuple, | ||
DataclassWithOptional, | ||
DataclassWithPrimitiveTypes, | ||
DataclassWithTuple, | ||
DeeplyNestedDataclass, | ||
NestedDataclass, | ||
NestedDataclassWithProtocol, | ||
SimpleClassWithProtocol, | ||
SimpleDataclass, | ||
], | ||
) | ||
assert_serializable(instances) |