From 0264ad6e0f3276071b54e7c8ce10f05786b587bc Mon Sep 17 00:00:00 2001 From: PeskyPotato Date: Sat, 4 May 2024 10:21:10 +0200 Subject: [PATCH 1/3] First version of load and dump --- sqlite_diffable/__init__.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/sqlite_diffable/__init__.py b/sqlite_diffable/__init__.py index e69de29..9f4e746 100644 --- a/sqlite_diffable/__init__.py +++ b/sqlite_diffable/__init__.py @@ -0,0 +1,24 @@ +import sqlite_diffable.cli +import click + +def load(dbpath, directory, replace=False): + params = [dbpath, directory] + if replace: + params.append('--replace') + try: + sqlite_diffable.cli.load(params, standalone_mode=False) + except click.exceptions.ClickException as e: + print("Use the replace parameter to over-write existing tables") + # raise(e) + +def dump(dbpath, output, tables=[], all=False): + params = [dbpath, output] + if tables: + params += tables + if all: + params.append('--all') + try: + sqlite_diffable.cli.dump(params, standalone_mode=False) + except click.exceptions.ClickException as e: + print("You must set all to True or specify a list of tables") + # raise(e) \ No newline at end of file From 8014dbea938ea0c1f540ec9599d58d09d186d849 Mon Sep 17 00:00:00 2001 From: PeskyPotato Date: Sat, 4 May 2024 15:58:41 +0200 Subject: [PATCH 2/3] First version of objects --- sqlite_diffable/__init__.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sqlite_diffable/__init__.py b/sqlite_diffable/__init__.py index 9f4e746..8bc54d3 100644 --- a/sqlite_diffable/__init__.py +++ b/sqlite_diffable/__init__.py @@ -21,4 +21,12 @@ def dump(dbpath, output, tables=[], all=False): sqlite_diffable.cli.dump(params, standalone_mode=False) except click.exceptions.ClickException as e: print("You must set all to True or specify a list of tables") - # raise(e) \ No newline at end of file + # raise(e) + +def objects(filepath, output='', array=False): + params = [filepath] + if output: + params += ['--output', output] + if array: + params.append('--array') + sqlite_diffable.cli.objects(params, standalone_mode=False) \ No newline at end of file From 8865ebf9abac110144e5c2c02fdbca5cc339c7a7 Mon Sep 17 00:00:00 2001 From: PeskyPotato Date: Sat, 4 May 2024 17:23:08 +0200 Subject: [PATCH 3/3] Basic tests on dump and load functions --- sqlite_diffable/__init__.py | 8 +++-- tests/test_dump_module.py | 61 +++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 3 deletions(-) create mode 100644 tests/test_dump_module.py diff --git a/sqlite_diffable/__init__.py b/sqlite_diffable/__init__.py index 8bc54d3..dbba096 100644 --- a/sqlite_diffable/__init__.py +++ b/sqlite_diffable/__init__.py @@ -9,7 +9,8 @@ def load(dbpath, directory, replace=False): sqlite_diffable.cli.load(params, standalone_mode=False) except click.exceptions.ClickException as e: print("Use the replace parameter to over-write existing tables") - # raise(e) + return False + return True def dump(dbpath, output, tables=[], all=False): params = [dbpath, output] @@ -20,8 +21,9 @@ def dump(dbpath, output, tables=[], all=False): try: sqlite_diffable.cli.dump(params, standalone_mode=False) except click.exceptions.ClickException as e: - print("You must set all to True or specify a list of tables") - # raise(e) + print("You must set all to True or specify a list of tables") + return False + return True def objects(filepath, output='', array=False): params = [filepath] diff --git a/tests/test_dump_module.py b/tests/test_dump_module.py new file mode 100644 index 0000000..8b2dca8 --- /dev/null +++ b/tests/test_dump_module.py @@ -0,0 +1,61 @@ +import sqlite_diffable +import json +import sqlite_utils + +def test_dump_module(one_table_db, tmpdir): + output_dir = tmpdir / "out" + sqlite_diffable.dump(one_table_db, str(output_dir), tables=["one_table"]) + + ndjson = output_dir / "one_table.ndjson" + metadata = output_dir / "one_table.metadata.json" + assert ndjson.exists() + assert metadata.exists() + assert [[1, "Stacey"], [2, "Tilda"], [3, "Bartek"]] == [ + json.loads(line) for line in ndjson.open() + ] + assert { + "name": "one_table", + "columns": ["id", "name"], + "schema": "CREATE TABLE [one_table] (\n [id] INTEGER PRIMARY KEY,\n [name] TEXT\n)", + } == json.load(metadata) + +def test_dump_all_module(two_tables_db, tmpdir): + output_dir = tmpdir / "out" + sqlite_diffable.dump(two_tables_db, str(output_dir), all=True) + assert (output_dir / "one_table.ndjson").exists() + assert (output_dir / "one_table.metadata.json").exists() + assert (output_dir / "second_table.ndjson").exists() + assert (output_dir / "second_table.metadata.json").exists() + +def test_load(two_tables_db, tmpdir): + output_dir = tmpdir / "out" + restore_db = str(tmpdir / "restore.db") + sqlite_diffable.dump(two_tables_db, str(output_dir), all=True) + + sqlite_diffable.load(restore_db, str(output_dir)) + + db = sqlite_utils.Database(restore_db) + assert set(db.table_names()) == {"second_table", "one_table"} + assert list(db["one_table"].rows) == [ + {"id": 1, "name": "Stacey"}, + {"id": 2, "name": "Tilda"}, + {"id": 3, "name": "Bartek"}, + ] + assert list(db["second_table"].rows) == [ + {"id": 1, "name": "Cleo"}, + ] + + # Running load a second time should error + result = sqlite_diffable.load(restore_db, str(output_dir)) + assert not result + + (output_dir / "one_table.ndjson").write_text( + '[1, "Stacey"]\n[2, "Tilda"]\n', "utf-8" + ) + result = sqlite_diffable.load(restore_db, str(output_dir), replace=True) + assert result + assert list(db["one_table"].rows) == [ + {"id": 1, "name": "Stacey"}, + {"id": 2, "name": "Tilda"}, + ] +