From eb39c84a8f27443abb7aaebc1724c99f68e441fb Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 4 Oct 2019 09:17:27 -0700 Subject: [PATCH] Test and docs for using :memory: as a filename --- docs/cli.rst | 5 +++++ tests/test_cli.py | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/docs/cli.rst b/docs/cli.rst index b67fbf9..10c3345 100644 --- a/docs/cli.rst +++ b/docs/cli.rst @@ -55,6 +55,11 @@ If you want to pretty-print the output further, you can pipe it through ``python } ] +You can run queries against a temporary in-memory database by passing ``:memory:`` as the filename:: + + $ sqlite-utils :memory: "select sqlite_version()" + [{"sqlite_version()": "3.29.0"}] + .. _cli_json_values: Nested JSON values diff --git a/tests/test_cli.py b/tests/test_cli.py index ca91bdb..d9ebe55 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -724,6 +724,20 @@ def test_query_json_with_json_cols(db_path): assert expected == result_rows.output.strip() +def test_query_memory_does_not_create_file(tmpdir): + owd = os.getcwd() + try: + os.chdir(tmpdir) + # This should create a foo.db file + CliRunner().invoke(cli.cli, ["foo.db", "select sqlite_version()"]) + # This should NOT create a file + result = CliRunner().invoke(cli.cli, [":memory:", "select sqlite_version()"]) + assert ["sqlite_version()"] == list(json.loads(result.output)[0].keys()) + finally: + os.chdir(owd) + assert ["foo.db"] == os.listdir(tmpdir) + + @pytest.mark.parametrize( "args,expected", [