Skip to content

Commit 5a01b1d

Browse files
committed
adding tests for #591
1 parent 1571c4a commit 5a01b1d

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

tests/test_hash_model.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import datetime
66
import decimal
77
from collections import namedtuple
8-
from typing import Dict, List, Optional, Set
8+
from typing import Dict, List, Optional, Set, Union
99
from unittest import mock
1010

1111
import pytest
@@ -807,3 +807,26 @@ async def test_count(members, m):
807807
m.Member.first_name == "Kim", m.Member.last_name == "Brookins"
808808
).count()
809809
assert actual_count == 1
810+
811+
812+
@py_test_mark_asyncio
813+
async def test_type_with_union(members, m):
814+
class TypeWithUnion(m.BaseHashModel):
815+
field: Union[str, int]
816+
817+
twu_str = TypeWithUnion(field="hello world")
818+
res = await twu_str.save()
819+
assert res.pk == twu_str.pk
820+
twu_str_rematerialized = await TypeWithUnion.get(twu_str.pk)
821+
assert (
822+
isinstance(twu_str_rematerialized.field, str)
823+
and twu_str_rematerialized.pk == twu_str.pk
824+
)
825+
826+
twu_int = TypeWithUnion(field=42)
827+
await twu_int.save()
828+
twu_int_rematerialized = await TypeWithUnion.get(twu_int.pk)
829+
830+
# Note - we will not be able to automatically serialize an int back to this union type,
831+
# since as far as we know from Redis this item is a string
832+
assert twu_int_rematerialized.pk == twu_int.pk

tests/test_json_model.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import datetime
66
import decimal
77
from collections import namedtuple
8-
from typing import Dict, List, Optional, Set
8+
from typing import Dict, List, Optional, Set, Union
99
from unittest import mock
1010

1111
import pytest
@@ -880,3 +880,26 @@ async def test_count(members, m):
880880
m.Member.first_name == "Kim", m.Member.last_name == "Brookins"
881881
).count()
882882
assert actual_count == 1
883+
884+
885+
@py_test_mark_asyncio
886+
async def test_type_with_union(members, m):
887+
class TypeWithUnion(m.BaseJsonModel):
888+
field: Union[str, int]
889+
890+
twu_str = TypeWithUnion(field="hello world")
891+
res = await twu_str.save()
892+
assert res.pk == twu_str.pk
893+
twu_str_rematerialized = await TypeWithUnion.get(twu_str.pk)
894+
assert (
895+
isinstance(twu_str_rematerialized.field, str)
896+
and twu_str_rematerialized.pk == twu_str.pk
897+
)
898+
899+
twu_int = TypeWithUnion(field=42)
900+
await twu_int.save()
901+
twu_int_rematerialized = await TypeWithUnion.get(twu_int.pk)
902+
assert (
903+
isinstance(twu_int_rematerialized.field, int)
904+
and twu_int_rematerialized.pk == twu_int.pk
905+
)

0 commit comments

Comments
 (0)