-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathtest_json.py
253 lines (209 loc) · 7.5 KB
/
test_json.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
import pytest
from datetime import datetime, timedelta
from gino.exceptions import UnknownJSONPropertyError
from .models import db, User, UserType
pytestmark = pytest.mark.asyncio
async def test_in_memory():
u = User()
assert u.age == 18
u.age += 10
assert u.age == 28
assert u.balance == 0
assert isinstance(u.balance, float)
# noinspection PyUnusedLocal
async def test_crud(bind):
from gino.json_support import DATETIME_FORMAT
now = datetime.utcnow()
now_str = now.strftime(DATETIME_FORMAT)
u = await User.create(nickname="fantix", birthday=now)
u.age += 1
assert await u.query.gino.model(None).first() == (
1,
"fantix",
{"age": 18, "birthday": now_str},
UserType.USER,
None,
)
u = await User.get(u.id)
assert u.nickname == "fantix"
assert u.birthday == now
assert u.age == 18
assert u.balance == 0
assert isinstance(u.balance, float)
assert await db.select([User.birthday]).where(User.id == u.id).gino.scalar() == now
# In-memory update, not applying
u.update(birthday=now - timedelta(days=3650))
# Update two JSON fields, one using expression
await u.update(age=User.age - 2, balance=100.85).apply()
assert u.birthday == now - timedelta(days=3650)
assert u.age == 16
assert u.balance == 100
assert isinstance(u.balance, float)
assert await u.query.gino.model(None).first() == (
1,
"fantix",
dict(age=16, balance=100, birthday=now_str),
UserType.USER,
None,
)
assert await db.select([User.realname]).where(User.id == u.id).gino.scalar() is None
# Reload and test updating both JSON and regular property
u = await User.get(u.id)
await u.update(
age=User.age - 2, balance=200.15, realname="daisy", nickname="daisy.nick"
).apply()
assert await u.query.gino.model(None).first() == (
1,
"daisy.nick",
dict(age=14, balance=200, realname="daisy", birthday=now_str),
UserType.USER,
None,
)
assert u.to_dict() == dict(
age=14,
balance=200.0,
birthday=now,
id=1,
nickname="daisy.nick",
realname="daisy",
type=UserType.USER,
team_id=None,
)
# Deleting property doesn't affect database
assert u.balance == 200
u.balance = 300
assert u.balance == 300
del u.balance
assert u.balance == 0
assert await db.select([User.balance]).where(User.id == u.id).gino.scalar() == 200
await u.update(age=22).apply()
assert u.balance == 0
assert await db.select([User.balance]).where(User.id == u.id).gino.scalar() == 200
await u.update(balance=None).apply()
assert u.balance == 0
assert await db.select([User.balance]).where(User.id == u.id).gino.scalar() is None
# noinspection PyUnusedLocal
async def test_reload(bind):
u = await User.create()
await u.update(realname=db.cast("888", db.Unicode)).apply()
assert u.realname == "888"
await u.update(profile=None).apply()
assert u.realname == "888"
User.__dict__["realname"].reload(u)
assert u.realname is None
# noinspection PyUnusedLocal
async def test_properties(bind):
from gino.dialects.aiomysql import JSON
class PropsTest(db.Model):
__tablename__ = "props_test"
profile = db.Column(JSON(), nullable=False, default="{}")
raw = db.JSONProperty()
bool = db.BooleanProperty()
obj = db.ObjectProperty()
arr = db.ArrayProperty()
await PropsTest.gino.create()
try:
t = await PropsTest.create(
raw=dict(a=[1, 2]), bool=True, obj=dict(x=1, y=2), arr=[3, 4, 5, 6],
)
assert t.obj["x"] == 1
assert t.arr[-1] == 6
assert await db.select(
[PropsTest.profile, PropsTest.raw, PropsTest.bool]
).gino.first() == (
{
"arr": [3, 4, 5, 6],
"obj": {"x": 1, "y": 2},
"raw": {"a": [1, 2]},
"bool": True,
},
dict(a=[1, 2]),
True,
)
t.obj = dict(x=10, y=20)
assert t.obj["x"] == 10
t.arr = [4, 5, 6, 7]
assert t.arr[-1] == 7
finally:
await PropsTest.gino.drop()
# noinspection PyUnusedLocal
async def test_unknown_properties(bind):
from gino.dialects.aiomysql import JSON
class PropsTest1(db.Model):
__tablename__ = "props_test1"
profile = db.Column(JSON(), nullable=False, default="{}")
bool = db.BooleanProperty()
await PropsTest1.gino.create()
try:
# bool1 is not defined in the model
t = await PropsTest1.create(profile=dict(bool1=True))
with pytest.raises(UnknownJSONPropertyError, match=r"bool1.*profile"):
t.to_dict()
finally:
await PropsTest1.gino.drop()
async def test_property_in_profile_and_attribute_collide(bind):
from gino.dialects.aiomysql import JSON
class PropsTest2(db.Model):
__tablename__ = "props_test2"
profile = db.Column(JSON(), nullable=False, default="{}")
bool_profile = db.BooleanProperty()
bool_attr = db.Column(db.Boolean)
await PropsTest2.gino.create()
try:
await PropsTest2.create(
profile={"bool_attr": False, "bool_profile": True}, bool_attr=True
)
# bool_attr is defined in the model
# bool_profile is defined as json property
t2 = await PropsTest2.query.gino.first()
assert t2.bool_attr is True
with pytest.raises(UnknownJSONPropertyError, match=r"bool_attr"):
assert t2.bool_profile is True
finally:
await PropsTest2.gino.drop()
async def test_no_profile():
with pytest.raises(AttributeError, match=r"JSON\[B\] column"):
# noinspection PyUnusedLocal
class Test(db.Model):
__tablename__ = "tests_no_profile"
id = db.Column(db.BigInteger(), primary_key=True)
age = db.IntegerProperty(default=18)
async def test_t291_t402(bind):
from gino.dialects.aiomysql import JSON
class CustomJSON(db.TypeDecorator):
impl = JSON
def process_result_value(self, *_):
return 123
class PropsTest(db.Model):
__tablename__ = "props_test_291"
profile = db.Column(JSON(), nullable=False, default={})
profile1 = db.Column(JSON(), nullable=False, default={})
profile2 = db.Column(CustomJSON(), nullable=False, default={})
bool = db.BooleanProperty()
bool1 = db.BooleanProperty(prop_name="profile1")
await PropsTest.gino.create()
try:
await PropsTest.create(bool=True, bool1=True)
profile1 = await bind.scalar("SELECT profile1 FROM props_test_291")
assert isinstance(profile1, dict)
profile2 = await bind.scalar("SELECT profile2 FROM props_test_291")
assert isinstance(profile2, dict)
custom_profile2 = await bind.scalar(PropsTest.select("profile2"))
assert isinstance(custom_profile2, int)
assert custom_profile2 == 123
finally:
await PropsTest.gino.drop()
async def test_json_path(bind):
from gino.dialects.aiomysql import JSON
class PathTest(db.Model):
__tablename__ = "path_test_json_path"
data = db.Column(JSON())
await PathTest.gino.create()
try:
t1 = await PathTest.create(data=dict(a=dict(b="c")))
t2 = await PathTest.query.where(
PathTest.data[("a", "b")] == "c"
).gino.first()
assert t1.data == t2.data
finally:
await PathTest.gino.drop()