@@ -14,6 +14,7 @@ async def test_in_memory():
1414 u .age += 10
1515 assert u .age == 28
1616 assert u .balance == 0
17+ assert u .height == 170
1718 assert isinstance (u .balance , float )
1819
1920
@@ -23,12 +24,15 @@ async def test_crud(bind):
2324
2425 now = datetime .utcnow ()
2526 now_str = now .strftime (DATETIME_FORMAT )
26- u = await User .create (nickname = "fantix" , birthday = now )
27+ u = await User .create (
28+ nickname = "fantix" , birthday = now , bio = "I code in Python and more."
29+ )
2730 u .age += 1
2831 assert await u .query .gino .model (None ).first () == (
2932 1 ,
3033 "fantix" ,
3134 {"age" : 18 , "birthday" : now_str },
35+ {"bio" : "I code in Python and more." , "height" : 170 },
3236 UserType .USER ,
3337 None ,
3438 )
@@ -38,23 +42,27 @@ async def test_crud(bind):
3842 assert u .birthday == now
3943 assert u .age == 18
4044 assert u .balance == 0
45+ assert u .height == 170
46+ assert u .bio == "I code in Python and more."
4147 assert isinstance (u .balance , float )
4248 assert await db .select ([User .birthday ]).where (User .id == u .id ).gino .scalar () == now
4349
4450 # In-memory update, not applying
4551 u .update (birthday = now - timedelta (days = 3650 ))
4652
4753 # Update two JSON fields, one using expression
48- await u .update (age = User .age - 2 , balance = 100.85 ).apply ()
54+ await u .update (age = User .age - 2 , balance = 100.85 , height = 180 ).apply ()
4955
5056 assert u .birthday == now - timedelta (days = 3650 )
5157 assert u .age == 16
5258 assert u .balance == 100
59+ assert u .height == 180
5360 assert isinstance (u .balance , float )
5461 assert await u .query .gino .model (None ).first () == (
5562 1 ,
5663 "fantix" ,
5764 dict (age = 16 , balance = 100 , birthday = now_str ),
65+ dict (bio = "I code in Python and more." , height = 180 ),
5866 UserType .USER ,
5967 None ,
6068 )
@@ -63,12 +71,19 @@ async def test_crud(bind):
6371 # Reload and test updating both JSON and regular property
6472 u = await User .get (u .id )
6573 await u .update (
66- age = User .age - 2 , balance = 200.15 , realname = "daisy" , nickname = "daisy.nick"
74+ age = User .age - 2 ,
75+ balance = 200.15 ,
76+ realname = "daisy" ,
77+ nickname = "daisy.nick" ,
78+ height = 185 ,
79+ weight = 75 ,
6780 ).apply ()
81+ data = await u .query .gino .model (None ).first ()
6882 assert await u .query .gino .model (None ).first () == (
6983 1 ,
7084 "daisy.nick" ,
7185 dict (age = 14 , balance = 200 , realname = "daisy" , birthday = now_str ),
86+ dict (bio = "I code in Python and more." , height = 185 , weight = 75 ),
7287 UserType .USER ,
7388 None ,
7489 )
@@ -81,6 +96,9 @@ async def test_crud(bind):
8196 realname = "daisy" ,
8297 type = UserType .USER ,
8398 team_id = None ,
99+ bio = "I code in Python and more." ,
100+ height = 185 ,
101+ weight = 75 ,
84102 )
85103
86104 # Deleting property doesn't affect database
@@ -130,12 +148,16 @@ class News(db.Model):
130148# noinspection PyUnusedLocal
131149async def test_reload (bind ):
132150 u = await User .create ()
133- await u .update (realname = db .cast ("888" , db .Unicode )).apply ()
151+ await u .update (realname = db .cast ("888" , db .Unicode ), weight = 75 ).apply ()
134152 assert u .realname == "888"
135- await u .update (profile = None ).apply ()
153+ assert u .weight == 75
154+ await u .update (profile = None , parameter = None ).apply ()
136155 assert u .realname == "888"
156+ assert u .weight == 75
137157 User .__dict__ ["realname" ].reload (u )
158+ User .__dict__ ["weight" ].reload (u )
138159 assert u .realname is None
160+ assert u .weight is None
139161
140162
141163# noinspection PyUnusedLocal
@@ -151,29 +173,71 @@ class PropsTest(db.Model):
151173 obj = db .ObjectProperty ()
152174 arr = db .ArrayProperty ()
153175
176+ parameter = db .Column (JSONB (), nullable = False , server_default = "{}" )
177+
178+ raw_param = db .JSONProperty (prop_name = "parameter" )
179+ bool_param = db .BooleanProperty (prop_name = "parameter" )
180+ obj_param = db .ObjectProperty (prop_name = "parameter" )
181+ arr_param = db .ArrayProperty (prop_name = "parameter" )
182+
154183 await PropsTest .gino .create ()
155184 try :
156185 t = await PropsTest .create (
157- raw = dict (a = [1 , 2 ]), bool = True , obj = dict (x = 1 , y = 2 ), arr = [3 , 4 , 5 , 6 ],
186+ raw = dict (a = [1 , 2 ]),
187+ bool = True ,
188+ obj = dict (x = 1 , y = 2 ),
189+ arr = [3 , 4 , 5 , 6 ],
190+ raw_param = dict (a = [3 , 4 ]),
191+ bool_param = False ,
192+ obj_param = dict (x = 3 , y = 4 ),
193+ arr_param = [7 , 8 , 9 , 10 ],
158194 )
159195 assert t .obj ["x" ] == 1
196+ assert t .obj_param ["x" ] == 3
160197 assert t .arr [- 1 ] == 6
198+ assert t .arr_param [- 1 ] == 10
199+ data = await db .select (
200+ [
201+ PropsTest .profile ,
202+ PropsTest .parameter ,
203+ PropsTest .raw ,
204+ PropsTest .bool ,
205+ PropsTest .obj_param ,
206+ ]
207+ ).gino .first ()
161208 assert await db .select (
162- [PropsTest .profile , PropsTest .raw , PropsTest .bool ,]
209+ [
210+ PropsTest .profile ,
211+ PropsTest .parameter ,
212+ PropsTest .raw ,
213+ PropsTest .bool ,
214+ PropsTest .obj_param ,
215+ ]
163216 ).gino .first () == (
164217 {
165218 "arr" : [3 , 4 , 5 , 6 ],
166219 "obj" : {"x" : 1 , "y" : 2 },
167220 "raw" : {"a" : [1 , 2 ]},
168221 "bool" : True ,
169222 },
223+ {
224+ "arr_param" : [7 , 8 , 9 , 10 ],
225+ "obj_param" : {"x" : 3 , "y" : 4 },
226+ "raw_param" : {"a" : [3 , 4 ]},
227+ "bool_param" : False ,
228+ },
170229 dict (a = [1 , 2 ]),
171230 True ,
231+ dict (x = 3 , y = 4 ),
172232 )
173233 t .obj = dict (x = 10 , y = 20 )
234+ t .obj_param = dict (x = 30 , y = 45 )
174235 assert t .obj ["x" ] == 10
236+ assert t .obj_param ["y" ] == 45
175237 t .arr = [4 , 5 , 6 , 7 ]
238+ t .arr_param = [11 , 12 , 13 , 14 , 15 ]
176239 assert t .arr [- 1 ] == 7
240+ assert t .arr_param [- 1 ] == 15
177241 finally :
178242 await PropsTest .gino .drop ()
179243
0 commit comments