-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathtest_3500_json.py
366 lines (342 loc) · 13.3 KB
/
test_3500_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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# -----------------------------------------------------------------------------
# Copyright (c) 2020, 2024, Oracle and/or its affiliates.
#
# This software is dual-licensed to you under the Universal Permissive License
# (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl and Apache License
# 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose
# either license.
#
# If you elect to accept the software under the Apache License, Version 2.0,
# the following applies:
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# -----------------------------------------------------------------------------
"""
3500 - Module for testing the JSON data type.
"""
import datetime
import decimal
import unittest
import oracledb
import test_env
@unittest.skipUnless(
test_env.get_client_version() >= (21, 0), "unsupported client"
)
@unittest.skipUnless(
test_env.get_server_version() >= (21, 0), "unsupported server"
)
class TestCase(test_env.BaseTestCase):
json_data = [
True,
False,
"String",
b"Some Bytes",
{},
{"name": None},
{"name": "John"},
{"age": 30},
{"Permanent": True},
{
"employee": {
"name": "John",
"age": 30,
"city": "Delhi",
"Parmanent": True,
}
},
{"employees": ["John", "Matthew", "James"]},
{
"employees": [
{"employee1": {"name": "John", "city": "Delhi"}},
{"employee2": {"name": "Matthew", "city": "Mumbai"}},
{"employee3": {"name": "James", "city": "Bangalore"}},
]
},
]
def __bind_scalar_as_json(self, data):
self.cursor.execute("delete from TestJson")
out_var = self.cursor.var(oracledb.DB_TYPE_JSON, arraysize=len(data))
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON, out_var)
bind_data = list(enumerate(data))
self.cursor.executemany(
"""
insert into TestJson values (:1, :2)
returning JsonCol into :json_out
""",
bind_data,
)
self.conn.commit()
self.assertEqual(out_var.values, [[value] for value in data])
def test_3500(self):
"3500 - insert and fetch single row with JSON"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.execute(
"insert into TestJson values (:1, :2)", [1, self.json_data]
)
self.cursor.execute("select JsonCol from TestJson")
(result,) = self.cursor.fetchone()
self.assertEqual(result, self.json_data)
def test_3501(self):
"3501 - inserting single rows with JSON and DML returning"
json_val = self.json_data[11]
self.cursor.execute("delete from TestJson")
json_out = self.cursor.var(oracledb.DB_TYPE_JSON)
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON, json_out)
self.cursor.execute(
"""
insert into TestJson
values (:1, :2)
returning JsonCol into :json_out
""",
[1, json_val],
)
self.assertEqual(json_out.getvalue(0), [json_val])
def test_3502(self):
"3502 - insert and fetch multiple rows with JSON"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
data = list(enumerate(self.json_data))
self.cursor.executemany("insert into TestJson values(:1, :2)", data)
self.cursor.execute("select * from TestJson")
self.assertEqual(self.cursor.fetchall(), data)
def test_3503(self):
"3503 - inserting multiple rows with JSON and DML returning"
self.cursor.execute("delete from TestJson")
int_values = [i for i in range(len(self.json_data))]
out_int_var = self.cursor.var(int, arraysize=len(int_values))
out_json_var = self.cursor.var(
oracledb.DB_TYPE_JSON, arraysize=len(int_values)
)
self.cursor.setinputsizes(
None, oracledb.DB_TYPE_JSON, out_int_var, out_json_var
)
data = list(zip(int_values, self.json_data))
self.cursor.executemany(
"""
insert into TestJson
values(:int_val, :json_val)
returning IntCol, JsonCol into :int_var, :json_var
""",
data,
)
self.assertEqual(out_int_var.values, [[v] for v in int_values])
self.assertEqual(out_json_var.values, [[v] for v in self.json_data])
def test_3504(self):
"3504 - test binding boolean values as scalar JSON values"
data = [True, False, True, True, False, True]
self.__bind_scalar_as_json(data)
def test_3505(self):
"3505 - test binding strings/bytes values as scalar JSON values"
data = [
"String 1",
b"A raw value",
"A much longer string",
b"A much longer RAW value",
"Short string",
b"Y",
]
self.__bind_scalar_as_json(data)
def test_3506(self):
"3506 - test binding dates/intervals as scalar JSON values"
data = [
datetime.datetime.today(),
datetime.datetime(2004, 2, 1, 3, 4, 5),
datetime.datetime(2020, 12, 2, 13, 29, 14),
datetime.timedelta(8.5),
datetime.datetime(2002, 12, 13, 9, 36, 0),
oracledb.Timestamp(2002, 12, 13, 9, 36, 0),
datetime.datetime(2002, 12, 13),
]
self.__bind_scalar_as_json(data)
def test_3507(self):
"3507 - test binding number in json values"
data = [
0,
1,
25.25,
6088343244,
-9999999999999999999,
decimal.Decimal("0.25"),
decimal.Decimal("10.25"),
decimal.Decimal("319438950232418390.273596"),
]
self.__bind_scalar_as_json(data)
def test_3508(self):
"3508 - test binding unsupported python type with JSON"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
insert_sql = "insert into TestJson values (:1, :2)"
with self.assertRaisesFullCode("DPY-3003"):
self.cursor.execute(insert_sql, [1, list])
def test_3509(self):
"3509 - test fetching an unsupported python type with JSON"
self.cursor.prefetchrows = 0
self.cursor.execute(
"select json(json_scalar(to_yminterval('8-04'))) from dual"
)
with self.assertRaisesFullCode("DPY-3007"):
self.cursor.fetchone()
def test_3510(self):
"3510 - fetch all supported types"
sql = """
select json('{
"binary_float": {"$numberFloat": 38.75},
"binary_double": {"$numberDouble": 125.875},
"date_no_time": {"$oracleDate": "2022-12-05"},
"date_with_time": {"$oracleDate": "2022-12-05T15:06:05"},
"empty_string": "",
"explicit_long": {"$numberLong": 9223372036854775807},
"false": false,
"interval_ds": {"$intervalDaySecond" : "P133DT2H5M8.123S"},
"long_integer": 12345678901234567890123456789012345,
"null": null,
"short_decimal": {"$numberDecimal": 18.25},
"short_integer": {"$numberInt": 5 },
"short_raw": {"$rawhex": "73686f72745f726177"},
"short_string": "A short string",
"small_integer": 1234,
"small_float": 25.25,
"string_uint8": "A longer string but still < 256 bytes",
"true": true,
"ts_no_fs": {"$oracleTimestamp": "2022-12-06T18:12:35"},
"ts_tz": {"$oracleTimestampTZ": "2022-12-07T22:59:15.1234Z"},
"ts_with_fs": {"$oracleTimestamp": "2022-12-06T18:12:35.123"}
}'
extended) from dual"""
expected_data = dict(
binary_float=38.75,
binary_double=125.875,
date_no_time=datetime.datetime(2022, 12, 5),
date_with_time=datetime.datetime(2022, 12, 5, 15, 6, 5),
empty_string="",
explicit_long=9223372036854775807,
false=False,
interval_ds=datetime.timedelta(
days=133, seconds=7508, microseconds=123000
),
null=None,
long_integer=12345678901234567890123456789012345,
short_decimal=18.25,
short_integer=5,
short_raw=b"short_raw",
short_string="A short string",
small_integer=1234,
small_float=25.25,
string_uint8="A longer string but still < 256 bytes",
true=True,
ts_no_fs=datetime.datetime(2022, 12, 6, 18, 12, 35),
ts_tz=datetime.datetime(2022, 12, 7, 22, 59, 15, 123400),
ts_with_fs=datetime.datetime(2022, 12, 6, 18, 12, 35, 123000),
)
self.cursor.execute(sql)
(actual_data,) = self.cursor.fetchone()
self.assertEqual(actual_data, expected_data)
def test_3511(self):
"3511 - test inserting and updating JSON"
self.cursor.execute("delete from TestJSON")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"insert into TestJSON values (:1, :2)",
list(enumerate(self.json_data)),
)
data = [({"a": i}, i) for i in range(len(self.json_data))]
self.cursor.setinputsizes(oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"update TestJSON set JsonCol = :1 where IntCol = :2",
data,
)
self.cursor.execute(
"select JsonCol, IntCol from TestJSON order by IntCol"
)
self.assertEqual(self.cursor.fetchall(), data)
def test_3512(self):
"3512 - test fetching json with json_query"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"insert into TestJSON values (:1, :2)",
list(enumerate(self.json_data)),
)
cases = [(1, "$.employees.employee1.name"), (2, "$.employees")]
for num_rows, json_query in cases:
self.cursor.execute(
f"""
select json_query(JsonCol, '{json_query}')
from TestJson
order by IntCol
"""
)
result = [r for r, in self.cursor if r is not None]
self.assertEqual(len(result), num_rows)
def test_3513(self):
"3513 - test fetching json with json_exists"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"insert into TestJSON values (:1, :2)",
list(enumerate(self.json_data)),
)
cases = [(1, "$.Permanent"), (2, "$.employees")]
for num_rows, json_query in cases:
self.cursor.execute(
f"""
select count(*)
from TestJson
where json_exists(JsonCol, '{json_query}')
"""
)
(count,) = self.cursor.fetchone()
self.assertEqual(count, num_rows)
def test_3514(self):
"3514 - test selecting json data"
self.cursor.execute("delete from TestJson")
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"insert into TestJSON values (:1, :2)",
list(enumerate(self.json_data)),
)
self.cursor.execute(
"""
select t.JsonCol.employees
from TestJson t
where t.JsonCol.employees is not null
order by t.IntCol
"""
)
expected_data = [
self.json_data[-2]["employees"],
self.json_data[-1]["employees"],
]
data = [r for r, in self.cursor]
self.assertEqual(data, expected_data)
def test_3515(self):
"3515 - test fetching json with json_serialize"
self.cursor.execute("delete from TestJson")
data = [{"a": 12.5}, {"b": True}, {"c": None}]
expected_data = ['{"a":12.5}', '{"b":true}', '{"c":null}']
self.cursor.setinputsizes(None, oracledb.DB_TYPE_JSON)
self.cursor.executemany(
"insert into TestJSON values (:1, :2)", list(enumerate(data))
)
self.cursor.execute(
"""
select json_serialize(JsonCol)
from TestJson
order by IntCol
"""
)
fetched_data = [r for r, in self.cursor]
self.assertEqual(fetched_data, expected_data)
if __name__ == "__main__":
test_env.run_test_cases()