forked from tortoise/tortoise-orm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_json.py
More file actions
275 lines (243 loc) · 10.2 KB
/
test_json.py
File metadata and controls
275 lines (243 loc) · 10.2 KB
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
from tests import testmodels
from tortoise.contrib import test
from tortoise.exceptions import ConfigurationError, FieldError, IntegrityError
from tortoise.fields import JSONField
class TestJSONFields(test.TestCase):
async def test_empty(self):
with self.assertRaises(IntegrityError):
await testmodels.JSONFields.create()
async def test_create(self):
obj0 = await testmodels.JSONFields.create(data={"some": ["text", 3]})
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data, {"some": ["text", 3]})
self.assertEqual(obj.data_null, None)
await obj.save()
obj2 = await testmodels.JSONFields.get(id=obj.id)
self.assertEqual(obj, obj2)
async def test_error(self):
with self.assertRaises(FieldError):
await testmodels.JSONFields.create(data='{"some": ')
obj = await testmodels.JSONFields.create(data='{"some": ["text", 3]}')
with self.assertRaises(FieldError):
await testmodels.JSONFields.filter(pk=obj.pk).update(data='{"some": ')
with self.assertRaises(FieldError):
obj.data = "error json"
await obj.save()
async def test_update(self):
obj0 = await testmodels.JSONFields.create(data={"some": ["text", 3]})
await testmodels.JSONFields.filter(id=obj0.id).update(data={"other": ["text", 5]})
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data, {"other": ["text", 5]})
self.assertEqual(obj.data_null, None)
async def test_dict_str(self):
obj0 = await testmodels.JSONFields.create(data={"some": ["text", 3]})
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data, {"some": ["text", 3]})
await testmodels.JSONFields.filter(id=obj0.id).update(data='{"other": ["text", 5]}')
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data, {"other": ["text", 5]})
async def test_list_str(self):
obj = await testmodels.JSONFields.create(data='["text", 3]')
obj0 = await testmodels.JSONFields.get(id=obj.id)
self.assertEqual(obj0.data, ["text", 3])
await testmodels.JSONFields.filter(id=obj.id).update(data='["text", 5]')
obj0 = await testmodels.JSONFields.get(id=obj.id)
self.assertEqual(obj0.data, ["text", 5])
async def test_list(self):
obj0 = await testmodels.JSONFields.create(data=["text", 3])
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data, ["text", 3])
self.assertEqual(obj.data_null, None)
await obj.save()
obj2 = await testmodels.JSONFields.get(id=obj.id)
self.assertEqual(obj, obj2)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_list_contains(self):
await testmodels.JSONFields.create(data=["text", 3, {"msg": "msg2"}])
obj = await testmodels.JSONFields.filter(data__contains=[{"msg": "msg2"}]).first()
self.assertEqual(obj.data, ["text", 3, {"msg": "msg2"}])
await obj.save()
obj2 = await testmodels.JSONFields.get(id=obj.id)
self.assertEqual(obj, obj2)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_list_contained_by(self):
obj0 = await testmodels.JSONFields.create(data=["text"])
obj1 = await testmodels.JSONFields.create(data=["tortoise", "msg"])
obj2 = await testmodels.JSONFields.create(data=["tortoise"])
obj3 = await testmodels.JSONFields.create(data=["new_message", "some_message"])
objs = set(
await testmodels.JSONFields.filter(data__contained_by=["text", "tortoise", "msg"])
)
created_objs = {obj0, obj1, obj2}
self.assertSetEqual(created_objs, objs)
self.assertTrue(obj3 not in objs)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_filter(self):
obj0 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": "Bob",
"last": None,
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj1 = await testmodels.JSONFields.create(
data={
"breed": "husky",
"owner": {
"name": "Goldast",
"last": None,
"other_pets": [
{
"name": None,
}
],
},
}
)
obj = await testmodels.JSONFields.get(data__filter={"breed": "labrador"})
obj2 = await testmodels.JSONFields.get(data__filter={"owner__name": "Goldast"})
obj3 = await testmodels.JSONFields.get(data__filter={"owner__other_pets__0__name": "Fishy"})
self.assertEqual(obj0, obj)
self.assertEqual(obj1, obj2)
self.assertEqual(obj0, obj3)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_filter_not_condition(self):
obj0 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": "Bob",
"last": None,
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj1 = await testmodels.JSONFields.create(
data={
"breed": "husky",
"owner": {
"name": "Goldast",
"last": None,
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj2 = await testmodels.JSONFields.get(data__filter={"breed__not": "husky"})
obj3 = await testmodels.JSONFields.get(data__filter={"breed__not": "labrador"})
self.assertEqual(obj0, obj2)
self.assertEqual(obj1, obj3)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_filter_is_null_condition(self):
obj0 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": "Boby",
"last": "Cloud",
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj1 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": None,
"last": "Cloud",
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj2 = await testmodels.JSONFields.get(data__filter={"owner__name__isnull": False})
obj3 = await testmodels.JSONFields.get(data__filter={"owner__name__isnull": True})
self.assertEqual(obj0, obj2)
self.assertEqual(obj1, obj3)
@test.requireCapability(dialect="mysql")
@test.requireCapability(dialect="postgres")
async def test_filter_not_is_null_condition(self):
obj0 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": "Boby",
"last": "Cloud",
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj1 = await testmodels.JSONFields.create(
data={
"breed": "labrador",
"owner": {
"name": None,
"last": "Cloud",
"other_pets": [
{
"name": "Fishy",
}
],
},
}
)
obj2 = await testmodels.JSONFields.get(data__filter={"owner__name__not_isnull": True})
obj3 = await testmodels.JSONFields.get(data__filter={"owner__name__not_isnull": False})
self.assertEqual(obj0, obj2)
self.assertEqual(obj1, obj3)
async def test_values(self):
obj0 = await testmodels.JSONFields.create(data={"some": ["text", 3]})
values = await testmodels.JSONFields.filter(id=obj0.id).values("data")
self.assertEqual(values[0]["data"], {"some": ["text", 3]})
async def test_values_list(self):
obj0 = await testmodels.JSONFields.create(data={"some": ["text", 3]})
values = await testmodels.JSONFields.filter(id=obj0.id).values_list("data", flat=True)
self.assertEqual(values[0], {"some": ["text", 3]})
def test_unique_fail(self):
with self.assertRaisesRegex(ConfigurationError, "can't be indexed"):
JSONField(unique=True)
def test_index_fail(self):
with self.assertRaisesRegex(ConfigurationError, "can't be indexed"):
JSONField(index=True)
async def test_validate_str(self):
obj0 = await testmodels.JSONFields.create(data=[], data_validate='["text", 5]')
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data_validate, ["text", 5])
async def test_validate_dict(self):
obj0 = await testmodels.JSONFields.create(data=[], data_validate={"some": ["text", 3]})
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data_validate, {"some": ["text", 3]})
async def test_validate_list(self):
obj0 = await testmodels.JSONFields.create(data=[], data_validate=["text", 3])
obj = await testmodels.JSONFields.get(id=obj0.id)
self.assertEqual(obj.data_validate, ["text", 3])