-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathtest_transactions.py
More file actions
598 lines (485 loc) · 22.1 KB
/
test_transactions.py
File metadata and controls
598 lines (485 loc) · 22.1 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
"""
End-to-end integration tests for Multi-Statement Transaction (MST) APIs.
These tests verify:
- autocommit property (getter/setter)
- commit() and rollback() methods
- get_transaction_isolation() and set_transaction_isolation() methods
- Transaction error handling
Requirements:
- DBSQL warehouse that supports Multi-Statement Transactions (MST)
- Test environment configured via test.env file or environment variables
Setup:
Set the following environment variables:
- DATABRICKS_SERVER_HOSTNAME
- DATABRICKS_HTTP_PATH
- DATABRICKS_ACCESS_TOKEN (or use OAuth)
Usage:
pytest tests/e2e/test_transactions.py -v
"""
import logging
import os
import pytest
from typing import Any, Dict
import databricks.sql as sql
from databricks.sql import TransactionError, NotSupportedError, InterfaceError
logger = logging.getLogger(__name__)
@pytest.mark.skip(
reason="Test environment does not yet support multi-statement transactions"
)
class TestTransactions:
"""E2E tests for transaction control methods (MST support)."""
# Test table name
TEST_TABLE_NAME = "transaction_test_table"
@pytest.fixture(autouse=True)
def setup_and_teardown(self, connection_details):
"""Setup test environment before each test and cleanup after."""
self.connection_params = {
"server_hostname": connection_details["host"],
"http_path": connection_details["http_path"],
"access_token": connection_details.get("access_token"),
"ignore_transactions": False, # Enable actual transaction functionality for these tests
}
# Get catalog and schema from environment or use defaults
self.catalog = os.getenv("DATABRICKS_CATALOG", "main")
self.schema = os.getenv("DATABRICKS_SCHEMA", "default")
# Create connection for setup
self.connection = sql.connect(**self.connection_params)
# Setup: Create test table
self._create_test_table()
yield
# Teardown: Cleanup
self._cleanup()
def _get_fully_qualified_table_name(self) -> str:
"""Get the fully qualified table name."""
return f"{self.catalog}.{self.schema}.{self.TEST_TABLE_NAME}"
def _create_test_table(self):
"""Create the test table with Delta format and MST support."""
fq_table_name = self._get_fully_qualified_table_name()
cursor = self.connection.cursor()
try:
# Drop if exists
cursor.execute(f"DROP TABLE IF EXISTS {fq_table_name}")
# Create table with Delta and catalog-owned feature for MST compatibility
cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS {fq_table_name}
(id INT, value STRING)
USING DELTA
TBLPROPERTIES ('delta.feature.catalogOwned-preview' = 'supported')
"""
)
logger.info(f"Created test table: {fq_table_name}")
finally:
cursor.close()
def _cleanup(self):
"""Cleanup after test: rollback pending transactions, drop table, close connection."""
try:
# Try to rollback any pending transaction
if (
self.connection
and self.connection.open
and not self.connection.autocommit
):
try:
self.connection.rollback()
except Exception as e:
logger.debug(
f"Rollback during cleanup failed (may be expected): {e}"
)
# Reset to autocommit mode
try:
self.connection.autocommit = True
except Exception as e:
logger.debug(f"Reset autocommit during cleanup failed: {e}")
# Drop test table
if self.connection and self.connection.open:
fq_table_name = self._get_fully_qualified_table_name()
cursor = self.connection.cursor()
try:
cursor.execute(f"DROP TABLE IF EXISTS {fq_table_name}")
logger.info(f"Dropped test table: {fq_table_name}")
except Exception as e:
logger.warning(f"Failed to drop test table: {e}")
finally:
cursor.close()
finally:
# Close connection
if self.connection:
self.connection.close()
# ==================== BASIC AUTOCOMMIT TESTS ====================
def test_default_autocommit_is_true(self):
"""Test that new connection defaults to autocommit=true."""
assert (
self.connection.autocommit is True
), "New connection should have autocommit=true by default"
def test_set_autocommit_to_false(self):
"""Test successfully setting autocommit to false."""
self.connection.autocommit = False
assert (
self.connection.autocommit is False
), "autocommit should be false after setting to false"
def test_set_autocommit_to_true(self):
"""Test successfully setting autocommit back to true."""
# First disable
self.connection.autocommit = False
assert self.connection.autocommit is False
# Then enable
self.connection.autocommit = True
assert (
self.connection.autocommit is True
), "autocommit should be true after setting to true"
# ==================== COMMIT TESTS ====================
def test_commit_single_insert(self):
"""Test successfully committing a transaction with single INSERT."""
fq_table_name = self._get_fully_qualified_table_name()
# Start transaction
self.connection.autocommit = False
# Insert data
cursor = self.connection.cursor()
cursor.execute(
f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'test_value')"
)
cursor.close()
# Commit
self.connection.commit()
# Verify data is persisted using a new connection
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(f"SELECT value FROM {fq_table_name} WHERE id = 1")
result = verify_cursor.fetchone()
verify_cursor.close()
assert result is not None, "Should find inserted row after commit"
assert result[0] == "test_value", "Value should match inserted value"
finally:
verify_conn.close()
def test_commit_multiple_inserts(self):
"""Test successfully committing a transaction with multiple INSERTs."""
fq_table_name = self._get_fully_qualified_table_name()
self.connection.autocommit = False
# Insert multiple rows
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'value1')")
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (2, 'value2')")
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (3, 'value3')")
cursor.close()
self.connection.commit()
# Verify all rows persisted
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name}")
result = verify_cursor.fetchone()
verify_cursor.close()
assert result[0] == 3, "Should have 3 rows after commit"
finally:
verify_conn.close()
# ==================== ROLLBACK TESTS ====================
def test_rollback_single_insert(self):
"""Test successfully rolling back a transaction."""
fq_table_name = self._get_fully_qualified_table_name()
self.connection.autocommit = False
# Insert data
cursor = self.connection.cursor()
cursor.execute(
f"INSERT INTO {fq_table_name} (id, value) VALUES (100, 'rollback_test')"
)
cursor.close()
# Rollback
self.connection.rollback()
# Verify data is NOT persisted
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 100"
)
result = verify_cursor.fetchone()
verify_cursor.close()
assert result[0] == 0, "Rolled back data should not be persisted"
finally:
verify_conn.close()
# ==================== SEQUENTIAL TRANSACTION TESTS ====================
def test_multiple_sequential_transactions(self):
"""Test executing multiple sequential transactions (commit, commit, rollback)."""
fq_table_name = self._get_fully_qualified_table_name()
self.connection.autocommit = False
# First transaction - commit
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'txn1')")
cursor.close()
self.connection.commit()
# Second transaction - commit
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (2, 'txn2')")
cursor.close()
self.connection.commit()
# Third transaction - rollback
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (3, 'txn3')")
cursor.close()
self.connection.rollback()
# Verify only first two transactions persisted
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table_name} WHERE id IN (1, 2)"
)
result = verify_cursor.fetchone()
assert result[0] == 2, "Should have 2 committed rows"
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 3")
result = verify_cursor.fetchone()
assert result[0] == 0, "Rolled back row should not exist"
verify_cursor.close()
finally:
verify_conn.close()
def test_auto_start_transaction_after_commit(self):
"""Test that new transaction automatically starts after commit."""
fq_table_name = self._get_fully_qualified_table_name()
self.connection.autocommit = False
# First transaction - commit
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'first')")
cursor.close()
self.connection.commit()
# New transaction should start automatically - insert and rollback
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (2, 'second')")
cursor.close()
self.connection.rollback()
# Verify: first committed, second rolled back
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 1")
result = verify_cursor.fetchone()
assert result[0] == 1, "First insert should be committed"
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 2")
result = verify_cursor.fetchone()
assert result[0] == 0, "Second insert should be rolled back"
verify_cursor.close()
finally:
verify_conn.close()
def test_auto_start_transaction_after_rollback(self):
"""Test that new transaction automatically starts after rollback."""
fq_table_name = self._get_fully_qualified_table_name()
self.connection.autocommit = False
# First transaction - rollback
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'first')")
cursor.close()
self.connection.rollback()
# New transaction should start automatically - insert and commit
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (2, 'second')")
cursor.close()
self.connection.commit()
# Verify: first rolled back, second committed
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 1")
result = verify_cursor.fetchone()
assert result[0] == 0, "First insert should be rolled back"
verify_cursor.execute(f"SELECT COUNT(*) FROM {fq_table_name} WHERE id = 2")
result = verify_cursor.fetchone()
assert result[0] == 1, "Second insert should be committed"
verify_cursor.close()
finally:
verify_conn.close()
# ==================== UPDATE/DELETE OPERATION TESTS ====================
def test_update_in_transaction(self):
"""Test UPDATE operation in transaction."""
fq_table_name = self._get_fully_qualified_table_name()
# First insert a row with autocommit
cursor = self.connection.cursor()
cursor.execute(
f"INSERT INTO {fq_table_name} (id, value) VALUES (1, 'original')"
)
cursor.close()
# Start transaction and update
self.connection.autocommit = False
cursor = self.connection.cursor()
cursor.execute(f"UPDATE {fq_table_name} SET value = 'updated' WHERE id = 1")
cursor.close()
self.connection.commit()
# Verify update persisted
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(f"SELECT value FROM {fq_table_name} WHERE id = 1")
result = verify_cursor.fetchone()
assert result[0] == "updated", "Value should be updated after commit"
verify_cursor.close()
finally:
verify_conn.close()
# ==================== MULTI-TABLE TRANSACTION TESTS ====================
def test_multi_table_transaction_commit(self):
"""Test atomic commit across multiple tables."""
fq_table1_name = self._get_fully_qualified_table_name()
table2_name = self.TEST_TABLE_NAME + "_2"
fq_table2_name = f"{self.catalog}.{self.schema}.{table2_name}"
# Create second table
cursor = self.connection.cursor()
cursor.execute(f"DROP TABLE IF EXISTS {fq_table2_name}")
cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS {fq_table2_name}
(id INT, category STRING)
USING DELTA
TBLPROPERTIES ('delta.feature.catalogOwned-preview' = 'supported')
"""
)
cursor.close()
try:
# Start transaction and insert into both tables
self.connection.autocommit = False
cursor = self.connection.cursor()
cursor.execute(
f"INSERT INTO {fq_table1_name} (id, value) VALUES (10, 'table1_data')"
)
cursor.execute(
f"INSERT INTO {fq_table2_name} (id, category) VALUES (10, 'table2_data')"
)
cursor.close()
# Commit both atomically
self.connection.commit()
# Verify both inserts persisted
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table1_name} WHERE id = 10"
)
result = verify_cursor.fetchone()
assert result[0] == 1, "Table1 insert should be committed"
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table2_name} WHERE id = 10"
)
result = verify_cursor.fetchone()
assert result[0] == 1, "Table2 insert should be committed"
verify_cursor.close()
finally:
verify_conn.close()
finally:
# Cleanup second table
self.connection.autocommit = True
cursor = self.connection.cursor()
cursor.execute(f"DROP TABLE IF EXISTS {fq_table2_name}")
cursor.close()
def test_multi_table_transaction_rollback(self):
"""Test atomic rollback across multiple tables."""
fq_table1_name = self._get_fully_qualified_table_name()
table2_name = self.TEST_TABLE_NAME + "_2"
fq_table2_name = f"{self.catalog}.{self.schema}.{table2_name}"
# Create second table
cursor = self.connection.cursor()
cursor.execute(f"DROP TABLE IF EXISTS {fq_table2_name}")
cursor.execute(
f"""
CREATE TABLE IF NOT EXISTS {fq_table2_name}
(id INT, category STRING)
USING DELTA
TBLPROPERTIES ('delta.feature.catalogOwned-preview' = 'supported')
"""
)
cursor.close()
try:
# Start transaction and insert into both tables
self.connection.autocommit = False
cursor = self.connection.cursor()
cursor.execute(
f"INSERT INTO {fq_table1_name} (id, value) VALUES (20, 'rollback1')"
)
cursor.execute(
f"INSERT INTO {fq_table2_name} (id, category) VALUES (20, 'rollback2')"
)
cursor.close()
# Rollback both atomically
self.connection.rollback()
# Verify both inserts were rolled back
verify_conn = sql.connect(**self.connection_params)
try:
verify_cursor = verify_conn.cursor()
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table1_name} WHERE id = 20"
)
result = verify_cursor.fetchone()
assert result[0] == 0, "Table1 insert should be rolled back"
verify_cursor.execute(
f"SELECT COUNT(*) FROM {fq_table2_name} WHERE id = 20"
)
result = verify_cursor.fetchone()
assert result[0] == 0, "Table2 insert should be rolled back"
verify_cursor.close()
finally:
verify_conn.close()
finally:
# Cleanup second table
self.connection.autocommit = True
cursor = self.connection.cursor()
cursor.execute(f"DROP TABLE IF EXISTS {fq_table2_name}")
cursor.close()
# ==================== ERROR HANDLING TESTS ====================
def test_set_autocommit_during_active_transaction(self):
"""Test that setting autocommit during an active transaction throws error."""
fq_table_name = self._get_fully_qualified_table_name()
# Start transaction
self.connection.autocommit = False
cursor = self.connection.cursor()
cursor.execute(f"INSERT INTO {fq_table_name} (id, value) VALUES (99, 'test')")
cursor.close()
# Try to set autocommit=True during active transaction
with pytest.raises(TransactionError) as exc_info:
self.connection.autocommit = True
# Verify error message mentions autocommit or active transaction
error_msg = str(exc_info.value).lower()
assert (
"autocommit" in error_msg or "active transaction" in error_msg
), "Error should mention autocommit or active transaction"
# Cleanup - rollback the transaction
self.connection.rollback()
def test_commit_without_active_transaction_throws_error(self):
"""Test that commit() throws error when autocommit=true (no active transaction)."""
# Ensure autocommit is true (default)
assert self.connection.autocommit is True
# Attempt commit without active transaction should throw
with pytest.raises(TransactionError) as exc_info:
self.connection.commit()
# Verify error message indicates no active transaction
error_message = str(exc_info.value)
assert (
"MULTI_STATEMENT_TRANSACTION_NO_ACTIVE_TRANSACTION" in error_message
or "no active transaction" in error_message.lower()
), "Error should indicate no active transaction"
def test_rollback_without_active_transaction_is_safe(self):
"""Test that rollback() without active transaction is a safe no-op."""
# With autocommit=true (no active transaction)
assert self.connection.autocommit is True
# ROLLBACK should be safe (no exception)
self.connection.rollback()
# Verify connection is still usable
assert self.connection.autocommit is True
assert self.connection.open is True
# ==================== TRANSACTION ISOLATION TESTS ====================
def test_get_transaction_isolation_returns_repeatable_read(self):
"""Test that get_transaction_isolation() returns REPEATABLE_READ."""
isolation_level = self.connection.get_transaction_isolation()
assert (
isolation_level == "REPEATABLE_READ"
), "Databricks MST should use REPEATABLE_READ (Snapshot Isolation)"
def test_set_transaction_isolation_accepts_repeatable_read(self):
"""Test that set_transaction_isolation() accepts REPEATABLE_READ."""
# Should not raise - these are all valid formats
self.connection.set_transaction_isolation("REPEATABLE_READ")
self.connection.set_transaction_isolation("REPEATABLE READ")
self.connection.set_transaction_isolation("repeatable_read")
self.connection.set_transaction_isolation("repeatable read")
def test_set_transaction_isolation_rejects_unsupported_level(self):
"""Test that set_transaction_isolation() rejects unsupported levels."""
with pytest.raises(NotSupportedError) as exc_info:
self.connection.set_transaction_isolation("READ_COMMITTED")
error_message = str(exc_info.value)
assert "not supported" in error_message.lower()
assert "READ_COMMITTED" in error_message