datasette/tests/utils.py
Simon Willison 35ea721469 Fixed hundreds of database connection closing warnings
From 409 warnings down to 52 warnings.

Claude Code says:

Fixed connection leaks in:
1. datasette/utils/sqlite.py - _sqlite_version() now closes connection
2. datasette/cli.py - --create flag now closes connection
3. datasette/app.py - _versions() now closes connection
4. datasette/utils/__init__.py - detect_json1() now closes connection when created internally
5. tests/conftest.py - pytest_report_header() now closes connection
6. tests/utils.py - has_load_extension() now closes connection
7. tests/fixtures.py - app_client_no_files and CLI fixtures now close connections
8. tests/test_api_write.py - ds_write fixture closes both connections
9. tests/test_cli.py - Multiple test functions now close connections
10. tests/test_config_dir.py - config_dir and config_dir_client fixtures now close connections
11. tests/test_crossdb.py - Loop connections now closed
12. tests/test_internals_database.py - Test setup connections now closed
13. tests/test_plugins.py - view_names_client fixture and test now close connections
14. tests/test_utils.py - Multiple test functions now close connections

Refs #2614
2025-12-12 22:38:04 -08:00

47 lines
1.4 KiB
Python

from datasette.utils.sqlite import sqlite3
def last_event(datasette):
events = getattr(datasette, "_tracked_events", [])
return events[-1] if events else None
def assert_footer_links(soup):
footer_links = soup.find("footer").find_all("a")
assert 4 == len(footer_links)
datasette_link, license_link, source_link, about_link = footer_links
assert "Datasette" == datasette_link.text.strip()
assert "tests/fixtures.py" == source_link.text.strip()
assert "Apache License 2.0" == license_link.text.strip()
assert "About Datasette" == about_link.text.strip()
assert "https://datasette.io/" == datasette_link["href"]
assert (
"https://github.com/simonw/datasette/blob/main/tests/fixtures.py"
== source_link["href"]
)
assert (
"https://github.com/simonw/datasette/blob/main/LICENSE" == license_link["href"]
)
assert "https://github.com/simonw/datasette" == about_link["href"]
def inner_html(soup):
html = str(soup)
# This includes the parent tag - so remove that
inner_html = html.split(">", 1)[1].rsplit("<", 1)[0]
return inner_html.strip()
def has_load_extension():
conn = sqlite3.connect(":memory:")
result = hasattr(conn, "enable_load_extension")
conn.close()
return result
def cookie_was_deleted(response, cookie):
return any(
h
for h in response.headers.get_list("set-cookie")
if h.startswith(f'{cookie}="";')
)