mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Unit tests for --plugins-dir=plugins/
This commit is contained in:
parent
e6ac9e013c
commit
33c6bcadb9
4 changed files with 37 additions and 3 deletions
|
|
@ -1058,7 +1058,11 @@ class Datasette:
|
||||||
filepath = os.path.join(self.plugins_dir, filename)
|
filepath = os.path.join(self.plugins_dir, filename)
|
||||||
with open(filepath) as f:
|
with open(filepath) as f:
|
||||||
mod = module_from_path(filepath, name=filename)
|
mod = module_from_path(filepath, name=filename)
|
||||||
pm.register(mod)
|
try:
|
||||||
|
pm.register(mod)
|
||||||
|
except ValueError:
|
||||||
|
# Plugin already registered
|
||||||
|
pass
|
||||||
|
|
||||||
def app_css_hash(self):
|
def app_css_hash(self):
|
||||||
if not hasattr(self, '_app_css_hash'):
|
if not hasattr(self, '_app_css_hash'):
|
||||||
|
|
|
||||||
|
|
@ -15,12 +15,16 @@ def app_client():
|
||||||
conn = sqlite3.connect(filepath)
|
conn = sqlite3.connect(filepath)
|
||||||
conn.executescript(TABLES)
|
conn.executescript(TABLES)
|
||||||
os.chdir(os.path.dirname(filepath))
|
os.chdir(os.path.dirname(filepath))
|
||||||
|
plugins_dir = os.path.join(tmpdir, 'plugins')
|
||||||
|
os.mkdir(plugins_dir)
|
||||||
|
open(os.path.join(plugins_dir, 'my_plugin.py'), 'w').write(PLUGIN)
|
||||||
ds = Datasette(
|
ds = Datasette(
|
||||||
[filepath],
|
[filepath],
|
||||||
page_size=50,
|
page_size=50,
|
||||||
max_returned_rows=100,
|
max_returned_rows=100,
|
||||||
sql_time_limit_ms=20,
|
sql_time_limit_ms=20,
|
||||||
metadata=METADATA,
|
metadata=METADATA,
|
||||||
|
plugins_dir=plugins_dir,
|
||||||
)
|
)
|
||||||
ds.sqlite_functions.append(
|
ds.sqlite_functions.append(
|
||||||
('sleep', 1, lambda n: time.sleep(float(n))),
|
('sleep', 1, lambda n: time.sleep(float(n))),
|
||||||
|
|
@ -90,6 +94,20 @@ METADATA = {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PLUGIN = '''
|
||||||
|
from datasette import hookimpl
|
||||||
|
import pint
|
||||||
|
|
||||||
|
ureg = pint.UnitRegistry()
|
||||||
|
|
||||||
|
|
||||||
|
@hookimpl
|
||||||
|
def prepare_connection(conn):
|
||||||
|
def convert_units(amount, from_, to_):
|
||||||
|
"select convert_units(100, 'm', 'ft');"
|
||||||
|
return (amount * ureg(from_)).to(to_).to_tuple()[0]
|
||||||
|
conn.create_function('convert_units', 3, convert_units)
|
||||||
|
'''
|
||||||
|
|
||||||
TABLES = '''
|
TABLES = '''
|
||||||
CREATE TABLE simple_primary_key (
|
CREATE TABLE simple_primary_key (
|
||||||
|
|
|
||||||
|
|
@ -588,8 +588,10 @@ def test_row_foreign_key_tables(app_client):
|
||||||
|
|
||||||
|
|
||||||
def test_unit_filters(app_client):
|
def test_unit_filters(app_client):
|
||||||
response = app_client.get('/test_tables/units.json?distance__lt=75km&frequency__gt=1kHz',
|
response = app_client.get(
|
||||||
gather_request=False)
|
'/test_tables/units.json?distance__lt=75km&frequency__gt=1kHz',
|
||||||
|
gather_request=False
|
||||||
|
)
|
||||||
assert response.status == 200
|
assert response.status == 200
|
||||||
data = response.json
|
data = response.json
|
||||||
|
|
||||||
|
|
@ -598,3 +600,11 @@ def test_unit_filters(app_client):
|
||||||
|
|
||||||
assert len(data['rows']) == 1
|
assert len(data['rows']) == 1
|
||||||
assert data['rows'][0][0] == 2
|
assert data['rows'][0][0] == 2
|
||||||
|
|
||||||
|
|
||||||
|
def test_plugins_dir_plugin(app_client):
|
||||||
|
response = app_client.get(
|
||||||
|
"/test_tables.json?sql=select+convert_units(100%2C+'m'%2C+'ft')",
|
||||||
|
gather_request=False
|
||||||
|
)
|
||||||
|
assert pytest.approx(328.0839) == response.json['rows'][0][0]
|
||||||
|
|
|
||||||
|
|
@ -195,6 +195,7 @@ def test_temporary_docker_directory_uses_hard_link():
|
||||||
extra_options=None,
|
extra_options=None,
|
||||||
branch=None,
|
branch=None,
|
||||||
template_dir=None,
|
template_dir=None,
|
||||||
|
plugins_dir=None,
|
||||||
static=[],
|
static=[],
|
||||||
) as temp_docker:
|
) as temp_docker:
|
||||||
hello = os.path.join(temp_docker, 'hello')
|
hello = os.path.join(temp_docker, 'hello')
|
||||||
|
|
@ -218,6 +219,7 @@ def test_temporary_docker_directory_uses_copy_if_hard_link_fails(mock_link):
|
||||||
extra_options=None,
|
extra_options=None,
|
||||||
branch=None,
|
branch=None,
|
||||||
template_dir=None,
|
template_dir=None,
|
||||||
|
plugins_dir=None,
|
||||||
static=[],
|
static=[],
|
||||||
) as temp_docker:
|
) as temp_docker:
|
||||||
hello = os.path.join(temp_docker, 'hello')
|
hello = os.path.join(temp_docker, 'hello')
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue