| 1 |
from datasette.cli import cli |
| 2 |
from click.testing import CliRunner |
| 3 |
import urllib |
| 4 |
import sqlite3 |
| 5 |
from .fixtures import app_client_two_attached_databases_crossdb_enabled |
| 6 |
|
| 7 |
|
| 8 |
def test_crossdb_join(app_client_two_attached_databases_crossdb_enabled): |
| 9 |
app_client = app_client_two_attached_databases_crossdb_enabled |
| 10 |
sql = """ |
| 11 |
select |
| 12 |
'extra database' as db, |
| 13 |
pk, |
| 14 |
text1, |
| 15 |
text2 |
| 16 |
from |
| 17 |
[extra database].searchable |
| 18 |
union all |
| 19 |
select |
| 20 |
'fixtures' as db, |
| 21 |
pk, |
| 22 |
text1, |
| 23 |
text2 |
| 24 |
from |
| 25 |
fixtures.searchable |
| 26 |
""" |
| 27 |
response = app_client.get( |
| 28 |
"/_memory.json?" + urllib.parse.urlencode({"sql": sql, "_shape": "array"}) |
| 29 |
) |
| 30 |
assert response.status == 200 |
| 31 |
assert response.json == [ |
| 32 |
{"db": "extra database", "pk": 1, "text1": "barry cat", "text2": "terry dog"}, |
| 33 |
{"db": "extra database", "pk": 2, "text1": "terry dog", "text2": "sara weasel"}, |
| 34 |
{"db": "fixtures", "pk": 1, "text1": "barry cat", "text2": "terry dog"}, |
| 35 |
{"db": "fixtures", "pk": 2, "text1": "terry dog", "text2": "sara weasel"}, |
| 36 |
] |
| 37 |
|
| 38 |
|
| 39 |
def test_crossdb_warning_if_too_many_databases(tmp_path_factory): |
| 40 |
db_dir = tmp_path_factory.mktemp("dbs") |
| 41 |
dbs = [] |
| 42 |
for i in range(11): |
| 43 |
path = str(db_dir / "db_{}.db".format(i)) |
| 44 |
conn = sqlite3.connect(path) |
| 45 |
conn.execute("vacuum") |
| 46 |
dbs.append(path) |
| 47 |
runner = CliRunner() |
| 48 |
result = runner.invoke( |
| 49 |
cli, |
| 50 |
[ |
| 51 |
"serve", |
| 52 |
"--crossdb", |
| 53 |
"--get", |
| 54 |
"/", |
| 55 |
] |
| 56 |
+ dbs, |
| 57 |
catch_exceptions=False, |
| 58 |
) |
| 59 |
assert ( |
| 60 |
"Warning: --crossdb only works with the first 10 attached databases" |
| 61 |
in result.stderr |
| 62 |
) |
| 63 |
|
| 64 |
|
| 65 |
def test_crossdb_attached_database_list_display( |
| 66 |
app_client_two_attached_databases_crossdb_enabled, |
| 67 |
): |
| 68 |
app_client = app_client_two_attached_databases_crossdb_enabled |
| 69 |
response = app_client.get("/_memory") |
| 70 |
for fragment in ( |
| 71 |
"databases are attached to this connection", |
| 72 |
"<li><strong>fixtures</strong> - ", |
| 73 |
"<li><strong>extra database</strong> - ", |
| 74 |
): |
| 75 |
assert fragment in response.text |