datasette/tests/test_crossdb.py
Simon Willison 5c537e0a3e Fix type annotation bugs and remove unused imports
This fixes issues introduced by the ruff commit e57f391a which converted
Optional[x] to x | None:

- Fixed datasette/app.py line 1024: Dict[id | str, Dict] -> Dict[int | str, Dict]
  (was using id built-in function instead of int type)
- Fixed datasette/app.py line 1074: Optional["Resource"] -> "Resource" | None
- Added 'from __future__ import annotations' for Python 3.10 compatibility
- Added TYPE_CHECKING blocks to avoid circular imports
- Removed dead code (unused variable assignments) from cli.py and views
- Removed unused imports flagged by ruff across multiple files
- Fixed test fixtures: moved app_client fixture imports to conftest.py
  (fixed 71 test errors caused by fixtures not being registered)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-10-26 16:03:13 -07:00

76 lines
2.1 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")
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