Test for sql_time_limit_ms + sqlite_functions mechanism

Added a unit test for the sql_time_limit_ms option.

To test this, I needed to add a custom SQLite sleep() function. I've added a
simple mechanism to the Datasette class for registering custom functions.

I also had to modify the sqlite_timelimit() function. It makes use of a magic
value, N, which is the number of SQLite virtual machine instructions that
should execute in between calls to my termination decision function.

The value of N was not finely grained enough for my test to work - so I've
added logic that says that if the time limit is less than 50ms, N is set to 1.
This got the tests working.

Refs #95
This commit is contained in:
Simon Willison 2017-11-14 18:41:03 -08:00
commit 0b8c1b0a6d
3 changed files with 37 additions and 5 deletions

View file

@ -3,6 +3,7 @@ import os
import pytest
import sqlite3
import tempfile
import time
@pytest.fixture(scope='module')
@ -12,7 +13,16 @@ def app_client():
conn = sqlite3.connect(filepath)
conn.executescript(TABLES)
os.chdir(os.path.dirname(filepath))
yield Datasette([filepath], page_size=50, max_returned_rows=100).app().test_client
ds = Datasette(
[filepath],
page_size=50,
max_returned_rows=100,
sql_time_limit_ms=20,
)
ds.sqlite_functions.append(
('sleep', 1, lambda n: time.sleep(float(n))),
)
yield ds.app().test_client
def test_homepage(app_client):
@ -83,6 +93,14 @@ def test_custom_sql(app_client):
assert not data['truncated']
def test_sql_time_limit(app_client):
_, response = app_client.get(
'/test_tables.jsono?sql=select+sleep(0.5)'
)
assert 400 == response.status
assert 'interrupted' == response.json['error']
def test_invalid_custom_sql(app_client):
_, response = app_client.get(
'/test_tables?sql=.schema'