Ejemplo n.º 1
0
def test_autocommit_mode_change(shared_instance, dbapi_database):
    """Test auto committing a transaction on `autocommit` mode change."""
    want_row = (
        2,
        "updated-first-name",
        "last-name",
        "*****@*****.**",
    )
    # connect to the test database
    conn = Connection(shared_instance, dbapi_database)
    cursor = conn.cursor()

    cursor.execute("""
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (2, 'first-name', 'last-name', '*****@*****.**')
    """)
    cursor.execute("""
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
""")
    conn.autocommit = True

    # read the resulting data from the database
    cursor.execute("SELECT * FROM contacts")
    got_rows = cursor.fetchall()

    assert got_rows == [want_row]

    cursor.close()
    conn.close()
Ejemplo n.º 2
0
def test_DDL_autocommit(shared_instance, dbapi_database):
    """Check that DDLs in autocommit mode are immediately executed."""
    conn = Connection(shared_instance, dbapi_database)
    conn.autocommit = True

    cur = conn.cursor()
    cur.execute("""
        CREATE TABLE Singers (
            SingerId     INT64 NOT NULL,
            Name    STRING(1024),
        ) PRIMARY KEY (SingerId)
    """)
    conn.close()

    # if previous DDL wasn't committed, the next DROP TABLE
    # statement will fail with a ProgrammingError
    conn = Connection(shared_instance, dbapi_database)
    cur = conn.cursor()

    cur.execute("DROP TABLE Singers")
    conn.commit()
Ejemplo n.º 3
0
def test_autocommit_with_json_data(shared_instance, dbapi_database):
    """Check that DDLs in autocommit mode are immediately executed for
    json fields."""
    # Create table
    conn = Connection(shared_instance, dbapi_database)
    conn.autocommit = True

    cur = conn.cursor()
    cur.execute("""
        CREATE TABLE JsonDetails (
            DataId     INT64 NOT NULL,
            Details    JSON,
        ) PRIMARY KEY (DataId)
    """)

    # Insert data to table
    cur.execute(
        sql="INSERT INTO JsonDetails (DataId, Details) VALUES (%s, %s)",
        args=(123, JsonObject({
            "name": "Jakob",
            "age": "26"
        })),
    )

    # Read back the data.
    cur.execute("""select * from JsonDetails;""")
    got_rows = cur.fetchall()

    # Assert the response
    assert len(got_rows) == 1
    assert got_rows[0][0] == 123
    assert got_rows[0][1] == {"age": "26", "name": "Jakob"}

    # Drop the table
    cur.execute("DROP TABLE JsonDetails")
    conn.commit()
    conn.close()