Skip to content

Commit a243582

Browse files
authored
fixing None types in hashes (#616)
1 parent 75fa446 commit a243582

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

aredis_om/model/model.py

+3
Original file line numberDiff line numberDiff line change
@@ -1695,6 +1695,9 @@ async def save(
16951695
self.check()
16961696
db = self._get_db(pipeline)
16971697
document = jsonable_encoder(self.dict())
1698+
1699+
# filter out values which are `None` because they are not valid in a HSET
1700+
document = {k: v for k, v in document.items() if v is not None}
16981701
# TODO: Wrap any Redis response errors in a custom exception?
16991702
await db.hset(self.key(), mapping=document)
17001703
return self

tests/test_hash_model.py

+20
Original file line numberDiff line numberDiff line change
@@ -878,6 +878,26 @@ async def test_xfix_queries(members, m):
878878

879879

880880
@py_test_mark_asyncio
881+
async def test_none():
882+
class ModelWithNoneDefault(HashModel):
883+
test: Optional[str] = Field(index=True, default=None)
884+
885+
class ModelWithStringDefault(HashModel):
886+
test: Optional[str] = Field(index=True, default="None")
887+
888+
await Migrator().run()
889+
890+
a = ModelWithNoneDefault()
891+
await a.save()
892+
res = await ModelWithNoneDefault.find(ModelWithNoneDefault.pk == a.pk).first()
893+
assert res.test is None
894+
895+
b = ModelWithStringDefault()
896+
await b.save()
897+
res = await ModelWithStringDefault.find(ModelWithStringDefault.pk == b.pk).first()
898+
assert res.test == "None"
899+
900+
881901
async def test_update_validation():
882902
class TestUpdate(HashModel):
883903
name: str

tests/test_json_model.py

+20
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,26 @@ async def test_xfix_queries(m):
974974

975975

976976
@py_test_mark_asyncio
977+
async def test_none():
978+
class ModelWithNoneDefault(JsonModel):
979+
test: Optional[str] = Field(index=True, default=None)
980+
981+
class ModelWithStringDefault(JsonModel):
982+
test: Optional[str] = Field(index=True, default="None")
983+
984+
await Migrator().run()
985+
986+
a = ModelWithNoneDefault()
987+
await a.save()
988+
res = await ModelWithNoneDefault.find(ModelWithNoneDefault.pk == a.pk).first()
989+
assert res.test is None
990+
991+
b = ModelWithStringDefault()
992+
await b.save()
993+
res = await ModelWithStringDefault.find(ModelWithStringDefault.pk == b.pk).first()
994+
assert res.test == "None"
995+
996+
977997
async def test_update_validation():
978998

979999
class Embedded(EmbeddedJsonModel):

0 commit comments

Comments
 (0)