def test_hook_filters_from_request(app_client): class ReturnNothingPlugin: __name__ = "ReturnNothingPlugin" @hookimpl def filters_from_request(self, request): if request.args.get("_nothing"): return FilterArguments(["1 = 0"], human_descriptions=["NOTHING"]) pm.register(ReturnNothingPlugin(), name="ReturnNothingPlugin") response = app_client.get("/fixtures/facetable?_nothing=1") assert "0 rows\n where NOTHING" in response.text json_response = app_client.get("/fixtures/facetable.json?_nothing=1") assert json_response.json["rows"] == [] pm.unregister(name="ReturnNothingPlugin")
def test_hook_register_commands(): # Without the plugin should have seven commands runner = CliRunner() result = runner.invoke(cli.cli, "--help") commands = _extract_commands(result.output) assert commands == { "serve", "inspect", "install", "package", "plugins", "publish", "uninstall", } # Now install a plugin class VerifyPlugin: __name__ = "VerifyPlugin" @hookimpl def register_commands(self, cli): @cli.command() def verify(): pass @cli.command() def unverify(): pass pm.register(VerifyPlugin(), name="verify") importlib.reload(cli) result2 = runner.invoke(cli.cli, "--help") commands2 = _extract_commands(result2.output) assert commands2 == { "serve", "inspect", "install", "package", "plugins", "publish", "uninstall", "verify", "unverify", } pm.unregister(name="verify") importlib.reload(cli)
def test_serve_with_get(tmp_path_factory): plugins_dir = tmp_path_factory.mktemp("plugins_for_serve_with_get") (plugins_dir / "init_for_serve_with_get.py").write_text( textwrap.dedent( """ from datasette import hookimpl @hookimpl def startup(datasette): with open("{}", "w") as fp: fp.write("hello") """.format( str(plugins_dir / "hello.txt") ), ), "utf-8", ) runner = CliRunner() result = runner.invoke( cli, [ "serve", "--memory", "--plugins-dir", str(plugins_dir), "--get", "/_memory.json?sql=select+sqlite_version()", ], ) assert 0 == result.exit_code, result.output assert { "database": "_memory", "truncated": False, "columns": ["sqlite_version()"], }.items() <= json.loads(result.output).items() # The plugin should have created hello.txt assert (plugins_dir / "hello.txt").read_text() == "hello" # Annoyingly that new test plugin stays resident - we need # to manually unregister it to avoid conflict with other tests to_unregister = [ p for p in pm.get_plugins() if p.__name__ == "init_for_serve_with_get.py" ][0] pm.unregister(to_unregister)