From 1d30e39d66baa07dec9e060d9b2f73b06e0cccc3 Mon Sep 17 00:00:00 2001
From: Zanie <contact@zanie.dev>
Date: Mon, 28 Aug 2023 16:20:58 -0500
Subject: [PATCH 1/3] Run garbage collection before asserting SQLite database
is cleared in test
---
tests/test_databases.py | 3 +++
1 file changed, 3 insertions(+)
diff --git a/tests/test_databases.py b/tests/test_databases.py
index 1ed0b1b..6d94822 100644
--- a/tests/test_databases.py
+++ b/tests/test_databases.py
@@ -1585,6 +1585,9 @@ async def test_should_remove_ref_on_disconnect():
values = {"text": "example1", "completed": True}
await database.execute(query, values)
+ # Run garbage collection to reset the database if we dropped the reference
+ gc.collect()
+
async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
query = notes.select()
with pytest.raises(sqlite3.OperationalError):
From 2d0fc8d6472cd944177751c730996b01c01f1851 Mon Sep 17 00:00:00 2001
From: Zanie <contact@zanie.dev>
Date: Tue, 29 Aug 2023 09:23:49 -0500
Subject: [PATCH 2/3] Use unique in-memory database name for each SQLite test
---
tests/test_databases.py | 20 ++++++++++++++++----
1 file changed, 16 insertions(+), 4 deletions(-)
diff --git a/tests/test_databases.py b/tests/test_databases.py
index 6d94822..83f0a03 100644
--- a/tests/test_databases.py
+++ b/tests/test_databases.py
@@ -1549,7 +1549,10 @@ async def test_mapping_property_interface(database_url):
@async_adapter
async def test_should_not_maintain_ref_when_no_cache_param():
- async with Database("sqlite:///file::memory:", uri=True) as database:
+ async with Database(
+ "sqlite:///file::memory:test_should_not_maintain_ref_when_no_cache_param",
+ uri=True,
+ ) as database:
query = sqlalchemy.schema.CreateTable(notes)
await database.execute(query)
@@ -1561,7 +1564,10 @@ async def test_should_not_maintain_ref_when_no_cache_param():
@async_adapter
async def test_should_maintain_ref_when_cache_param():
- async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
+ async with Database(
+ "sqlite:///file::memory:test_should_maintain_ref_when_cache_param?cache=shared",
+ uri=True,
+ ) as database:
query = sqlalchemy.schema.CreateTable(notes)
await database.execute(query)
@@ -1577,7 +1583,10 @@ async def test_should_maintain_ref_when_cache_param():
@async_adapter
async def test_should_remove_ref_on_disconnect():
- async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
+ async with Database(
+ "sqlite:///file::memory:test_should_remove_ref_on_disconnect?cache=shared",
+ uri=True,
+ ) as database:
query = sqlalchemy.schema.CreateTable(notes)
await database.execute(query)
@@ -1588,7 +1597,10 @@ async def test_should_remove_ref_on_disconnect():
# Run garbage collection to reset the database if we dropped the reference
gc.collect()
- async with Database("sqlite:///file::memory:?cache=shared", uri=True) as database:
+ async with Database(
+ "sqlite:///file::memory:test_should_remove_ref_on_disconnect?cache=shared",
+ uri=True,
+ ) as database:
query = notes.select()
with pytest.raises(sqlite3.OperationalError):
await database.fetch_all(query=query)
From b7d2652f9c36a8daf9478946c6b0fdee26532d4e Mon Sep 17 00:00:00 2001
From: Zanie <contact@zanie.dev>
Date: Tue, 29 Aug 2023 10:39:57 -0500
Subject: [PATCH 3/3] Run garbage collection after every test
---
tests/test_databases.py | 11 +++++++----
1 file changed, 7 insertions(+), 4 deletions(-)
diff --git a/tests/test_databases.py b/tests/test_databases.py
index 83f0a03..144691b 100644
--- a/tests/test_databases.py
+++ b/tests/test_databases.py
@@ -115,6 +115,9 @@ def create_test_database():
engine = sqlalchemy.create_engine(url)
metadata.drop_all(engine)
+ # Run garbage collection to ensure any in-memory databases are dropped
+ gc.collect()
+
def async_adapter(wrapped_func):
"""
@@ -1550,7 +1553,7 @@ async def test_mapping_property_interface(database_url):
@async_adapter
async def test_should_not_maintain_ref_when_no_cache_param():
async with Database(
- "sqlite:///file::memory:test_should_not_maintain_ref_when_no_cache_param",
+ "sqlite:///file::memory:",
uri=True,
) as database:
query = sqlalchemy.schema.CreateTable(notes)
@@ -1565,7 +1568,7 @@ async def test_should_not_maintain_ref_when_no_cache_param():
@async_adapter
async def test_should_maintain_ref_when_cache_param():
async with Database(
- "sqlite:///file::memory:test_should_maintain_ref_when_cache_param?cache=shared",
+ "sqlite:///file::memory:?cache=shared",
uri=True,
) as database:
query = sqlalchemy.schema.CreateTable(notes)
@@ -1584,7 +1587,7 @@ async def test_should_maintain_ref_when_cache_param():
@async_adapter
async def test_should_remove_ref_on_disconnect():
async with Database(
- "sqlite:///file::memory:test_should_remove_ref_on_disconnect?cache=shared",
+ "sqlite:///file::memory:?cache=shared",
uri=True,
) as database:
query = sqlalchemy.schema.CreateTable(notes)
@@ -1598,7 +1601,7 @@ async def test_should_remove_ref_on_disconnect():
gc.collect()
async with Database(
- "sqlite:///file::memory:test_should_remove_ref_on_disconnect?cache=shared",
+ "sqlite:///file::memory:?cache=shared",
uri=True,
) as database:
query = notes.select()