|
15 | 15 | import hashlib |
16 | 16 | import pickle |
17 | 17 | import pkg_resources |
18 | | - |
19 | 18 | import pytest |
20 | 19 |
|
21 | 20 | from google.cloud import spanner_v1 |
22 | 21 | from google.cloud.spanner_dbapi.connection import connect, Connection |
| 22 | +from google.cloud.spanner_v1 import JsonObject |
23 | 23 | from . import _helpers |
24 | 24 |
|
25 | 25 | DATABASE_NAME = "dbapi-txn" |
@@ -328,6 +328,45 @@ def test_DDL_autocommit(shared_instance, dbapi_database): |
328 | 328 | conn.commit() |
329 | 329 |
|
330 | 330 |
|
| 331 | +@pytest.mark.skipif(_helpers.USE_EMULATOR, reason="Emulator does not support json.") |
| 332 | +def test_autocommit_with_json_data(shared_instance, dbapi_database): |
| 333 | + """Check that DDLs in autocommit mode are immediately executed for |
| 334 | + json fields.""" |
| 335 | + # Create table |
| 336 | + conn = Connection(shared_instance, dbapi_database) |
| 337 | + conn.autocommit = True |
| 338 | + |
| 339 | + cur = conn.cursor() |
| 340 | + cur.execute( |
| 341 | + """ |
| 342 | + CREATE TABLE JsonDetails ( |
| 343 | + DataId INT64 NOT NULL, |
| 344 | + Details JSON, |
| 345 | + ) PRIMARY KEY (DataId) |
| 346 | + """ |
| 347 | + ) |
| 348 | + |
| 349 | + # Insert data to table |
| 350 | + cur.execute( |
| 351 | + sql="INSERT INTO JsonDetails (DataId, Details) VALUES (%s, %s)", |
| 352 | + args=(123, JsonObject({"name": "Jakob", "age": "26"})), |
| 353 | + ) |
| 354 | + |
| 355 | + # Read back the data. |
| 356 | + cur.execute("""select * from JsonDetails;""") |
| 357 | + got_rows = cur.fetchall() |
| 358 | + |
| 359 | + # Assert the response |
| 360 | + assert len(got_rows) == 1 |
| 361 | + assert got_rows[0][0] == 123 |
| 362 | + assert got_rows[0][1] == '{"age":"26","name":"Jakob"}' |
| 363 | + |
| 364 | + # Drop the table |
| 365 | + cur.execute("DROP TABLE JsonDetails") |
| 366 | + conn.commit() |
| 367 | + conn.close() |
| 368 | + |
| 369 | + |
331 | 370 | def test_DDL_commit(shared_instance, dbapi_database): |
332 | 371 | """Check that DDLs in commit mode are executed on calling `commit()`.""" |
333 | 372 | conn = Connection(shared_instance, dbapi_database) |
|
0 commit comments