Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit d047d40

Browse files
authored
Fix flaky tests with in-memory SQLite databases (#569)
* Run garbage collection before asserting SQLite database is cleared in test * Use unique in-memory database name for each SQLite test * Run garbage collection after every test
1 parent 27c1699 commit d047d40

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

tests/test_databases.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,9 @@ def create_test_database():
115115
engine = sqlalchemy.create_engine(url)
116116
metadata.drop_all(engine)
117117

118+
# Run garbage collection to ensure any in-memory databases are dropped
119+
gc.collect()
120+
118121

119122
def async_adapter(wrapped_func):
120123
"""
@@ -1549,7 +1552,10 @@ async def test_mapping_property_interface(database_url):
15491552

15501553
@async_adapter
15511554
async def test_should_not_maintain_ref_when_no_cache_param():
1552-
async with Database("sqlite:///file::memory:", uri=True) as database:
1555+
async with Database(
1556+
"sqlite:///file::memory:",
1557+
uri=True,
1558+
) as database:
15531559
query = sqlalchemy.schema.CreateTable(notes)
15541560
await database.execute(query)
15551561

@@ -1561,7 +1567,10 @@ async def test_should_not_maintain_ref_when_no_cache_param():
15611567

15621568
@async_adapter
15631569
async def test_should_maintain_ref_when_cache_param():
1564-
async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
1570+
async with Database(
1571+
"sqlite:///file::memory:?cache=shared",
1572+
uri=True,
1573+
) as database:
15651574
query = sqlalchemy.schema.CreateTable(notes)
15661575
await database.execute(query)
15671576

@@ -1577,15 +1586,24 @@ async def test_should_maintain_ref_when_cache_param():
15771586

15781587
@async_adapter
15791588
async def test_should_remove_ref_on_disconnect():
1580-
async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
1589+
async with Database(
1590+
"sqlite:///file::memory:?cache=shared",
1591+
uri=True,
1592+
) as database:
15811593
query = sqlalchemy.schema.CreateTable(notes)
15821594
await database.execute(query)
15831595

15841596
query = notes.insert()
15851597
values = {"text": "example1", "completed": True}
15861598
await database.execute(query, values)
15871599

1588-
async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
1600+
# Run garbage collection to reset the database if we dropped the reference
1601+
gc.collect()
1602+
1603+
async with Database(
1604+
"sqlite:///file::memory:?cache=shared",
1605+
uri=True,
1606+
) as database:
15891607
query = notes.select()
15901608
with pytest.raises(sqlite3.OperationalError):
15911609
await database.fetch_all(query=query)

0 commit comments

Comments
 (0)