generated from dbt-labs/dbt-oss-template
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add msgpack mixin to dbtClassMixin (#125)
* Add msgpack mixin to dbtClassMixin * missing Optional * Changie
- Loading branch information
Showing
6 changed files
with
70 additions
and
14 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Features | ||
body: Enable serialization contexts | ||
time: 2024-05-06T17:00:58.272104-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "126" |
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,42 @@ | ||
from dataclasses import dataclass | ||
from typing import Dict, Optional | ||
|
||
from dbt_common.dataclass_schema import dbtClassMixin | ||
|
||
|
||
@dataclass | ||
class MySubObject(dbtClassMixin): | ||
name: str | ||
|
||
def __post_serialize__(self, dct: Dict, context: Optional[Dict] = None): | ||
if context and "artifact" in context: | ||
dct["name"] = "xxxx" | ||
return dct | ||
|
||
|
||
@dataclass | ||
class MyObject(dbtClassMixin): | ||
sub_object: MySubObject | ||
unique_id: str | ||
|
||
def __post_serialize__(self, dct: Dict, context: Optional[Dict] = None): | ||
if context and "artifact" in context: | ||
dct["unique_id"] = "my.xxxx.object" | ||
return dct | ||
|
||
|
||
def test_serialization_context(): | ||
sub_obj = MySubObject("testing") | ||
|
||
obj = MyObject(sub_object=sub_obj, unique_id="my.test.object") | ||
|
||
assert obj | ||
|
||
dct = obj.to_dict() | ||
assert dct == {"unique_id": "my.test.object", "sub_object": {"name": "testing"}} | ||
|
||
mod_dct = obj.to_dict(context={"artifact": True}) | ||
assert mod_dct == {"unique_id": "my.xxxx.object", "sub_object": {"name": "xxxx"}} | ||
|
||
obj = MyObject.from_dict(dct) | ||
assert obj.sub_object.name == "testing" |
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