Skip to content

Commit 1fd6b58

Browse files
authored
REFACTOR: Change pytests to use temp tables (starting with #) (#79)
### Summary This pull request updates the test cases to use temporary tables (prefixed with `#`) instead of permanent tables for improved isolation and to prevent unintended side effects during test execution. The changes span across multiple test functions in the `tests/test_003_connection.py` and `tests/test_004_cursor.py` files. ### Transition to Temporary Tables * Updated all test cases in `tests/test_003_connection.py` to use temporary tables (e.g., `#pytest_test_autocommit`, `#pytest_test_commit`, `#pytest_test_rollback`) instead of permanent tables. This includes changes in table creation, data insertion, selection, and cleanup steps. [[1]](diffhunk://#diff-ca315ffd463e0f93f3d45ace0869a4a3b1e572941da6723d7e9ce028cf9d2278L80-R114) [[2]](diffhunk://#diff-ca315ffd463e0f93f3d45ace0869a4a3b1e572941da6723d7e9ce028cf9d2278L126-R168) * Modified the `TEST_TABLE` definition in `tests/test_004_cursor.py` to create a temporary table `#pytest_all_data_types` instead of a permanent one. * Updated various test cases in `tests/test_004_cursor.py` (e.g., `test_insert_id_column`, `test_insert_bit_column`, `test_insert_nvarchar_column`, `test_insert_date_column`, `test_insert_real_column`) to use temporary tables (`#pytest_single_column`) for creating, inserting, and querying data. This ensures better test isolation and cleanup. [[1]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L72-R115) [[2]](diffhunk://#diff-82594712308ff34afa8b067af67db231e9a1372ef474da3db121e14e4d418f69L190-R217) ### Issue Reference Fixes [AB#34163](https://sqlclientdrivers.visualstudio.com/c6d89619-62de-46a0-8b46-70b92a84d85e/_workitems/edit/34163) ### Checklist - [x] **Tests Passed** (if applicable) - [x] **Code is formatted** - [x] **Docs Updated** (if necessary) ### Testing Performed <!-- How was this fix tested? --> - [x] Unit Tests
1 parent 453faeb commit 1fd6b58

File tree

2 files changed

+235
-235
lines changed

2 files changed

+235
-235
lines changed

tests/test_003_connection.py

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,41 +77,41 @@ def test_autocommit_setter(db_connection):
7777
db_connection.autocommit = True
7878
cursor = db_connection.cursor()
7979
# Make a transaction and check if it is autocommited
80-
drop_table_if_exists(cursor, "pytest_test_autocommit")
80+
drop_table_if_exists(cursor, "#pytest_test_autocommit")
8181
try:
82-
cursor.execute("CREATE TABLE pytest_test_autocommit (id INT PRIMARY KEY, value VARCHAR(50));")
83-
cursor.execute("INSERT INTO pytest_test_autocommit (id, value) VALUES (1, 'test');")
84-
cursor.execute("SELECT * FROM pytest_test_autocommit WHERE id = 1;")
82+
cursor.execute("CREATE TABLE #pytest_test_autocommit (id INT PRIMARY KEY, value VARCHAR(50));")
83+
cursor.execute("INSERT INTO #pytest_test_autocommit (id, value) VALUES (1, 'test');")
84+
cursor.execute("SELECT * FROM #pytest_test_autocommit WHERE id = 1;")
8585
result = cursor.fetchone()
8686
assert result is not None, "Autocommit failed: No data found"
8787
assert result[1] == 'test', "Autocommit failed: Incorrect data"
8888
except Exception as e:
8989
pytest.fail(f"Autocommit failed: {e}")
9090
finally:
91-
cursor.execute("DROP TABLE pytest_test_autocommit;")
91+
cursor.execute("DROP TABLE #pytest_test_autocommit;")
9292
db_connection.commit()
9393
assert db_connection.autocommit is True, "Autocommit should be True"
9494

9595
db_connection.autocommit = False
9696
cursor = db_connection.cursor()
9797
# Make a transaction and check if it is not autocommited
98-
drop_table_if_exists(cursor, "pytest_test_autocommit")
98+
drop_table_if_exists(cursor, "#pytest_test_autocommit")
9999
try:
100-
cursor.execute("CREATE TABLE pytest_test_autocommit (id INT PRIMARY KEY, value VARCHAR(50));")
101-
cursor.execute("INSERT INTO pytest_test_autocommit (id, value) VALUES (1, 'test');")
102-
cursor.execute("SELECT * FROM pytest_test_autocommit WHERE id = 1;")
100+
cursor.execute("CREATE TABLE #pytest_test_autocommit (id INT PRIMARY KEY, value VARCHAR(50));")
101+
cursor.execute("INSERT INTO #pytest_test_autocommit (id, value) VALUES (1, 'test');")
102+
cursor.execute("SELECT * FROM #pytest_test_autocommit WHERE id = 1;")
103103
result = cursor.fetchone()
104104
assert result is not None, "Autocommit failed: No data found"
105105
assert result[1] == 'test', "Autocommit failed: Incorrect data"
106106
db_connection.commit()
107-
cursor.execute("SELECT * FROM pytest_test_autocommit WHERE id = 1;")
107+
cursor.execute("SELECT * FROM #pytest_test_autocommit WHERE id = 1;")
108108
result = cursor.fetchone()
109109
assert result is not None, "Autocommit failed: No data found after commit"
110110
assert result[1] == 'test', "Autocommit failed: Incorrect data after commit"
111111
except Exception as e:
112112
pytest.fail(f"Autocommit failed: {e}")
113113
finally:
114-
cursor.execute("DROP TABLE pytest_test_autocommit;")
114+
cursor.execute("DROP TABLE #pytest_test_autocommit;")
115115
db_connection.commit()
116116

117117
def test_set_autocommit(db_connection):
@@ -123,49 +123,49 @@ def test_set_autocommit(db_connection):
123123
def test_commit(db_connection):
124124
# Make a transaction and commit
125125
cursor = db_connection.cursor()
126-
drop_table_if_exists(cursor, "pytest_test_commit")
126+
drop_table_if_exists(cursor, "#pytest_test_commit")
127127
try:
128-
cursor.execute("CREATE TABLE pytest_test_commit (id INT PRIMARY KEY, value VARCHAR(50));")
129-
cursor.execute("INSERT INTO pytest_test_commit (id, value) VALUES (1, 'test');")
128+
cursor.execute("CREATE TABLE #pytest_test_commit (id INT PRIMARY KEY, value VARCHAR(50));")
129+
cursor.execute("INSERT INTO #pytest_test_commit (id, value) VALUES (1, 'test');")
130130
db_connection.commit()
131-
cursor.execute("SELECT * FROM pytest_test_commit WHERE id = 1;")
131+
cursor.execute("SELECT * FROM #pytest_test_commit WHERE id = 1;")
132132
result = cursor.fetchone()
133133
assert result is not None, "Commit failed: No data found"
134134
assert result[1] == 'test', "Commit failed: Incorrect data"
135135
except Exception as e:
136136
pytest.fail(f"Commit failed: {e}")
137137
finally:
138-
cursor.execute("DROP TABLE pytest_test_commit;")
138+
cursor.execute("DROP TABLE #pytest_test_commit;")
139139
db_connection.commit()
140140

141141
def test_rollback(db_connection):
142142
# Make a transaction and rollback
143143
cursor = db_connection.cursor()
144-
drop_table_if_exists(cursor, "pytest_test_rollback")
144+
drop_table_if_exists(cursor, "#pytest_test_rollback")
145145
try:
146146
# Create a table and insert data
147-
cursor.execute("CREATE TABLE pytest_test_rollback (id INT PRIMARY KEY, value VARCHAR(50));")
148-
cursor.execute("INSERT INTO pytest_test_rollback (id, value) VALUES (1, 'test');")
147+
cursor.execute("CREATE TABLE #pytest_test_rollback (id INT PRIMARY KEY, value VARCHAR(50));")
148+
cursor.execute("INSERT INTO #pytest_test_rollback (id, value) VALUES (1, 'test');")
149149
db_connection.commit()
150150

151151
# Check if the data is present before rollback
152-
cursor.execute("SELECT * FROM pytest_test_rollback WHERE id = 1;")
152+
cursor.execute("SELECT * FROM #pytest_test_rollback WHERE id = 1;")
153153
result = cursor.fetchone()
154154
assert result is not None, "Rollback failed: No data found before rollback"
155155
assert result[1] == 'test', "Rollback failed: Incorrect data"
156156

157157
# Insert data and rollback
158-
cursor.execute("INSERT INTO pytest_test_rollback (id, value) VALUES (2, 'test');")
158+
cursor.execute("INSERT INTO #pytest_test_rollback (id, value) VALUES (2, 'test');")
159159
db_connection.rollback()
160160

161161
# Check if the data is not present after rollback
162-
cursor.execute("SELECT * FROM pytest_test_rollback WHERE id = 2;")
162+
cursor.execute("SELECT * FROM #pytest_test_rollback WHERE id = 2;")
163163
result = cursor.fetchone()
164164
assert result is None, "Rollback failed: Data found after rollback"
165165
except Exception as e:
166166
pytest.fail(f"Rollback failed: {e}")
167167
finally:
168-
cursor.execute("DROP TABLE pytest_test_rollback;")
168+
cursor.execute("DROP TABLE #pytest_test_rollback;")
169169
db_connection.commit()
170170

171171
def test_invalid_connection_string():

0 commit comments

Comments
 (0)