datasette/tests/test_crossdb.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

77 lines
2.2 KiB
Python

from datasette.cli import cli
from click.testing import CliRunner
import urllib
import sqlite3
def test_crossdb_join(app_client_two_attached_databases_crossdb_enabled):
app_client = app_client_two_attached_databases_crossdb_enabled
sql = """
select
'extra database' as db,
pk,
text1,
text2
from
[extra database].searchable
union all
select
'fixtures' as db,
pk,
text1,
text2
from
fixtures.searchable
"""
response = app_client.get(
"/_memory/-/query.json?"
+ urllib.parse.urlencode({"sql": sql, "_shape": "array"})
)
assert response.status == 200
assert response.json == [
{"db": "extra database", "pk": 1, "text1": "barry cat", "text2": "terry dog"},
{"db": "extra database", "pk": 2, "text1": "terry dog", "text2": "sara weasel"},
{"db": "fixtures", "pk": 1, "text1": "barry cat", "text2": "terry dog"},
{"db": "fixtures", "pk": 2, "text1": "terry dog", "text2": "sara weasel"},
]
def test_crossdb_warning_if_too_many_databases(tmp_path_factory):
db_dir = tmp_path_factory.mktemp("dbs")
dbs = []
for i in range(11):
path = str(db_dir / "db_{}.db".format(i))
conn = sqlite3.connect(path)
conn.execute("vacuum")
conn.close()
dbs.append(path)
runner = CliRunner()
result = runner.invoke(
cli,
[
"serve",
"--crossdb",
"--get",
"/",
]
+ dbs,
catch_exceptions=False,
)
assert (
"Warning: --crossdb only works with the first 10 attached databases"
in result.stderr
)
def test_crossdb_attached_database_list_display(
app_client_two_attached_databases_crossdb_enabled,
):
app_client = app_client_two_attached_databases_crossdb_enabled
response = app_client.get("/_memory")
response2 = app_client.get("/")
for fragment in (
"databases are attached to this connection",
"<li><strong>fixtures</strong> - ",
'<li><strong>extra database</strong> - <a href="/extra+database/-/query?sql=',
):
assert fragment in response.text