generated from simonw/python-lib-template-repository
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Datasette() with plugin_config option
Tests against multiple Datasette versions, refs #1
- Loading branch information
Showing
4 changed files
with
52 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,31 @@ | ||
def example_function(): | ||
return 1 + 1 | ||
from datasette.app import Datasette as _Datasette | ||
|
||
try: | ||
from datasette.utils import fail_if_plugins_in_metadata | ||
|
||
plugin_config_should_be_in_metadata = False | ||
except ImportError: | ||
plugin_config_should_be_in_metadata = True | ||
|
||
|
||
class Datasette(_Datasette): | ||
def __init__(self, *args, **kwargs): | ||
plugin_config = kwargs.pop("plugin_config", None) | ||
if plugin_config is not None: | ||
if plugin_config_should_be_in_metadata: | ||
metadata = kwargs.pop("metadata", None) or {} | ||
metadata["plugins"] = plugin_config | ||
kwargs["metadata"] = metadata | ||
else: | ||
config = kwargs.pop("config", None) or {} | ||
config["plugins"] = plugin_config | ||
kwargs["config"] = config | ||
elif ( | ||
"plugins" in (kwargs.get("metadata") or {}) | ||
and not plugin_config_should_be_in_metadata | ||
): | ||
# Move plugins to config anyway | ||
plugin_config = kwargs["metadata"].pop("plugins") | ||
kwargs["config"] = kwargs.get("config") or {} | ||
kwargs["config"]["plugins"] = plugin_config | ||
super().__init__(*args, **kwargs) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,16 @@ | ||
from datasette_test import example_function | ||
from datasette_test import Datasette, plugin_config_should_be_in_metadata | ||
import pytest | ||
|
||
|
||
def test_example_function(): | ||
assert example_function() == 2 | ||
@pytest.mark.asyncio | ||
async def test_datasette_plugin_config(): | ||
ds = Datasette() | ||
response = await ds.client.get("/-/metadata.json") | ||
assert response.json() == {} | ||
|
||
ds = Datasette(plugin_config={"foo": "bar"}) | ||
response2 = await ds.client.get("/-/metadata.json") | ||
if plugin_config_should_be_in_metadata: | ||
assert response2.json() == {"plugins": {"foo": "bar"}} | ||
else: | ||
assert response2.json() == {} |