@@ -14,6 +14,7 @@ async def test_in_memory():
14
14
u .age += 10
15
15
assert u .age == 28
16
16
assert u .balance == 0
17
+ assert u .height == 170
17
18
assert isinstance (u .balance , float )
18
19
19
20
@@ -23,12 +24,15 @@ async def test_crud(bind):
23
24
24
25
now = datetime .utcnow ()
25
26
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
+ )
27
30
u .age += 1
28
31
assert await u .query .gino .model (None ).first () == (
29
32
1 ,
30
33
"fantix" ,
31
34
{"age" : 18 , "birthday" : now_str },
35
+ {"bio" : "I code in Python and more." , "height" : 170 },
32
36
UserType .USER ,
33
37
None ,
34
38
)
@@ -38,23 +42,27 @@ async def test_crud(bind):
38
42
assert u .birthday == now
39
43
assert u .age == 18
40
44
assert u .balance == 0
45
+ assert u .height == 170
46
+ assert u .bio == "I code in Python and more."
41
47
assert isinstance (u .balance , float )
42
48
assert await db .select ([User .birthday ]).where (User .id == u .id ).gino .scalar () == now
43
49
44
50
# In-memory update, not applying
45
51
u .update (birthday = now - timedelta (days = 3650 ))
46
52
47
53
# 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 ()
49
55
50
56
assert u .birthday == now - timedelta (days = 3650 )
51
57
assert u .age == 16
52
58
assert u .balance == 100
59
+ assert u .height == 180
53
60
assert isinstance (u .balance , float )
54
61
assert await u .query .gino .model (None ).first () == (
55
62
1 ,
56
63
"fantix" ,
57
64
dict (age = 16 , balance = 100 , birthday = now_str ),
65
+ dict (bio = "I code in Python and more." , height = 180 ),
58
66
UserType .USER ,
59
67
None ,
60
68
)
@@ -63,12 +71,19 @@ async def test_crud(bind):
63
71
# Reload and test updating both JSON and regular property
64
72
u = await User .get (u .id )
65
73
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 ,
67
80
).apply ()
81
+ data = await u .query .gino .model (None ).first ()
68
82
assert await u .query .gino .model (None ).first () == (
69
83
1 ,
70
84
"daisy.nick" ,
71
85
dict (age = 14 , balance = 200 , realname = "daisy" , birthday = now_str ),
86
+ dict (bio = "I code in Python and more." , height = 185 , weight = 75 ),
72
87
UserType .USER ,
73
88
None ,
74
89
)
@@ -81,6 +96,9 @@ async def test_crud(bind):
81
96
realname = "daisy" ,
82
97
type = UserType .USER ,
83
98
team_id = None ,
99
+ bio = "I code in Python and more." ,
100
+ height = 185 ,
101
+ weight = 75 ,
84
102
)
85
103
86
104
# Deleting property doesn't affect database
@@ -130,12 +148,16 @@ class News(db.Model):
130
148
# noinspection PyUnusedLocal
131
149
async def test_reload (bind ):
132
150
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 ()
134
152
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 ()
136
155
assert u .realname == "888"
156
+ assert u .weight == 75
137
157
User .__dict__ ["realname" ].reload (u )
158
+ User .__dict__ ["weight" ].reload (u )
138
159
assert u .realname is None
160
+ assert u .weight is None
139
161
140
162
141
163
# noinspection PyUnusedLocal
@@ -151,29 +173,71 @@ class PropsTest(db.Model):
151
173
obj = db .ObjectProperty ()
152
174
arr = db .ArrayProperty ()
153
175
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
+
154
183
await PropsTest .gino .create ()
155
184
try :
156
185
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 ],
158
194
)
159
195
assert t .obj ["x" ] == 1
196
+ assert t .obj_param ["x" ] == 3
160
197
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 ()
161
208
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
+ ]
163
216
).gino .first () == (
164
217
{
165
218
"arr" : [3 , 4 , 5 , 6 ],
166
219
"obj" : {"x" : 1 , "y" : 2 },
167
220
"raw" : {"a" : [1 , 2 ]},
168
221
"bool" : True ,
169
222
},
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
+ },
170
229
dict (a = [1 , 2 ]),
171
230
True ,
231
+ dict (x = 3 , y = 4 ),
172
232
)
173
233
t .obj = dict (x = 10 , y = 20 )
234
+ t .obj_param = dict (x = 30 , y = 45 )
174
235
assert t .obj ["x" ] == 10
236
+ assert t .obj_param ["y" ] == 45
175
237
t .arr = [4 , 5 , 6 , 7 ]
238
+ t .arr_param = [11 , 12 , 13 , 14 , 15 ]
176
239
assert t .arr [- 1 ] == 7
240
+ assert t .arr_param [- 1 ] == 15
177
241
finally :
178
242
await PropsTest .gino .drop ()
179
243
0 commit comments