@@ -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,13 @@ 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 (nickname = "fantix" , birthday = now , bio = "I code in Python and more." )
27
28
u .age += 1
28
29
assert await u .query .gino .model (None ).first () == (
29
30
1 ,
30
31
"fantix" ,
31
32
{"age" : 18 , "birthday" : now_str },
33
+ {"bio" : "I code in Python and more." , "height" : 170 },
32
34
UserType .USER ,
33
35
None ,
34
36
)
@@ -38,23 +40,27 @@ async def test_crud(bind):
38
40
assert u .birthday == now
39
41
assert u .age == 18
40
42
assert u .balance == 0
43
+ assert u .height == 170
44
+ assert u .bio == "I code in Python and more."
41
45
assert isinstance (u .balance , float )
42
46
assert await db .select ([User .birthday ]).where (User .id == u .id ).gino .scalar () == now
43
47
44
48
# In-memory update, not applying
45
49
u .update (birthday = now - timedelta (days = 3650 ))
46
50
47
51
# Update two JSON fields, one using expression
48
- await u .update (age = User .age - 2 , balance = 100.85 ).apply ()
52
+ await u .update (age = User .age - 2 , balance = 100.85 , height = 180 ).apply ()
49
53
50
54
assert u .birthday == now - timedelta (days = 3650 )
51
55
assert u .age == 16
52
56
assert u .balance == 100
57
+ assert u .height == 180
53
58
assert isinstance (u .balance , float )
54
59
assert await u .query .gino .model (None ).first () == (
55
60
1 ,
56
61
"fantix" ,
57
62
dict (age = 16 , balance = 100 , birthday = now_str ),
63
+ dict (bio = "I code in Python and more." , height = 180 ),
58
64
UserType .USER ,
59
65
None ,
60
66
)
@@ -63,12 +69,14 @@ async def test_crud(bind):
63
69
# Reload and test updating both JSON and regular property
64
70
u = await User .get (u .id )
65
71
await u .update (
66
- age = User .age - 2 , balance = 200.15 , realname = "daisy" , nickname = "daisy.nick"
72
+ age = User .age - 2 , balance = 200.15 , realname = "daisy" , nickname = "daisy.nick" , height = 185 , weight = 75
67
73
).apply ()
74
+ data = await u .query .gino .model (None ).first ()
68
75
assert await u .query .gino .model (None ).first () == (
69
76
1 ,
70
77
"daisy.nick" ,
71
78
dict (age = 14 , balance = 200 , realname = "daisy" , birthday = now_str ),
79
+ dict (bio = "I code in Python and more." , height = 185 , weight = 75 ),
72
80
UserType .USER ,
73
81
None ,
74
82
)
@@ -81,6 +89,9 @@ async def test_crud(bind):
81
89
realname = "daisy" ,
82
90
type = UserType .USER ,
83
91
team_id = None ,
92
+ bio = "I code in Python and more." ,
93
+ height = 185 ,
94
+ weight = 75
84
95
)
85
96
86
97
# Deleting property doesn't affect database
@@ -130,12 +141,16 @@ class News(db.Model):
130
141
# noinspection PyUnusedLocal
131
142
async def test_reload (bind ):
132
143
u = await User .create ()
133
- await u .update (realname = db .cast ("888" , db .Unicode )).apply ()
144
+ await u .update (realname = db .cast ("888" , db .Unicode ), weight = 75 ).apply ()
134
145
assert u .realname == "888"
135
- await u .update (profile = None ).apply ()
146
+ assert u .weight == 75
147
+ await u .update (profile = None , parameter = None ).apply ()
136
148
assert u .realname == "888"
149
+ assert u .weight == 75
137
150
User .__dict__ ["realname" ].reload (u )
151
+ User .__dict__ ["weight" ].reload (u )
138
152
assert u .realname is None
153
+ assert u .weight is None
139
154
140
155
141
156
# noinspection PyUnusedLocal
@@ -151,29 +166,60 @@ class PropsTest(db.Model):
151
166
obj = db .ObjectProperty ()
152
167
arr = db .ArrayProperty ()
153
168
169
+ parameter = db .Column (JSONB (), nullable = False , server_default = "{}" )
170
+
171
+ raw_param = db .JSONProperty (prop_name = 'parameter' )
172
+ bool_param = db .BooleanProperty (prop_name = 'parameter' )
173
+ obj_param = db .ObjectProperty (prop_name = 'parameter' )
174
+ arr_param = db .ArrayProperty (prop_name = 'parameter' )
175
+
154
176
await PropsTest .gino .create ()
155
177
try :
156
178
t = await PropsTest .create (
157
- raw = dict (a = [1 , 2 ]), bool = True , obj = dict (x = 1 , y = 2 ), arr = [3 , 4 , 5 , 6 ],
179
+ raw = dict (a = [1 , 2 ]),
180
+ bool = True ,
181
+ obj = dict (x = 1 , y = 2 ),
182
+ arr = [3 , 4 , 5 , 6 ],
183
+
184
+ raw_param = dict (a = [3 , 4 ]),
185
+ bool_param = False ,
186
+ obj_param = dict (x = 3 , y = 4 ),
187
+ arr_param = [7 , 8 , 9 , 10 ],
158
188
)
159
189
assert t .obj ["x" ] == 1
190
+ assert t .obj_param ["x" ] == 3
160
191
assert t .arr [- 1 ] == 6
192
+ assert t .arr_param [- 1 ] == 10
193
+ data = await db .select (
194
+ [PropsTest .profile , PropsTest .parameter , PropsTest .raw , PropsTest .bool , PropsTest .obj_param ]
195
+ ).gino .first ()
161
196
assert await db .select (
162
- [PropsTest .profile , PropsTest .raw , PropsTest .bool ,]
197
+ [PropsTest .profile , PropsTest .parameter , PropsTest . raw , PropsTest .bool , PropsTest . obj_param ]
163
198
).gino .first () == (
164
199
{
165
200
"arr" : [3 , 4 , 5 , 6 ],
166
201
"obj" : {"x" : 1 , "y" : 2 },
167
202
"raw" : {"a" : [1 , 2 ]},
168
203
"bool" : True ,
169
204
},
205
+ {
206
+ "arr_param" : [7 , 8 , 9 , 10 ],
207
+ "obj_param" : {"x" : 3 , "y" : 4 },
208
+ "raw_param" : {"a" : [3 , 4 ]},
209
+ "bool_param" : False ,
210
+ },
170
211
dict (a = [1 , 2 ]),
171
212
True ,
213
+ dict (x = 3 , y = 4 ),
172
214
)
173
215
t .obj = dict (x = 10 , y = 20 )
216
+ t .obj_param = dict (x = 30 , y = 45 )
174
217
assert t .obj ["x" ] == 10
218
+ assert t .obj_param ["y" ] == 45
175
219
t .arr = [4 , 5 , 6 , 7 ]
220
+ t .arr_param = [11 , 12 , 13 , 14 , 15 ]
176
221
assert t .arr [- 1 ] == 7
222
+ assert t .arr_param [- 1 ] == 15
177
223
finally :
178
224
await PropsTest .gino .drop ()
179
225
0 commit comments