|
| 1 | +# Copyright 2025 Google LLC All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from google.cloud.spanner_dbapi import Connection |
| 16 | +from google.cloud.spanner_v1 import ( |
| 17 | + ExecuteSqlRequest, |
| 18 | + TypeCode, |
| 19 | + CommitRequest, |
| 20 | + ExecuteBatchDmlRequest, |
| 21 | +) |
| 22 | +from tests.mockserver_tests.mock_server_test_base import ( |
| 23 | + MockServerTestBase, |
| 24 | + add_single_result, |
| 25 | + add_update_count, |
| 26 | +) |
| 27 | + |
| 28 | + |
| 29 | +class TestDbapiAutoCommit(MockServerTestBase): |
| 30 | + @classmethod |
| 31 | + def setup_class(cls): |
| 32 | + super().setup_class() |
| 33 | + add_single_result( |
| 34 | + "select name from singers", "name", TypeCode.STRING, [("Some Singer",)] |
| 35 | + ) |
| 36 | + add_update_count("insert into singers (id, name) values (1, 'Some Singer')", 1) |
| 37 | + |
| 38 | + def test_select_autocommit(self): |
| 39 | + connection = Connection(self.instance, self.database) |
| 40 | + connection.autocommit = True |
| 41 | + with connection.cursor() as cursor: |
| 42 | + cursor.execute("select name from singers") |
| 43 | + result_list = cursor.fetchall() |
| 44 | + for _ in result_list: |
| 45 | + pass |
| 46 | + requests = list( |
| 47 | + filter( |
| 48 | + lambda msg: isinstance(msg, ExecuteSqlRequest), |
| 49 | + self.spanner_service.requests, |
| 50 | + ) |
| 51 | + ) |
| 52 | + self.assertEqual(1, len(requests)) |
| 53 | + self.assertFalse(requests[0].last_statement, requests[0]) |
| 54 | + self.assertIsNotNone(requests[0].transaction, requests[0]) |
| 55 | + self.assertIsNotNone(requests[0].transaction.single_use, requests[0]) |
| 56 | + self.assertTrue(requests[0].transaction.single_use.read_only, requests[0]) |
| 57 | + |
| 58 | + def test_dml_autocommit(self): |
| 59 | + connection = Connection(self.instance, self.database) |
| 60 | + connection.autocommit = True |
| 61 | + with connection.cursor() as cursor: |
| 62 | + cursor.execute("insert into singers (id, name) values (1, 'Some Singer')") |
| 63 | + self.assertEqual(1, cursor.rowcount) |
| 64 | + requests = list( |
| 65 | + filter( |
| 66 | + lambda msg: isinstance(msg, ExecuteSqlRequest), |
| 67 | + self.spanner_service.requests, |
| 68 | + ) |
| 69 | + ) |
| 70 | + self.assertEqual(1, len(requests)) |
| 71 | + self.assertTrue(requests[0].last_statement, requests[0]) |
| 72 | + commit_requests = list( |
| 73 | + filter( |
| 74 | + lambda msg: isinstance(msg, CommitRequest), |
| 75 | + self.spanner_service.requests, |
| 76 | + ) |
| 77 | + ) |
| 78 | + self.assertEqual(1, len(commit_requests)) |
| 79 | + |
| 80 | + def test_executemany_autocommit(self): |
| 81 | + connection = Connection(self.instance, self.database) |
| 82 | + connection.autocommit = True |
| 83 | + with connection.cursor() as cursor: |
| 84 | + cursor.executemany( |
| 85 | + "insert into singers (id, name) values (1, 'Some Singer')", [(), ()] |
| 86 | + ) |
| 87 | + self.assertEqual(2, cursor.rowcount) |
| 88 | + requests = list( |
| 89 | + filter( |
| 90 | + lambda msg: isinstance(msg, ExecuteBatchDmlRequest), |
| 91 | + self.spanner_service.requests, |
| 92 | + ) |
| 93 | + ) |
| 94 | + self.assertEqual(1, len(requests)) |
| 95 | + self.assertTrue(requests[0].last_statements, requests[0]) |
| 96 | + commit_requests = list( |
| 97 | + filter( |
| 98 | + lambda msg: isinstance(msg, CommitRequest), |
| 99 | + self.spanner_service.requests, |
| 100 | + ) |
| 101 | + ) |
| 102 | + self.assertEqual(1, len(commit_requests)) |
| 103 | + |
| 104 | + def test_batch_dml_autocommit(self): |
| 105 | + connection = Connection(self.instance, self.database) |
| 106 | + connection.autocommit = True |
| 107 | + with connection.cursor() as cursor: |
| 108 | + cursor.execute("start batch dml") |
| 109 | + cursor.execute("insert into singers (id, name) values (1, 'Some Singer')") |
| 110 | + cursor.execute("insert into singers (id, name) values (1, 'Some Singer')") |
| 111 | + cursor.execute("run batch") |
| 112 | + self.assertEqual(2, cursor.rowcount) |
| 113 | + requests = list( |
| 114 | + filter( |
| 115 | + lambda msg: isinstance(msg, ExecuteBatchDmlRequest), |
| 116 | + self.spanner_service.requests, |
| 117 | + ) |
| 118 | + ) |
| 119 | + self.assertEqual(1, len(requests)) |
| 120 | + self.assertTrue(requests[0].last_statements, requests[0]) |
| 121 | + commit_requests = list( |
| 122 | + filter( |
| 123 | + lambda msg: isinstance(msg, CommitRequest), |
| 124 | + self.spanner_service.requests, |
| 125 | + ) |
| 126 | + ) |
| 127 | + self.assertEqual(1, len(commit_requests)) |
0 commit comments