Skip to content

Commit 5ca7b58

Browse files
committed
tests: use a "clean" default_connection due to tests contaminating each other
1 parent 3922560 commit 5ca7b58

1 file changed

Lines changed: 59 additions & 60 deletions

File tree

tests/fast/api/test_duckdb_connection.py

Lines changed: 59 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -24,23 +24,23 @@ def tmp_database(tmp_path_factory):
2424
# wrapped by the 'duckdb' module, to execute with the 'default_connection'
2525
class TestDuckDBConnection(object):
2626
@pytest.mark.parametrize('pandas', [NumpyPandas(), ArrowPandas()])
27-
def test_append(self, pandas):
28-
duckdb.execute("Create table integers (i integer)")
27+
def test_append(self, pandas, default_con):
28+
default_con.execute("Create table integers (i integer)")
2929
df_in = pandas.DataFrame(
3030
{
3131
'numbers': [1, 2, 3, 4, 5],
3232
}
3333
)
34-
duckdb.append('integers', df_in)
35-
assert duckdb.execute('select count(*) from integers').fetchone()[0] == 5
34+
default_con.append('integers', df_in)
35+
assert default_con.execute('select count(*) from integers').fetchone()[0] == 5
3636
# cleanup
37-
duckdb.execute("drop table integers")
37+
default_con.execute("drop table integers")
3838

39-
def test_default_connection_from_connect(self):
40-
duckdb.sql('create or replace table connect_default_connect (i integer)')
39+
def test_default_connection_from_connect(self, default_con):
40+
default_con.sql('create or replace table connect_default_connect (i integer)')
4141
con = duckdb.connect(':default:')
4242
con.sql('select i from connect_default_connect')
43-
duckdb.sql('drop table connect_default_connect')
43+
default_con.sql('drop table connect_default_connect')
4444
with pytest.raises(duckdb.Error):
4545
con.sql('select i from connect_default_connect')
4646

@@ -57,31 +57,31 @@ def test_arrow(self):
5757

5858
def test_begin_commit(self):
5959
duckdb.begin()
60-
duckdb.execute("create table tbl as select 1")
60+
duckdb.execute("create table tbl_1 as select 1")
6161
duckdb.commit()
62-
res = duckdb.table("tbl")
63-
duckdb.execute("drop table tbl")
62+
res = duckdb.table("tbl_1")
63+
duckdb.execute("drop table tbl_1")
6464

65-
def test_begin_rollback(self):
66-
duckdb.begin()
67-
duckdb.execute("create table tbl as select 1")
68-
duckdb.rollback()
65+
def test_begin_rollback(self, default_con):
66+
default_con.begin()
67+
default_con.execute("create table tbl_1rb as select 1")
68+
default_con.rollback()
6969
with pytest.raises(duckdb.CatalogException):
7070
# Table does not exist
71-
res = duckdb.table("tbl")
71+
res = default_con.table("tbl_1rb")
7272

73-
def test_cursor(self):
74-
duckdb.execute("create table tbl as select 3")
73+
def test_cursor(self, default_con):
74+
default_con.execute("create table tbl_3 as select 3")
7575
duckdb_cursor = duckdb.cursor()
76-
res = duckdb_cursor.table("tbl").fetchall()
76+
res = duckdb_cursor.table("tbl_3").fetchall()
7777
assert res == [(3,)]
78-
duckdb_cursor.execute("drop table tbl")
78+
duckdb_cursor.execute("drop table tbl_3")
7979
with pytest.raises(duckdb.CatalogException):
8080
# 'tbl' no longer exists
81-
duckdb.table("tbl")
81+
default_con.table("tbl_3")
8282

83-
def test_cursor_lifetime(self):
84-
con = duckdb.connect()
83+
def test_cursor_lifetime(self, default_con):
84+
con = default_con
8585

8686
def use_cursors():
8787
cursors = []
@@ -103,12 +103,12 @@ def test_df(self):
103103
assert res == ref
104104

105105
def test_duplicate(self):
106-
duckdb.execute("create table tbl as select 5")
106+
duckdb.execute("create table tbl_5 as select 5")
107107
dup_conn = duckdb.duplicate()
108-
dup_conn.table("tbl").fetchall()
109-
duckdb.execute("drop table tbl")
108+
dup_conn.table("tbl_5").fetchall()
109+
duckdb.execute("drop table tbl_5")
110110
with pytest.raises(duckdb.CatalogException):
111-
dup_conn.table("tbl").fetchall()
111+
dup_conn.table("tbl_5").fetchall()
112112

113113
def test_readonly_properties(self):
114114
duckdb.execute("select 42")
@@ -123,11 +123,11 @@ def test_execute(self):
123123
def test_executemany(self):
124124
# executemany does not keep an open result set
125125
# TODO: shouldn't we also have a version that executes a query multiple times with different parameters, returning all of the results?
126-
duckdb.execute("create table tbl (i integer, j varchar)")
127-
duckdb.executemany("insert into tbl VALUES (?, ?)", [(5, 'test'), (2, 'duck'), (42, 'quack')])
128-
res = duckdb.table("tbl").fetchall()
126+
duckdb.execute("create table tbl_many (i integer, j varchar)")
127+
duckdb.executemany("insert into tbl_many VALUES (?, ?)", [(5, 'test'), (2, 'duck'), (42, 'quack')])
128+
res = duckdb.table("tbl_many").fetchall()
129129
assert res == [(5, 'test'), (2, 'duck'), (42, 'quack')]
130-
duckdb.execute("drop table tbl")
130+
duckdb.execute("drop table tbl_many")
131131

132132
def test_pystatement(self):
133133
with pytest.raises(duckdb.ParserException, match='seledct'):
@@ -163,8 +163,8 @@ def test_pystatement(self):
163163
duckdb.execute(statements[0])
164164
assert duckdb.execute(statements[0], {'1': 42}).fetchall() == [(42,)]
165165

166-
duckdb.execute("create table tbl(a integer)")
167-
statements = duckdb.extract_statements('insert into tbl select $1')
166+
duckdb.execute("create table tbl_a(a integer)")
167+
statements = duckdb.extract_statements('insert into tbl_a select $1')
168168
assert statements[0].expected_result_type == [
169169
duckdb.ExpectedResultType.CHANGED_ROWS,
170170
duckdb.ExpectedResultType.QUERY_RESULT,
@@ -174,23 +174,23 @@ def test_pystatement(self):
174174
):
175175
duckdb.executemany(statements[0])
176176
duckdb.executemany(statements[0], [(21,), (22,), (23,)])
177-
assert duckdb.table('tbl').fetchall() == [(21,), (22,), (23,)]
178-
duckdb.execute("drop table tbl")
177+
assert duckdb.table('tbl_a').fetchall() == [(21,), (22,), (23,)]
178+
duckdb.execute("drop table tbl_a")
179179

180180
def test_fetch_arrow_table(self):
181181
# Needed for 'fetch_arrow_table'
182182
pyarrow = pytest.importorskip("pyarrow")
183183

184-
duckdb.execute("Create Table test (a integer)")
184+
duckdb.execute("Create Table test_arrow_tble (a integer)")
185185

186186
for i in range(1024):
187187
for j in range(2):
188-
duckdb.execute("Insert Into test values ('" + str(i) + "')")
189-
duckdb.execute("Insert Into test values ('5000')")
190-
duckdb.execute("Insert Into test values ('6000')")
188+
duckdb.execute("Insert Into test_arrow_tble values ('" + str(i) + "')")
189+
duckdb.execute("Insert Into test_arrow_tble values ('5000')")
190+
duckdb.execute("Insert Into test_arrow_tble values ('6000')")
191191
sql = '''
192192
SELECT a, COUNT(*) AS repetitions
193-
FROM test
193+
FROM test_arrow_tble
194194
GROUP BY a
195195
'''
196196

@@ -200,7 +200,7 @@ def test_fetch_arrow_table(self):
200200

201201
arrow_df = arrow_table.to_pandas()
202202
assert result_df['repetitions'].sum() == arrow_df['repetitions'].sum()
203-
duckdb.execute("drop table test")
203+
duckdb.execute("drop table test_arrow_tble")
204204

205205
def test_fetch_df(self):
206206
ref = [([1, 2, 3],)]
@@ -210,22 +210,22 @@ def test_fetch_df(self):
210210
assert res == ref
211211

212212
def test_fetch_df_chunk(self):
213-
duckdb.execute("CREATE table t as select range a from range(3000);")
214-
query = duckdb.execute("SELECT a FROM t")
213+
duckdb.execute("CREATE table t_df_chunk as select range a from range(3000);")
214+
query = duckdb.execute("SELECT a FROM t_df_chunk")
215215
cur_chunk = query.fetch_df_chunk()
216216
assert cur_chunk['a'][0] == 0
217217
assert len(cur_chunk) == 2048
218218
cur_chunk = query.fetch_df_chunk()
219219
assert cur_chunk['a'][0] == 2048
220220
assert len(cur_chunk) == 952
221-
duckdb.execute("DROP TABLE t")
221+
duckdb.execute("DROP TABLE t_df_chunk")
222222

223223
def test_fetch_record_batch(self):
224224
# Needed for 'fetch_arrow_table'
225225
pyarrow = pytest.importorskip("pyarrow")
226226

227-
duckdb.execute("CREATE table t as select range a from range(3000);")
228-
duckdb.execute("SELECT a FROM t")
227+
duckdb.execute("CREATE table t_record_batch as select range a from range(3000);")
228+
duckdb.execute("SELECT a FROM t_record_batch")
229229
record_batch_reader = duckdb.fetch_record_batch(1024)
230230
chunk = record_batch_reader.read_all()
231231
assert len(chunk) == 3000
@@ -286,13 +286,13 @@ def test_query(self):
286286
def test_register(self):
287287
assert None != duckdb.register
288288

289-
def test_register_relation(self):
290-
con = duckdb.connect()
289+
def test_register_relation(self, default_con):
290+
con = default_con
291291
rel = con.sql('select [5,4,3]')
292-
con.register("relation", rel)
292+
con.register("relation_rr", rel)
293293

294-
con.sql("create table tbl as select * from relation")
295-
assert con.table('tbl').fetchall() == [([5, 4, 3],)]
294+
con.sql("create table tbl_reg_rel as select * from relation_rr")
295+
assert con.table('tbl_reg_rel').fetchall() == [([5, 4, 3],)]
296296

297297
def test_unregister_problematic_behavior(self, duckdb_cursor):
298298
# We have a VIEW called 'vw' in the Catalog
@@ -314,10 +314,10 @@ def test_unregister_problematic_behavior(self, duckdb_cursor):
314314
assert duckdb_cursor.execute("select * from vw").fetchone() == (0,)
315315

316316
@pytest.mark.parametrize('pandas', [NumpyPandas(), ArrowPandas()])
317-
def test_relation_out_of_scope(self, pandas):
317+
def test_relation_out_of_scope(self, pandas, default_con):
318318
def temporary_scope():
319319
# Create a connection, we will return this
320-
con = duckdb.connect()
320+
con = default_con
321321
# Create a dataframe
322322
df = pandas.DataFrame({'a': [1, 2, 3]})
323323
# The dataframe has to be registered as well
@@ -333,8 +333,8 @@ def temporary_scope():
333333

334334
def test_table(self):
335335
con = duckdb.connect()
336-
con.execute("create table tbl as select 1")
337-
assert [(1,)] == con.table("tbl").fetchall()
336+
con.execute("create table tbl_test_table as select 1")
337+
assert [(1,)] == con.table("tbl_test_table").fetchall()
338338

339339
def test_table_function(self):
340340
assert None != duckdb.table_function
@@ -356,16 +356,15 @@ def test_close(self):
356356
def test_interrupt(self):
357357
assert None != duckdb.interrupt
358358

359-
def test_wrap_shadowing(self):
359+
def test_wrap_shadowing(self, default_con):
360360
pd = NumpyPandas()
361-
import duckdb
362361

363362
df = pd.DataFrame({"a": [1, 2, 3]})
364-
res = duckdb.sql("from df").fetchall()
363+
res = default_con.sql("from df").fetchall()
365364
assert res == [(1,), (2,), (3,)]
366365

367-
def test_wrap_coverage(self):
368-
con = duckdb.default_connection
366+
def test_wrap_coverage(self, default_con):
367+
con = default_con
369368

370369
# Skip all of the initial __xxxx__ methods
371370
connection_methods = dir(con)

0 commit comments

Comments
 (0)