MCPcopy Create free account
hub / github.com/PyMySQL/PyMySQL / test_json

Method test_json

pymysql/tests/test_basic.py:298–328  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

296 c.close()
297
298 def test_json(self):
299 args = self.databases[0].copy()
300 args["charset"] = "utf8mb4"
301 conn = pymysql.connect(**args)
302 # MariaDB only has limited JSON support, stores data as longtext
303 # https://mariadb.com/kb/en/json-data-type/
304 if not self.mysql_server_is(conn, (5, 7, 0)):
305 pytest.skip("JSON type is only supported on MySQL >= 5.7")
306
307 self.safe_create_table(
308 conn,
309 "test_json",
310 """\
311create table test_json (
312 id int not null,
313 json JSON not null,
314 primary key (id)
315);""",
316 )
317 cur = conn.cursor()
318
319 json_str = '{"hello": "こんにちは"}'
320 cur.execute("INSERT INTO test_json (id, `json`) values (42, %s)", (json_str,))
321 cur.execute("SELECT `json` from `test_json` WHERE `id`=42")
322 res = cur.fetchone()[0]
323 self.assertEqual(json.loads(res), json.loads(json_str))
324
325 if self.get_mysql_vendor(conn) == "mysql":
326 cur.execute("SELECT CAST(%s AS JSON) AS x", (json_str,))
327 res = cur.fetchone()[0]
328 self.assertEqual(json.loads(res), json.loads(json_str))
329
330
331class TestBulkInserts(base.PyMySQLTestCase):

Callers

nothing calls this directly

Calls 7

mysql_server_isMethod · 0.80
safe_create_tableMethod · 0.80
cursorMethod · 0.80
executeMethod · 0.80
get_mysql_vendorMethod · 0.80
connectMethod · 0.45
fetchoneMethod · 0.45

Tested by

no test coverage detected