-
Notifications
You must be signed in to change notification settings - Fork 13
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 #34 from Opus10/deserialization
Fix deserialization of trigger channels
- Loading branch information
Showing
8 changed files
with
324 additions
and
141 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
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,24 @@ | ||
# Generated by Django 3.2.12 on 2023-04-11 08:29 | ||
|
||
from django.db import migrations, models | ||
import django.db.models.deletion | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('tests', '0001_initial'), | ||
] | ||
|
||
operations = [ | ||
migrations.AddField( | ||
model_name='author', | ||
name='alternative_name', | ||
field=models.TextField(db_column='other', null=True), | ||
), | ||
migrations.AlterField( | ||
model_name='author', | ||
name='profile_picture', | ||
field=models.ForeignKey(db_column='picture', null=True, on_delete=django.db.models.deletion.PROTECT, to='tests.media'), | ||
), | ||
] |
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,80 @@ | ||
from dataclasses import dataclass | ||
import datetime | ||
from typing import Dict, List, Set, Tuple | ||
|
||
from pgpubsub.channel import Channel | ||
|
||
|
||
def test_deserialize_1(): | ||
@dataclass | ||
class MyChannel(Channel): | ||
arg1: str | ||
arg2: Dict[int, int] | ||
default_arg1: float = 0.0 | ||
|
||
deserialized = _deserialize(MyChannel, arg1='1', arg2={1: 2}, default_arg1=3.4) | ||
assert {'arg1': '1', 'arg2': {1: 2}, | ||
'default_arg1': 3.4} == deserialized | ||
|
||
|
||
def test_deserialize_2(): | ||
@dataclass | ||
class MyChannel(Channel): | ||
arg1: Dict[str, bool] | ||
default_arg1: bool = False | ||
default_arg2: int = 0 | ||
|
||
deserialized = _deserialize( | ||
MyChannel, arg1={'Paul': False}, default_arg1=True) | ||
assert {'arg1': {'Paul': False}, | ||
'default_arg1': True, | ||
'default_arg2': 0} == deserialized | ||
|
||
|
||
def test_deserialize_3(): | ||
@dataclass | ||
class MyChannel(Channel): | ||
arg1: datetime.date | ||
arg2: Dict[datetime.date, bool] | ||
arg3: Dict[str, datetime.datetime] | ||
|
||
deserialized = _deserialize( | ||
MyChannel, | ||
arg1=datetime.date(2021, 1, 1), | ||
arg2={ | ||
datetime.date(2021, 1, 7): True, | ||
datetime.date(2021, 1, 17): False, | ||
}, | ||
arg3={'chosen_date': datetime.datetime(2021, 1, 1, 9, 30)}, | ||
) | ||
|
||
assert { | ||
'arg1': datetime.date(2021, 1, 1), | ||
'arg2': {datetime.date(2021, 1, 7): True, datetime.date(2021, 1, 17): False}, | ||
'arg3': {'chosen_date': datetime.datetime(2021, 1, 1, 9, 30)}, | ||
} == deserialized | ||
|
||
|
||
def test_deserialize_4(): | ||
@dataclass | ||
class MyChannel(Channel): | ||
arg1: List[datetime.date] | ||
arg2: Set[float] | ||
arg3: Tuple[str] | ||
|
||
deserialized = _deserialize( | ||
MyChannel, | ||
arg1=[datetime.date(2021, 1, 1), datetime.date(2021, 1, 2)], | ||
arg2={1.0, 2.1}, | ||
arg3=('hello', 'world'), | ||
) | ||
assert { | ||
'arg1': [datetime.date(2021, 1, 1), datetime.date(2021, 1, 2)], | ||
'arg2': {1.0, 2.1}, | ||
'arg3': ('hello', 'world'), | ||
} == deserialized | ||
|
||
|
||
def _deserialize(channel_cls, **kwargs): | ||
serialized = channel_cls(**kwargs).serialize() | ||
return channel_cls.deserialize(serialized) |
Oops, something went wrong.