|
1 | 1 | import unittest |
2 | 2 |
|
| 3 | +import pytest |
| 4 | + |
3 | 5 | import pymssql as pym |
4 | 6 |
|
5 | | -from .helpers import pymssqlconn, PyTableBase, drop_table, CursorBase, eq_, config |
| 7 | +from .helpers import (pymssqlconn, PyTableBase, CursorBase, eq_, config, |
| 8 | + skip_test) |
6 | 9 |
|
7 | 10 | class TestDBAPI2(object): |
8 | 11 | def test_version(self): |
@@ -83,13 +86,15 @@ def test_rollback_after_create_error(self): |
83 | 86 | # too |
84 | 87 | eq_(self.row_count(), 0) |
85 | 88 |
|
| 89 | + |
86 | 90 | class TestCursor(CursorBase): |
87 | 91 | dbmod = pym |
88 | 92 |
|
89 | 93 | @classmethod |
90 | 94 | def newconn(cls): |
91 | 95 | cls.conn = pymssqlconn() |
92 | 96 |
|
| 97 | + |
93 | 98 | class TestBasicConnection(unittest.TestCase): |
94 | 99 |
|
95 | 100 | def connect(self, conn_props=None): |
@@ -130,3 +135,82 @@ def test_conn_props_override(self): |
130 | 135 | password=config.password |
131 | 136 | ) |
132 | 137 | conn.close() |
| 138 | + |
| 139 | + |
| 140 | +class TestAutocommit(unittest.TestCase, PyTableBase): |
| 141 | + tname = 'test' |
| 142 | + cols = ( |
| 143 | + 'name varchar(50)', |
| 144 | + ) |
| 145 | + |
| 146 | + insert_query = 'INSERT INTO {tname} VALUES (%s)'.format(tname=tname) |
| 147 | + select_query = 'SELECT * FROM {tname} WHERE name = (%s)'.format(tname=tname) |
| 148 | + |
| 149 | + test_db_name = 'autocommit_test_database' |
| 150 | + |
| 151 | + def setUp(self): |
| 152 | + PyTableBase.setUp(self) |
| 153 | + |
| 154 | + def tearDown(self): |
| 155 | + self.conn._conn.execute_non_query("IF EXISTS(select * from sys.databases where name='{0}') DROP DATABASE {0}".format(self.test_db_name)) |
| 156 | + |
| 157 | + def test_db_creation_with_autocommit(self): |
| 158 | + """ |
| 159 | + Try creating and dropping database with autocommit |
| 160 | + """ |
| 161 | + cur = pymssqlconn(autocommit=True).cursor() |
| 162 | + try: |
| 163 | + cur.execute("CREATE DATABASE {0}".format(self.test_db_name)) |
| 164 | + except pym.OperationalError as e: |
| 165 | + expected_msg = "CREATE DATABASE permission denied in database 'master'" |
| 166 | + if expected_msg in str(e.args[1]): |
| 167 | + skip_test('We have no CREATE DATABASE permission on test database') |
| 168 | + else: |
| 169 | + pytest.fail() |
| 170 | + else: |
| 171 | + cur.execute("DROP DATABASE {0}".format(self.test_db_name)) |
| 172 | + |
| 173 | + def test_db_creation_without_autocommit(self): |
| 174 | + """ |
| 175 | + Try creating and dropping database without autocommit, expecting it to fail |
| 176 | + """ |
| 177 | + cur = pymssqlconn(autocommit=False).cursor() |
| 178 | + with pytest.raises(pym.OperationalError) as excinfo: |
| 179 | + cur.execute("CREATE DATABASE autocommit_test_database") |
| 180 | + expected_msg = "CREATE DATABASE statement not allowed within multi-statement transaction" |
| 181 | + assert expected_msg in excinfo.exconly() |
| 182 | + |
| 183 | + def test_autocommit_flipping_tf(self): |
| 184 | + insert_value = 'true-false' |
| 185 | + conn = pymssqlconn(autocommit=True) |
| 186 | + conn.autocommit(False) |
| 187 | + cur = conn.cursor() |
| 188 | + cur.execute(self.insert_query, insert_value) |
| 189 | + conn.commit() |
| 190 | + cur.execute(self.select_query, insert_value) |
| 191 | + row = cur.fetchone() |
| 192 | + cur.close() |
| 193 | + conn.close() |
| 194 | + assert len(row) > 0 |
| 195 | + |
| 196 | + def test_autocommit_flipping_ft(self): |
| 197 | + insert_value = 'false-true' |
| 198 | + conn = pymssqlconn(autocommit=False) |
| 199 | + conn.autocommit(True) |
| 200 | + cur = conn.cursor() |
| 201 | + cur.execute(self.insert_query, insert_value) |
| 202 | + cur.execute(self.select_query, insert_value) |
| 203 | + row = cur.fetchone() |
| 204 | + assert len(row) > 0 |
| 205 | + |
| 206 | + def test_autocommit_false_does_not_commit(self): |
| 207 | + insert_value = 'false' |
| 208 | + conn = pymssqlconn(autocommit=False) |
| 209 | + cur = conn.cursor() |
| 210 | + cur.execute(self.insert_query, insert_value) |
| 211 | + conn.rollback() |
| 212 | + cur.execute(self.select_query, insert_value) |
| 213 | + row = cur.fetchone() |
| 214 | + cur.close() |
| 215 | + conn.close() |
| 216 | + assert row is None |
0 commit comments