Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unit tests for artifact mixin #11050

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions tests/unit/artifacts/test_artifact_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

from dataclasses import dataclass
import pytest


from dbt.artifacts.schemas.base import (
ArtifactMixin,
BaseArtifactMetadata,
schema_version,
)

@dataclass
@schema_version("manifest", 12)
class Artifact(ArtifactMixin):
a: int


@dataclass
@schema_version("manifest", 12)
class ArtifactMinorSchemaChange(ArtifactMixin):
a: int
b: int = 2


class TestMinorSchemaChange:
@pytest.fixture
def artifact(self):
return Artifact(a = 1, metadata=BaseArtifactMetadata(dbt_schema_version="1.9.0"))

@pytest.fixture
def artifact_minor_schema_change(self):
return ArtifactMinorSchemaChange(a=1, metadata=BaseArtifactMetadata(dbt_schema_version="1.9.0"))

def test_serializing_new_default_field_is_backward_compatabile(
self, artifact_minor_schema_change
):
# old code (using old class) can create an instance of itself given new data (new class)
new_data = artifact_minor_schema_change.to_dict()
# Ensure new data has additional field
assert new_data["b"] == 2
artifact = Artifact.from_dict(new_data)

assert artifact.a == 1
# Additional fields are ignored
assert not hasattr(artifact, "b")

def test_serializing_new_default_field_is_forward_compatible(self, artifact):
# new code (using new class) can create an instance of itself given old data (old class)
artifact = ArtifactMinorSchemaChange.from_dict(artifact.to_dict())

assert artifact.a == 1
assert artifact.b == 2

def test_serializing_removed_default_field_is_backward_compatabile(self, artifact):
# old code (using old class with default field) can create an instance of itself given new data (class w/o default field)
old_artifact = ArtifactMinorSchemaChange.from_dict(artifact.to_dict())

# set to the default value when not provided in data
assert old_artifact.b == 2

def test_serializing_removed_default_field_is_forward_compatible(
self, artifact_minor_schema_change
):
# new code (using class without default field) can create an instance of itself given old data (class with old field)
Artifact.from_dict(artifact_minor_schema_change.to_dict())
Loading