File tree 3 files changed +43
-0
lines changed
3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -1695,6 +1695,9 @@ async def save(
1695
1695
self .check ()
1696
1696
db = self ._get_db (pipeline )
1697
1697
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 }
1698
1701
# TODO: Wrap any Redis response errors in a custom exception?
1699
1702
await db .hset (self .key (), mapping = document )
1700
1703
return self
Original file line number Diff line number Diff line change @@ -878,6 +878,26 @@ async def test_xfix_queries(members, m):
878
878
879
879
880
880
@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
+
881
901
async def test_update_validation ():
882
902
class TestUpdate (HashModel ):
883
903
name : str
Original file line number Diff line number Diff line change @@ -974,6 +974,26 @@ async def test_xfix_queries(m):
974
974
975
975
976
976
@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
+
977
997
async def test_update_validation ():
978
998
979
999
class Embedded (EmbeddedJsonModel ):
You can’t perform that action at this time.
0 commit comments