-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathtest_dbapi.py
369 lines (298 loc) · 9.45 KB
/
test_dbapi.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
367
368
369
# Copyright 2021 Google LLC All rights reserved.
#
# 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
#
# http://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.
import hashlib
import pickle
import pkg_resources
import pytest
from google.cloud import spanner_v1
from google.cloud.spanner_dbapi.connection import connect, Connection
from . import _helpers
DATABASE_NAME = "dbapi-txn"
DDL_STATEMENTS = (
"""CREATE TABLE contacts (
contact_id INT64,
first_name STRING(1024),
last_name STRING(1024),
email STRING(1024)
)
PRIMARY KEY (contact_id)""",
)
@pytest.fixture(scope="session")
def raw_database(shared_instance, database_operation_timeout):
databse_id = _helpers.unique_id("dbapi-txn")
pool = spanner_v1.BurstyPool(labels={"testcase": "database_api"})
database = shared_instance.database(
databse_id, ddl_statements=DDL_STATEMENTS, pool=pool,
)
op = database.create()
op.result(database_operation_timeout) # raises on failure / timeout.
yield database
database.drop()
def clear_table(transaction):
transaction.execute_update("DELETE FROM contacts WHERE true")
@pytest.fixture(scope="function")
def dbapi_database(raw_database):
raw_database.run_in_transaction(clear_table)
yield raw_database
raw_database.run_in_transaction(clear_table)
def test_commit(shared_instance, dbapi_database):
"""Test committing a transaction with several statements."""
want_row = (
1,
"updated-first-name",
"last-name",
"test.email_updated@domen.ru",
)
# connect to the test database
conn = Connection(shared_instance, dbapi_database)
cursor = conn.cursor()
# execute several DML statements within one transaction
cursor.execute(
"""
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (1, 'first-name', 'last-name', 'test.email@domen.ru')
"""
)
cursor.execute(
"""
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
)
cursor.execute(
"""
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
)
conn.commit()
# read the resulting data from the database
cursor.execute("SELECT * FROM contacts")
got_rows = cursor.fetchall()
conn.commit()
assert got_rows == [want_row]
cursor.close()
conn.close()
def test_rollback(shared_instance, dbapi_database):
"""Test rollbacking a transaction with several statements."""
want_row = (2, "first-name", "last-name", "test.email@domen.ru")
# 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', 'test.email@domen.ru')
"""
)
conn.commit()
# execute several DMLs with one transaction
cursor.execute(
"""
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
)
cursor.execute(
"""
UPDATE contacts
SET email = 'test.email_updated@domen.ru'
WHERE email = 'test.email@domen.ru'
"""
)
conn.rollback()
# read the resulting data from the database
cursor.execute("SELECT * FROM contacts")
got_rows = cursor.fetchall()
conn.commit()
assert got_rows == [want_row]
cursor.close()
conn.close()
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",
"test.email@domen.ru",
)
# 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', 'test.email@domen.ru')
"""
)
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()
def test_rollback_on_connection_closing(shared_instance, dbapi_database):
"""
When closing a connection all the pending transactions
must be rollbacked. Testing if it's working this way.
"""
want_row = (1, "first-name", "last-name", "test.email@domen.ru")
# 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 (1, 'first-name', 'last-name', 'test.email@domen.ru')
"""
)
conn.commit()
cursor.execute(
"""
UPDATE contacts
SET first_name = 'updated-first-name'
WHERE first_name = 'first-name'
"""
)
conn.close()
# connect again, as the previous connection is no-op after closing
conn = Connection(shared_instance, dbapi_database)
cursor = conn.cursor()
# read the resulting data from the database
cursor.execute("SELECT * FROM contacts")
got_rows = cursor.fetchall()
conn.commit()
assert got_rows == [want_row]
cursor.close()
conn.close()
def test_results_checksum(shared_instance, dbapi_database):
"""Test that results checksum is calculated properly."""
conn = Connection(shared_instance, dbapi_database)
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES
(1, 'first-name', 'last-name', 'test.email@domen.ru'),
(2, 'first-name2', 'last-name2', 'test.email2@domen.ru')
"""
)
assert len(conn._statements) == 1
conn.commit()
cursor.execute("SELECT * FROM contacts")
got_rows = cursor.fetchall()
assert len(conn._statements) == 1
conn.commit()
checksum = hashlib.sha256()
checksum.update(pickle.dumps(got_rows[0]))
checksum.update(pickle.dumps(got_rows[1]))
assert cursor._checksum.checksum.digest() == checksum.digest()
def test_execute_many(shared_instance, dbapi_database):
# connect to the test database
conn = Connection(shared_instance, dbapi_database)
cursor = conn.cursor()
row_data = [
(1, "first-name", "last-name", "test.email@example.com"),
(2, "first-name2", "last-name2", "test.email2@example.com"),
]
cursor.executemany(
"""
INSERT INTO contacts (contact_id, first_name, last_name, email)
VALUES (%s, %s, %s, %s)
""",
row_data,
)
conn.commit()
cursor.executemany(
"""SELECT * FROM contacts WHERE contact_id = @a1""", ({"a1": 1}, {"a1": 2}),
)
res = cursor.fetchall()
conn.commit()
assert len(res) == len(row_data)
for found, expected in zip(res, row_data):
assert found[0] == expected[0]
# checking that execute() and executemany()
# results are not mixed together
cursor.execute(
"""
SELECT * FROM contacts WHERE contact_id = 1
""",
)
res = cursor.fetchone()
conn.commit()
assert res[0] == 1
conn.close()
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()
def test_DDL_commit(shared_instance, dbapi_database):
"""Check that DDLs in commit mode are executed on calling `commit()`."""
conn = Connection(shared_instance, dbapi_database)
cur = conn.cursor()
cur.execute(
"""
CREATE TABLE Singers (
SingerId INT64 NOT NULL,
Name STRING(1024),
) PRIMARY KEY (SingerId)
"""
)
conn.commit()
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()
def test_ping(shared_instance, dbapi_database):
"""Check connection validation method."""
conn = Connection(shared_instance, dbapi_database)
conn.validate()
conn.close()
def test_user_agent(shared_instance, dbapi_database):
"""Check that DB API uses an appropriate user agent."""
conn = connect(shared_instance.name, dbapi_database.name)
assert (
conn.instance._client._client_info.user_agent
== "dbapi/" + pkg_resources.get_distribution("google-cloud-spanner").version
)