mirror of
https://github.com/simonw/datasette.git
synced 2025-12-10 16:51:24 +01:00
Better tests for prepare_connection() plugin hook, refs #678
This commit is contained in:
parent
6303ea5048
commit
7c6a9c3529
2 changed files with 16 additions and 2 deletions
|
|
@ -329,11 +329,16 @@ ureg = pint.UnitRegistry()
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
def prepare_connection(conn):
|
def prepare_connection(conn, database, datasette):
|
||||||
def convert_units(amount, from_, to_):
|
def convert_units(amount, from_, to_):
|
||||||
"select convert_units(100, 'm', 'ft');"
|
"select convert_units(100, 'm', 'ft');"
|
||||||
return (amount * ureg(from_)).to(to_).to_tuple()[0]
|
return (amount * ureg(from_)).to(to_).to_tuple()[0]
|
||||||
conn.create_function('convert_units', 3, convert_units)
|
conn.create_function('convert_units', 3, convert_units)
|
||||||
|
def prepare_connection_args():
|
||||||
|
return 'database={}, datasette.plugin_config("name-of-plugin")={}'.format(
|
||||||
|
database, datasette.plugin_config("name-of-plugin")
|
||||||
|
)
|
||||||
|
conn.create_function('prepare_connection_args', 0, prepare_connection_args)
|
||||||
|
|
||||||
|
|
||||||
@hookimpl
|
@hookimpl
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,22 @@ import pytest
|
||||||
import urllib
|
import urllib
|
||||||
|
|
||||||
|
|
||||||
def test_plugins_dir_plugin(app_client):
|
def test_plugins_dir_plugin_prepare_connection(app_client):
|
||||||
response = app_client.get(
|
response = app_client.get(
|
||||||
"/fixtures.json?sql=select+convert_units(100%2C+'m'%2C+'ft')"
|
"/fixtures.json?sql=select+convert_units(100%2C+'m'%2C+'ft')"
|
||||||
)
|
)
|
||||||
assert pytest.approx(328.0839) == response.json["rows"][0][0]
|
assert pytest.approx(328.0839) == response.json["rows"][0][0]
|
||||||
|
|
||||||
|
|
||||||
|
def test_plugin_prepare_connection_arguments(app_client):
|
||||||
|
response = app_client.get(
|
||||||
|
"/fixtures.json?sql=select+prepare_connection_args()&_shape=arrayfirst"
|
||||||
|
)
|
||||||
|
assert [
|
||||||
|
"database=fixtures, datasette.plugin_config(\"name-of-plugin\")={'depth': 'root'}"
|
||||||
|
] == response.json
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize(
|
@pytest.mark.parametrize(
|
||||||
"path,expected_decoded_object",
|
"path,expected_decoded_object",
|
||||||
[
|
[
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue