| 1 |
from datasette.app import Datasette |
| 2 |
from datasette.utils.sqlite import sqlite3 |
| 3 |
from datasette.utils.testing import TestClient |
| 4 |
import click |
| 5 |
import contextlib |
| 6 |
import itertools |
| 7 |
import json |
| 8 |
import os |
| 9 |
import pathlib |
| 10 |
import pytest |
| 11 |
import random |
| 12 |
import string |
| 13 |
import tempfile |
| 14 |
import textwrap |
| 15 |
|
| 16 |
|
| 17 |
# This temp file is used by one of the plugin config tests |
| 18 |
TEMP_PLUGIN_SECRET_FILE = os.path.join(tempfile.gettempdir(), "plugin-secret") |
| 19 |
|
| 20 |
PLUGINS_DIR = str(pathlib.Path(__file__).parent / "plugins") |
| 21 |
|
| 22 |
EXPECTED_PLUGINS = [ |
| 23 |
{ |
| 24 |
"name": "messages_output_renderer.py", |
| 25 |
"static": False, |
| 26 |
"templates": False, |
| 27 |
"version": None, |
| 28 |
"hooks": ["register_output_renderer"], |
| 29 |
}, |
| 30 |
{ |
| 31 |
"name": "my_plugin.py", |
| 32 |
"static": False, |
| 33 |
"templates": False, |
| 34 |
"version": None, |
| 35 |
"hooks": [ |
| 36 |
"actor_from_request", |
| 37 |
"asgi_wrapper", |
| 38 |
"canned_queries", |
| 39 |
"database_actions", |
| 40 |
"extra_body_script", |
| 41 |
"extra_css_urls", |
| 42 |
"extra_js_urls", |
| 43 |
"extra_template_vars", |
| 44 |
"forbidden", |
| 45 |
"menu_links", |
| 46 |
"permission_allowed", |
| 47 |
"prepare_connection", |
| 48 |
"prepare_jinja2_environment", |
| 49 |
"register_facet_classes", |
| 50 |
"register_magic_parameters", |
| 51 |
"register_routes", |
| 52 |
"render_cell", |
| 53 |
"skip_csrf", |
| 54 |
"startup", |
| 55 |
"table_actions", |
| 56 |
], |
| 57 |
}, |
| 58 |
{ |
| 59 |
"name": "my_plugin_2.py", |
| 60 |
"static": False, |
| 61 |
"templates": False, |
| 62 |
"version": None, |
| 63 |
"hooks": [ |
| 64 |
"actor_from_request", |
| 65 |
"asgi_wrapper", |
| 66 |
"canned_queries", |
| 67 |
"extra_js_urls", |
| 68 |
"extra_template_vars", |
| 69 |
"handle_exception", |
| 70 |
"menu_links", |
| 71 |
"permission_allowed", |
| 72 |
"prepare_jinja2_environment", |
| 73 |
"register_routes", |
| 74 |
"render_cell", |
| 75 |
"startup", |
| 76 |
"table_actions", |
| 77 |
], |
| 78 |
}, |
| 79 |
{ |
| 80 |
"name": "register_output_renderer.py", |
| 81 |
"static": False, |
| 82 |
"templates": False, |
| 83 |
"version": None, |
| 84 |
"hooks": ["register_output_renderer"], |
| 85 |
}, |
| 86 |
{ |
| 87 |
"name": "sleep_sql_function.py", |
| 88 |
"static": False, |
| 89 |
"templates": False, |
| 90 |
"version": None, |
| 91 |
"hooks": ["prepare_connection"], |
| 92 |
}, |
| 93 |
{ |
| 94 |
"name": "view_name.py", |
| 95 |
"static": False, |
| 96 |
"templates": False, |
| 97 |
"version": None, |
| 98 |
"hooks": ["extra_template_vars"], |
| 99 |
}, |
| 100 |
] |
| 101 |
|
| 102 |
|
| 103 |
@contextlib.contextmanager |
| 104 |
def make_app_client( |
| 105 |
sql_time_limit_ms=None, |
| 106 |
max_returned_rows=None, |
| 107 |
cors=False, |
| 108 |
memory=False, |
| 109 |
settings=None, |
| 110 |
filename="fixtures.db", |
| 111 |
is_immutable=False, |
| 112 |
extra_databases=None, |
| 113 |
inspect_data=None, |
| 114 |
static_mounts=None, |
| 115 |
template_dir=None, |
| 116 |
metadata=None, |
| 117 |
crossdb=False, |
| 118 |
): |
| 119 |
with tempfile.TemporaryDirectory() as tmpdir: |
| 120 |
filepath = os.path.join(tmpdir, filename) |
| 121 |
if is_immutable: |
| 122 |
files = [] |
| 123 |
immutables = [filepath] |
| 124 |
else: |
| 125 |
files = [filepath] |
| 126 |
immutables = [] |
| 127 |
conn = sqlite3.connect(filepath) |
| 128 |
conn.executescript(TABLES) |
| 129 |
for sql, params in TABLE_PARAMETERIZED_SQL: |
| 130 |
with conn: |
| 131 |
conn.execute(sql, params) |
| 132 |
if extra_databases is not None: |
| 133 |
for extra_filename, extra_sql in extra_databases.items(): |
| 134 |
extra_filepath = os.path.join(tmpdir, extra_filename) |
| 135 |
sqlite3.connect(extra_filepath).executescript(extra_sql) |
| 136 |
# Insert at start to help test /-/databases ordering: |
| 137 |
files.insert(0, extra_filepath) |
| 138 |
os.chdir(os.path.dirname(filepath)) |
| 139 |
settings = settings or {} |
| 140 |
for key, value in { |
| 141 |
"default_page_size": 50, |
| 142 |
"max_returned_rows": max_returned_rows or 100, |
| 143 |
"sql_time_limit_ms": sql_time_limit_ms or 200, |
| 144 |
# Default is 3 but this results in "too many open files" |
| 145 |
# errors when running the full test suite: |
| 146 |
"num_sql_threads": 1, |
| 147 |
}.items(): |
| 148 |
if key not in settings: |
| 149 |
settings[key] = value |
| 150 |
ds = Datasette( |
| 151 |
files, |
| 152 |
immutables=immutables, |
| 153 |
memory=memory, |
| 154 |
cors=cors, |
| 155 |
metadata=metadata or METADATA, |
| 156 |
plugins_dir=PLUGINS_DIR, |
| 157 |
settings=settings, |
| 158 |
inspect_data=inspect_data, |
| 159 |
static_mounts=static_mounts, |
| 160 |
template_dir=template_dir, |
| 161 |
crossdb=crossdb, |
| 162 |
) |
| 163 |
yield TestClient(ds) |
| 164 |
# Close as many database connections as possible |
| 165 |
# to try and avoid too many open files error |
| 166 |
for db in ds.databases.values(): |
| 167 |
if not db.is_memory: |
| 168 |
db.close() |
| 169 |
|
| 170 |
|
| 171 |
@pytest.fixture(scope="session") |
| 172 |
def app_client(): |
| 173 |
with make_app_client() as client: |
| 174 |
yield client |
| 175 |
|
| 176 |
|
| 177 |
@pytest.fixture(scope="session") |
| 178 |
def app_client_no_files(): |
| 179 |
ds = Datasette([]) |
| 180 |
yield TestClient(ds) |
| 181 |
|
| 182 |
|
| 183 |
@pytest.fixture(scope="session") |
| 184 |
def app_client_base_url_prefix(): |
| 185 |
with make_app_client(settings={"base_url": "/prefix/"}) as client: |
| 186 |
yield client |
| 187 |
|
| 188 |
|
| 189 |
@pytest.fixture(scope="session") |
| 190 |
def app_client_two_attached_databases(): |
| 191 |
with make_app_client( |
| 192 |
extra_databases={"extra database.db": EXTRA_DATABASE_SQL} |
| 193 |
) as client: |
| 194 |
yield client |
| 195 |
|
| 196 |
|
| 197 |
@pytest.fixture(scope="session") |
| 198 |
def app_client_two_attached_databases_crossdb_enabled(): |
| 199 |
with make_app_client( |
| 200 |
extra_databases={"extra database.db": EXTRA_DATABASE_SQL}, |
| 201 |
crossdb=True, |
| 202 |
) as client: |
| 203 |
yield client |
| 204 |
|
| 205 |
|
| 206 |
@pytest.fixture(scope="session") |
| 207 |
def app_client_conflicting_database_names(): |
| 208 |
with make_app_client( |
| 209 |
extra_databases={"foo.db": EXTRA_DATABASE_SQL, "foo-bar.db": EXTRA_DATABASE_SQL} |
| 210 |
) as client: |
| 211 |
yield client |
| 212 |
|
| 213 |
|
| 214 |
@pytest.fixture(scope="session") |
| 215 |
def app_client_two_attached_databases_one_immutable(): |
| 216 |
with make_app_client( |
| 217 |
is_immutable=True, extra_databases={"extra database.db": EXTRA_DATABASE_SQL} |
| 218 |
) as client: |
| 219 |
yield client |
| 220 |
|
| 221 |
|
| 222 |
@pytest.fixture(scope="session") |
| 223 |
def app_client_with_trace(): |
| 224 |
with make_app_client(settings={"trace_debug": True}, is_immutable=True) as client: |
| 225 |
yield client |
| 226 |
|
| 227 |
|
| 228 |
@pytest.fixture(scope="session") |
| 229 |
def app_client_shorter_time_limit(): |
| 230 |
with make_app_client(20) as client: |
| 231 |
yield client |
| 232 |
|
| 233 |
|
| 234 |
@pytest.fixture(scope="session") |
| 235 |
def app_client_returned_rows_matches_page_size(): |
| 236 |
with make_app_client(max_returned_rows=50) as client: |
| 237 |
yield client |
| 238 |
|
| 239 |
|
| 240 |
@pytest.fixture(scope="session") |
| 241 |
def app_client_larger_cache_size(): |
| 242 |
with make_app_client(settings={"cache_size_kb": 2500}) as client: |
| 243 |
yield client |
| 244 |
|
| 245 |
|
| 246 |
@pytest.fixture(scope="session") |
| 247 |
def app_client_csv_max_mb_one(): |
| 248 |
with make_app_client(settings={"max_csv_mb": 1}) as client: |
| 249 |
yield client |
| 250 |
|
| 251 |
|
| 252 |
@pytest.fixture(scope="session") |
| 253 |
def app_client_with_dot(): |
| 254 |
with make_app_client(filename="fixtures.dot.db") as client: |
| 255 |
yield client |
| 256 |
|
| 257 |
|
| 258 |
@pytest.fixture(scope="session") |
| 259 |
def app_client_with_cors(): |
| 260 |
with make_app_client(is_immutable=True, cors=True) as client: |
| 261 |
yield client |
| 262 |
|
| 263 |
|
| 264 |
@pytest.fixture(scope="session") |
| 265 |
def app_client_immutable_and_inspect_file(): |
| 266 |
inspect_data = {"fixtures": {"tables": {"sortable": {"count": 100}}}} |
| 267 |
with make_app_client(is_immutable=True, inspect_data=inspect_data) as client: |
| 268 |
yield client |
| 269 |
|
| 270 |
|
| 271 |
def generate_compound_rows(num): |
| 272 |
for a, b, c in itertools.islice( |
| 273 |
itertools.product(string.ascii_lowercase, repeat=3), num |
| 274 |
): |
| 275 |
yield a, b, c, f"{a}-{b}-{c}" |
| 276 |
|
| 277 |
|
| 278 |
def generate_sortable_rows(num): |
| 279 |
rand = random.Random(42) |
| 280 |
for a, b in itertools.islice( |
| 281 |
itertools.product(string.ascii_lowercase, repeat=2), num |
| 282 |
): |
| 283 |
yield { |
| 284 |
"pk1": a, |
| 285 |
"pk2": b, |
| 286 |
"content": f"{a}-{b}", |
| 287 |
"sortable": rand.randint(-100, 100), |
| 288 |
"sortable_with_nulls": rand.choice([None, rand.random(), rand.random()]), |
| 289 |
"sortable_with_nulls_2": rand.choice([None, rand.random(), rand.random()]), |
| 290 |
"text": rand.choice(["$null", "$blah"]), |
| 291 |
} |
| 292 |
|
| 293 |
|
| 294 |
METADATA = { |
| 295 |
"title": "Datasette Fixtures", |
| 296 |
"description_html": 'An example SQLite database demonstrating Datasette. <a href="/login-as-root">Sign in as root user</a>', |
| 297 |
"license": "Apache License 2.0", |
| 298 |
"license_url": "https://github.com/simonw/datasette/blob/main/LICENSE", |
| 299 |
"source": "tests/fixtures.py", |
| 300 |
"source_url": "https://github.com/simonw/datasette/blob/main/tests/fixtures.py", |
| 301 |
"about": "About Datasette", |
| 302 |
"about_url": "https://github.com/simonw/datasette", |
| 303 |
"extra_css_urls": ["/static/extra-css-urls.css"], |
| 304 |
"plugins": { |
| 305 |
"name-of-plugin": {"depth": "root"}, |
| 306 |
"env-plugin": {"foo": {"$env": "FOO_ENV"}}, |
| 307 |
"env-plugin-list": [{"in_a_list": {"$env": "FOO_ENV"}}], |
| 308 |
"file-plugin": {"foo": {"$file": TEMP_PLUGIN_SECRET_FILE}}, |
| 309 |
}, |
| 310 |
"databases": { |
| 311 |
"fixtures": { |
| 312 |
"description": "Test tables description", |
| 313 |
"plugins": {"name-of-plugin": {"depth": "database"}}, |
| 314 |
"tables": { |
| 315 |
"simple_primary_key": { |
| 316 |
"description_html": "Simple <em>primary</em> key", |
| 317 |
"title": "This <em>HTML</em> is escaped", |
| 318 |
"plugins": { |
| 319 |
"name-of-plugin": { |
| 320 |
"depth": "table", |
| 321 |
"special": "this-is-simple_primary_key", |
| 322 |
} |
| 323 |
}, |
| 324 |
}, |
| 325 |
"sortable": { |
| 326 |
"sortable_columns": [ |
| 327 |
"sortable", |
| 328 |
"sortable_with_nulls", |
| 329 |
"sortable_with_nulls_2", |
| 330 |
"text", |
| 331 |
], |
| 332 |
"plugins": {"name-of-plugin": {"depth": "table"}}, |
| 333 |
}, |
| 334 |
"no_primary_key": {"sortable_columns": [], "hidden": True}, |
| 335 |
"units": {"units": {"distance": "m", "frequency": "Hz"}}, |
| 336 |
"primary_key_multiple_columns_explicit_label": { |
| 337 |
"label_column": "content2" |
| 338 |
}, |
| 339 |
"simple_view": {"sortable_columns": ["content"]}, |
| 340 |
"searchable_view_configured_by_metadata": { |
| 341 |
"fts_table": "searchable_fts", |
| 342 |
"fts_pk": "pk", |
| 343 |
}, |
| 344 |
"roadside_attractions": { |
| 345 |
"columns": { |
| 346 |
"name": "The name of the attraction", |
| 347 |
"address": "The street address for the attraction", |
| 348 |
} |
| 349 |
}, |
| 350 |
"attraction_characteristic": {"sort_desc": "pk"}, |
| 351 |
"facet_cities": {"sort": "name"}, |
| 352 |
"paginated_view": {"size": 25}, |
| 353 |
}, |
| 354 |
"queries": { |
| 355 |
"𝐜𝐢𝐭𝐢𝐞𝐬": "select id, name from facet_cities order by id limit 1;", |
| 356 |
"pragma_cache_size": "PRAGMA cache_size;", |
| 357 |
"magic_parameters": { |
| 358 |
"sql": "select :_header_user_agent as user_agent, :_now_datetime_utc as datetime", |
| 359 |
}, |
| 360 |
"neighborhood_search": { |
| 361 |
"sql": textwrap.dedent( |
| 362 |
""" |
| 363 |
select _neighborhood, facet_cities.name, state |
| 364 |
from facetable |
| 365 |
join facet_cities |
| 366 |
on facetable._city_id = facet_cities.id |
| 367 |
where _neighborhood like '%' || :text || '%' |
| 368 |
order by _neighborhood; |
| 369 |
""" |
| 370 |
), |
| 371 |
"title": "Search neighborhoods", |
| 372 |
"description_html": "<b>Demonstrating</b> simple like search", |
| 373 |
"fragment": "fragment-goes-here", |
| 374 |
"hide_sql": True, |
| 375 |
}, |
| 376 |
}, |
| 377 |
} |
| 378 |
}, |
| 379 |
} |
| 380 |
|
| 381 |
TABLES = ( |
| 382 |
""" |
| 383 |
CREATE TABLE simple_primary_key ( |
| 384 |
id varchar(30) primary key, |
| 385 |
content text |
| 386 |
); |
| 387 |
|
| 388 |
CREATE TABLE primary_key_multiple_columns ( |
| 389 |
id varchar(30) primary key, |
| 390 |
content text, |
| 391 |
content2 text |
| 392 |
); |
| 393 |
|
| 394 |
CREATE TABLE primary_key_multiple_columns_explicit_label ( |
| 395 |
id varchar(30) primary key, |
| 396 |
content text, |
| 397 |
content2 text |
| 398 |
); |
| 399 |
|
| 400 |
CREATE TABLE compound_primary_key ( |
| 401 |
pk1 varchar(30), |
| 402 |
pk2 varchar(30), |
| 403 |
content text, |
| 404 |
PRIMARY KEY (pk1, pk2) |
| 405 |
); |
| 406 |
|
| 407 |
INSERT INTO compound_primary_key VALUES ('a', 'b', 'c'); |
| 408 |
INSERT INTO compound_primary_key VALUES ('a/b', '.c-d', 'c'); |
| 409 |
|
| 410 |
CREATE TABLE compound_three_primary_keys ( |
| 411 |
pk1 varchar(30), |
| 412 |
pk2 varchar(30), |
| 413 |
pk3 varchar(30), |
| 414 |
content text, |
| 415 |
PRIMARY KEY (pk1, pk2, pk3) |
| 416 |
); |
| 417 |
CREATE INDEX idx_compound_three_primary_keys_content ON compound_three_primary_keys(content); |
| 418 |
|
| 419 |
CREATE TABLE foreign_key_references ( |
| 420 |
pk varchar(30) primary key, |
| 421 |
foreign_key_with_label varchar(30), |
| 422 |
foreign_key_with_blank_label varchar(30), |
| 423 |
foreign_key_with_no_label varchar(30), |
| 424 |
foreign_key_compound_pk1 varchar(30), |
| 425 |
foreign_key_compound_pk2 varchar(30), |
| 426 |
FOREIGN KEY (foreign_key_with_label) REFERENCES simple_primary_key(id), |
| 427 |
FOREIGN KEY (foreign_key_with_blank_label) REFERENCES simple_primary_key(id), |
| 428 |
FOREIGN KEY (foreign_key_with_no_label) REFERENCES primary_key_multiple_columns(id) |
| 429 |
FOREIGN KEY (foreign_key_compound_pk1, foreign_key_compound_pk2) REFERENCES compound_primary_key(pk1, pk2) |
| 430 |
); |
| 431 |
|
| 432 |
CREATE TABLE sortable ( |
| 433 |
pk1 varchar(30), |
| 434 |
pk2 varchar(30), |
| 435 |
content text, |
| 436 |
sortable integer, |
| 437 |
sortable_with_nulls real, |
| 438 |
sortable_with_nulls_2 real, |
| 439 |
text text, |
| 440 |
PRIMARY KEY (pk1, pk2) |
| 441 |
); |
| 442 |
|
| 443 |
CREATE TABLE no_primary_key ( |
| 444 |
content text, |
| 445 |
a text, |
| 446 |
b text, |
| 447 |
c text |
| 448 |
); |
| 449 |
|
| 450 |
CREATE TABLE [123_starts_with_digits] ( |
| 451 |
content text |
| 452 |
); |
| 453 |
|
| 454 |
CREATE VIEW paginated_view AS |
| 455 |
SELECT |
| 456 |
content, |
| 457 |
'- ' || content || ' -' AS content_extra |
| 458 |
FROM no_primary_key; |
| 459 |
|
| 460 |
CREATE TABLE "Table With Space In Name" ( |
| 461 |
pk varchar(30) primary key, |
| 462 |
content text |
| 463 |
); |
| 464 |
|
| 465 |
CREATE TABLE "table/with/slashes.csv" ( |
| 466 |
pk varchar(30) primary key, |
| 467 |
content text |
| 468 |
); |
| 469 |
|
| 470 |
CREATE TABLE "complex_foreign_keys" ( |
| 471 |
pk varchar(30) primary key, |
| 472 |
f1 text, |
| 473 |
f2 text, |
| 474 |
f3 text, |
| 475 |
FOREIGN KEY ("f1") REFERENCES [simple_primary_key](id), |
| 476 |
FOREIGN KEY ("f2") REFERENCES [simple_primary_key](id), |
| 477 |
FOREIGN KEY ("f3") REFERENCES [simple_primary_key](id) |
| 478 |
); |
| 479 |
|
| 480 |
CREATE TABLE "custom_foreign_key_label" ( |
| 481 |
pk varchar(30) primary key, |
| 482 |
foreign_key_with_custom_label text, |
| 483 |
FOREIGN KEY ("foreign_key_with_custom_label") REFERENCES [primary_key_multiple_columns_explicit_label](id) |
| 484 |
); |
| 485 |
|
| 486 |
CREATE TABLE units ( |
| 487 |
pk integer primary key, |
| 488 |
distance int, |
| 489 |
frequency int |
| 490 |
); |
| 491 |
|
| 492 |
INSERT INTO units VALUES (1, 1, 100); |
| 493 |
INSERT INTO units VALUES (2, 5000, 2500); |
| 494 |
INSERT INTO units VALUES (3, 100000, 75000); |
| 495 |
|
| 496 |
CREATE TABLE tags ( |
| 497 |
tag TEXT PRIMARY KEY |
| 498 |
); |
| 499 |
|
| 500 |
CREATE TABLE searchable ( |
| 501 |
pk integer primary key, |
| 502 |
text1 text, |
| 503 |
text2 text, |
| 504 |
[name with . and spaces] text |
| 505 |
); |
| 506 |
|
| 507 |
CREATE TABLE searchable_tags ( |
| 508 |
searchable_id integer, |
| 509 |
tag text, |
| 510 |
PRIMARY KEY (searchable_id, tag), |
| 511 |
FOREIGN KEY (searchable_id) REFERENCES searchable(pk), |
| 512 |
FOREIGN KEY (tag) REFERENCES tags(tag) |
| 513 |
); |
| 514 |
|
| 515 |
INSERT INTO searchable VALUES (1, 'barry cat', 'terry dog', 'panther'); |
| 516 |
INSERT INTO searchable VALUES (2, 'terry dog', 'sara weasel', 'puma'); |
| 517 |
|
| 518 |
INSERT INTO tags VALUES ("canine"); |
| 519 |
INSERT INTO tags VALUES ("feline"); |
| 520 |
|
| 521 |
INSERT INTO searchable_tags (searchable_id, tag) VALUES |
| 522 |
(1, "feline"), |
| 523 |
(2, "canine") |
| 524 |
; |
| 525 |
|
| 526 |
CREATE VIRTUAL TABLE "searchable_fts" |
| 527 |
USING FTS4 (text1, text2, [name with . and spaces], content="searchable"); |
| 528 |
INSERT INTO "searchable_fts" (rowid, text1, text2, [name with . and spaces]) |
| 529 |
SELECT rowid, text1, text2, [name with . and spaces] FROM searchable; |
| 530 |
|
| 531 |
CREATE TABLE [select] ( |
| 532 |
[group] text, |
| 533 |
[having] text, |
| 534 |
[and] text, |
| 535 |
[json] text |
| 536 |
); |
| 537 |
INSERT INTO [select] VALUES ('group', 'having', 'and', |
| 538 |
'{"href": "http://example.com/", "label":"Example"}' |
| 539 |
); |
| 540 |
|
| 541 |
CREATE TABLE infinity ( |
| 542 |
value REAL |
| 543 |
); |
| 544 |
INSERT INTO infinity VALUES |
| 545 |
(1e999), |
| 546 |
(-1e999), |
| 547 |
(1.5) |
| 548 |
; |
| 549 |
|
| 550 |
CREATE TABLE facet_cities ( |
| 551 |
id integer primary key, |
| 552 |
name text |
| 553 |
); |
| 554 |
INSERT INTO facet_cities (id, name) VALUES |
| 555 |
(1, 'San Francisco'), |
| 556 |
(2, 'Los Angeles'), |
| 557 |
(3, 'Detroit'), |
| 558 |
(4, 'Memnonia') |
| 559 |
; |
| 560 |
|
| 561 |
CREATE TABLE facetable ( |
| 562 |
pk integer primary key, |
| 563 |
created text, |
| 564 |
planet_int integer, |
| 565 |
on_earth integer, |
| 566 |
state text, |
| 567 |
_city_id integer, |
| 568 |
_neighborhood text, |
| 569 |
tags text, |
| 570 |
complex_array text, |
| 571 |
distinct_some_null, |
| 572 |
n text, |
| 573 |
FOREIGN KEY ("_city_id") REFERENCES [facet_cities](id) |
| 574 |
); |
| 575 |
INSERT INTO facetable |
| 576 |
(created, planet_int, on_earth, state, _city_id, _neighborhood, tags, complex_array, distinct_some_null, n) |
| 577 |
VALUES |
| 578 |
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Mission', '["tag1", "tag2"]', '[{"foo": "bar"}]', 'one', 'n1'), |
| 579 |
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Dogpatch', '["tag1", "tag3"]', '[]', 'two', 'n2'), |
| 580 |
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'SOMA', '[]', '[]', null, null), |
| 581 |
("2019-01-14 08:00:00", 1, 1, 'CA', 1, 'Tenderloin', '[]', '[]', null, null), |
| 582 |
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Bernal Heights', '[]', '[]', null, null), |
| 583 |
("2019-01-15 08:00:00", 1, 1, 'CA', 1, 'Hayes Valley', '[]', '[]', null, null), |
| 584 |
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Hollywood', '[]', '[]', null, null), |
| 585 |
("2019-01-15 08:00:00", 1, 1, 'CA', 2, 'Downtown', '[]', '[]', null, null), |
| 586 |
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Los Feliz', '[]', '[]', null, null), |
| 587 |
("2019-01-16 08:00:00", 1, 1, 'CA', 2, 'Koreatown', '[]', '[]', null, null), |
| 588 |
("2019-01-16 08:00:00", 1, 1, 'MI', 3, 'Downtown', '[]', '[]', null, null), |
| 589 |
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Greektown', '[]', '[]', null, null), |
| 590 |
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Corktown', '[]', '[]', null, null), |
| 591 |
("2019-01-17 08:00:00", 1, 1, 'MI', 3, 'Mexicantown', '[]', '[]', null, null), |
| 592 |
("2019-01-17 08:00:00", 2, 0, 'MC', 4, 'Arcadia Planitia', '[]', '[]', null, null) |
| 593 |
; |
| 594 |
|
| 595 |
CREATE TABLE binary_data ( |
| 596 |
data BLOB |
| 597 |
); |
| 598 |
|
| 599 |
-- Many 2 Many demo: roadside attractions! |
| 600 |
|
| 601 |
CREATE TABLE roadside_attractions ( |
| 602 |
pk integer primary key, |
| 603 |
name text, |
| 604 |
address text, |
| 605 |
url text, |
| 606 |
latitude real, |
| 607 |
longitude real |
| 608 |
); |
| 609 |
INSERT INTO roadside_attractions VALUES ( |
| 610 |
1, "The Mystery Spot", "465 Mystery Spot Road, Santa Cruz, CA 95065", "https://www.mysteryspot.com/", |
| 611 |
37.0167, -122.0024 |
| 612 |
); |
| 613 |
INSERT INTO roadside_attractions VALUES ( |
| 614 |
2, "Winchester Mystery House", "525 South Winchester Boulevard, San Jose, CA 95128", "https://winchestermysteryhouse.com/", |
| 615 |
37.3184, -121.9511 |
| 616 |
); |
| 617 |
INSERT INTO roadside_attractions VALUES ( |
| 618 |
3, "Burlingame Museum of PEZ Memorabilia", "214 California Drive, Burlingame, CA 94010", null, |
| 619 |
37.5793, -122.3442 |
| 620 |
); |
| 621 |
INSERT INTO roadside_attractions VALUES ( |
| 622 |
4, "Bigfoot Discovery Museum", "5497 Highway 9, Felton, CA 95018", "https://www.bigfootdiscoveryproject.com/", |
| 623 |
37.0414, -122.0725 |
| 624 |
); |
| 625 |
|
| 626 |
CREATE TABLE attraction_characteristic ( |
| 627 |
pk integer primary key, |
| 628 |
name text |
| 629 |
); |
| 630 |
INSERT INTO attraction_characteristic VALUES ( |
| 631 |
1, "Museum" |
| 632 |
); |
| 633 |
INSERT INTO attraction_characteristic VALUES ( |
| 634 |
2, "Paranormal" |
| 635 |
); |
| 636 |
|
| 637 |
CREATE TABLE roadside_attraction_characteristics ( |
| 638 |
attraction_id INTEGER REFERENCES roadside_attractions(pk), |
| 639 |
characteristic_id INTEGER REFERENCES attraction_characteristic(pk) |
| 640 |
); |
| 641 |
INSERT INTO roadside_attraction_characteristics VALUES ( |
| 642 |
1, 2 |
| 643 |
); |
| 644 |
INSERT INTO roadside_attraction_characteristics VALUES ( |
| 645 |
2, 2 |
| 646 |
); |
| 647 |
INSERT INTO roadside_attraction_characteristics VALUES ( |
| 648 |
4, 2 |
| 649 |
); |
| 650 |
INSERT INTO roadside_attraction_characteristics VALUES ( |
| 651 |
3, 1 |
| 652 |
); |
| 653 |
INSERT INTO roadside_attraction_characteristics VALUES ( |
| 654 |
4, 1 |
| 655 |
); |
| 656 |
|
| 657 |
INSERT INTO simple_primary_key VALUES (1, 'hello'); |
| 658 |
INSERT INTO simple_primary_key VALUES (2, 'world'); |
| 659 |
INSERT INTO simple_primary_key VALUES (3, ''); |
| 660 |
INSERT INTO simple_primary_key VALUES (4, 'RENDER_CELL_DEMO'); |
| 661 |
INSERT INTO simple_primary_key VALUES (5, 'RENDER_CELL_ASYNC'); |
| 662 |
|
| 663 |
INSERT INTO primary_key_multiple_columns VALUES (1, 'hey', 'world'); |
| 664 |
INSERT INTO primary_key_multiple_columns_explicit_label VALUES (1, 'hey', 'world2'); |
| 665 |
|
| 666 |
INSERT INTO foreign_key_references VALUES (1, 1, 3, 1, 'a', 'b'); |
| 667 |
INSERT INTO foreign_key_references VALUES (2, null, null, null, null, null); |
| 668 |
|
| 669 |
INSERT INTO complex_foreign_keys VALUES (1, 1, 2, 1); |
| 670 |
INSERT INTO custom_foreign_key_label VALUES (1, 1); |
| 671 |
|
| 672 |
INSERT INTO [table/with/slashes.csv] VALUES (3, 'hey'); |
| 673 |
|
| 674 |
CREATE VIEW simple_view AS |
| 675 |
SELECT content, upper(content) AS upper_content FROM simple_primary_key; |
| 676 |
|
| 677 |
CREATE VIEW searchable_view AS |
| 678 |
SELECT * from searchable; |
| 679 |
|
| 680 |
CREATE VIEW searchable_view_configured_by_metadata AS |
| 681 |
SELECT * from searchable; |
| 682 |
|
| 683 |
""" |
| 684 |
+ "\n".join( |
| 685 |
[ |
| 686 |
'INSERT INTO no_primary_key VALUES ({i}, "a{i}", "b{i}", "c{i}");'.format( |
| 687 |
i=i + 1 |
| 688 |
) |
| 689 |
for i in range(201) |
| 690 |
] |
| 691 |
) |
| 692 |
+ "\n".join( |
| 693 |
[ |
| 694 |
'INSERT INTO compound_three_primary_keys VALUES ("{a}", "{b}", "{c}", "{content}");'.format( |
| 695 |
a=a, b=b, c=c, content=content |
| 696 |
) |
| 697 |
for a, b, c, content in generate_compound_rows(1001) |
| 698 |
] |
| 699 |
) |
| 700 |
+ "\n".join( |
| 701 |
[ |
| 702 |
"""INSERT INTO sortable VALUES ( |
| 703 |
"{pk1}", "{pk2}", "{content}", {sortable}, |
| 704 |
{sortable_with_nulls}, {sortable_with_nulls_2}, "{text}"); |
| 705 |
""".format( |
| 706 |
**row |
| 707 |
).replace( |
| 708 |
"None", "null" |
| 709 |
) |
| 710 |
for row in generate_sortable_rows(201) |
| 711 |
] |
| 712 |
) |
| 713 |
) |
| 714 |
TABLE_PARAMETERIZED_SQL = [ |
| 715 |
("insert into binary_data (data) values (?);", [b"\x15\x1c\x02\xc7\xad\x05\xfe"]), |
| 716 |
("insert into binary_data (data) values (?);", [b"\x15\x1c\x03\xc7\xad\x05\xfe"]), |
| 717 |
("insert into binary_data (data) values (null);", []), |
| 718 |
] |
| 719 |
|
| 720 |
EXTRA_DATABASE_SQL = """ |
| 721 |
CREATE TABLE searchable ( |
| 722 |
pk integer primary key, |
| 723 |
text1 text, |
| 724 |
text2 text |
| 725 |
); |
| 726 |
|
| 727 |
CREATE VIEW searchable_view AS SELECT * FROM searchable; |
| 728 |
|
| 729 |
INSERT INTO searchable VALUES (1, 'barry cat', 'terry dog'); |
| 730 |
INSERT INTO searchable VALUES (2, 'terry dog', 'sara weasel'); |
| 731 |
|
| 732 |
CREATE VIRTUAL TABLE "searchable_fts" |
| 733 |
USING FTS3 (text1, text2, content="searchable"); |
| 734 |
INSERT INTO "searchable_fts" (rowid, text1, text2) |
| 735 |
SELECT rowid, text1, text2 FROM searchable; |
| 736 |
""" |
| 737 |
|
| 738 |
|
| 739 |
def assert_permissions_checked(datasette, actions): |
| 740 |
# actions is a list of "action" or (action, resource) tuples |
| 741 |
for action in actions: |
| 742 |
if isinstance(action, str): |
| 743 |
resource = None |
| 744 |
else: |
| 745 |
action, resource = action |
| 746 |
assert [ |
| 747 |
pc |
| 748 |
for pc in datasette._permission_checks |
| 749 |
if pc["action"] == action and pc["resource"] == resource |
| 750 |
], """Missing expected permission check: action={}, resource={} |
| 751 |
Permission checks seen: {} |
| 752 |
""".format( |
| 753 |
action, |
| 754 |
resource, |
| 755 |
json.dumps(list(datasette._permission_checks), indent=4), |
| 756 |
) |
| 757 |
|
| 758 |
|
| 759 |
@click.command() |
| 760 |
@click.argument( |
| 761 |
"db_filename", |
| 762 |
default="fixtures.db", |
| 763 |
type=click.Path(file_okay=True, dir_okay=False), |
| 764 |
) |
| 765 |
@click.argument("metadata", required=False) |
| 766 |
@click.argument( |
| 767 |
"plugins_path", type=click.Path(file_okay=False, dir_okay=True), required=False |
| 768 |
) |
| 769 |
@click.option( |
| 770 |
"--recreate", |
| 771 |
is_flag=True, |
| 772 |
default=False, |
| 773 |
help="Delete and recreate database if it exists", |
| 774 |
) |
| 775 |
@click.option( |
| 776 |
"--extra-db-filename", |
| 777 |
type=click.Path(file_okay=True, dir_okay=False), |
| 778 |
help="Write out second test DB to this file", |
| 779 |
) |
| 780 |
def cli(db_filename, metadata, plugins_path, recreate, extra_db_filename): |
| 781 |
"""Write out the fixtures database used by Datasette's test suite""" |
| 782 |
if metadata and not metadata.endswith(".json"): |
| 783 |
raise click.ClickException("Metadata should end with .json") |
| 784 |
if not db_filename.endswith(".db"): |
| 785 |
raise click.ClickException("Database file should end with .db") |
| 786 |
if pathlib.Path(db_filename).exists(): |
| 787 |
if not recreate: |
| 788 |
raise click.ClickException( |
| 789 |
f"{db_filename} already exists, use --recreate to reset it" |
| 790 |
) |
| 791 |
else: |
| 792 |
pathlib.Path(db_filename).unlink() |
| 793 |
conn = sqlite3.connect(db_filename) |
| 794 |
conn.executescript(TABLES) |
| 795 |
for sql, params in TABLE_PARAMETERIZED_SQL: |
| 796 |
with conn: |
| 797 |
conn.execute(sql, params) |
| 798 |
print(f"Test tables written to {db_filename}") |
| 799 |
if metadata: |
| 800 |
with open(metadata, "w") as fp: |
| 801 |
fp.write(json.dumps(METADATA, indent=4)) |
| 802 |
print(f"- metadata written to {metadata}") |
| 803 |
if plugins_path: |
| 804 |
path = pathlib.Path(plugins_path) |
| 805 |
if not path.exists(): |
| 806 |
path.mkdir() |
| 807 |
test_plugins = pathlib.Path(__file__).parent / "plugins" |
| 808 |
for filepath in test_plugins.glob("*.py"): |
| 809 |
newpath = path / filepath.name |
| 810 |
newpath.write_text(filepath.read_text()) |
| 811 |
print(f" Wrote plugin: {newpath}") |
| 812 |
if extra_db_filename: |
| 813 |
if pathlib.Path(extra_db_filename).exists(): |
| 814 |
if not recreate: |
| 815 |
raise click.ClickException( |
| 816 |
f"{extra_db_filename} already exists, use --recreate to reset it" |
| 817 |
) |
| 818 |
else: |
| 819 |
pathlib.Path(extra_db_filename).unlink() |
| 820 |
conn = sqlite3.connect(extra_db_filename) |
| 821 |
conn.executescript(EXTRA_DATABASE_SQL) |
| 822 |
print(f"Test tables written to {extra_db_filename}") |
| 823 |
|
| 824 |
|
| 825 |
if __name__ == "__main__": |
| 826 |
cli() |