[Export] Remove pickle format

Pickling directly from a DB object will also save DB related attributes,
namely the state of the object. When exporting values from a DB select,
they will be in detached state, which means on the import, the session
will not insert the object back into the DB, it already assumes they are
there.
This commit is contained in:
Luís Murta 2023-05-16 21:20:40 +01:00
parent 6574165f8f
commit 2cf0ba4374
Signed by: satprog
GPG Key ID: 169EF1BBD7049F94
3 changed files with 27 additions and 22 deletions

View File

@ -34,6 +34,7 @@ class ExportCommand(Command):
with open(self.fn, "w", newline="") as f:
json.dump([e.serialize() for e in values], f, indent=4)
case ExportFormat.pickle:
raise AttributeError("pickle export not working at the moment!")
with open(self.fn, "wb") as f:
pickle.dump(values, f)
@ -58,6 +59,7 @@ class ImportCommand(Command):
raise ImportFailedError(e)
case ExportFormat.pickle:
raise AttributeError("pickle import not working at the moment!")
with open(self.fn, "rb") as f:
values = pickle.load(f)

View File

@ -79,6 +79,15 @@ class TestBackup:
assert originals == imported
command = ExportCommand(client, what, file, ExportFormat.pickle)
with pytest.raises(AttributeError):
command.execute()
command = ImportCommand(other, what, file, ExportFormat.pickle)
with pytest.raises(AttributeError):
command.execute()
@pytest.mark.parametrize("input, what", not_serializable)
def test_try_backup_not_serializable(
self, tmp_path: Path, input: Sequence[Any], what: Type[Any]

View File

@ -1,7 +1,6 @@
from collections.abc import Sequence
import json
from pathlib import Path
import pickle
import pytest
from typing import Any, cast
@ -68,28 +67,23 @@ class TestCommand:
def test_export_pickle(self, tmp_path: Path, client: Client):
file = tmp_path / "test.pickle"
command = ExportCommand(client, Transaction, file, ExportFormat.pickle)
with pytest.raises(AttributeError):
command.execute()
with open(file, "rb") as f:
result = pickle.load(f)
assert result == mocks.transactions.simple
cast(FakeClient, client).transactions = mocks.transactions.simple_transformed
command.execute()
with open(file, "rb") as f:
result = pickle.load(f)
assert result == mocks.transactions.simple_transformed
def test_import(self, tmp_path: Path, client: Client):
def test_import_json(self, tmp_path: Path, client: Client):
file = tmp_path / "test"
for format in list(ExportFormat):
command = ExportCommand(client, Transaction, file, format)
command = ExportCommand(client, Transaction, file, ExportFormat.JSON)
command.execute()
command = ImportCommand(client, Transaction, file, format)
command = ImportCommand(client, Transaction, file, ExportFormat.JSON)
command.execute()
transactions = cast(FakeClient, client).transactions
assert len(transactions) > 0
assert transactions == client.select(Transaction)
def test_import_pickle(self, tmp_path: Path, client: Client):
file = tmp_path / "test"
command = ExportCommand(client, Transaction, file, ExportFormat.pickle)
with pytest.raises(AttributeError):
command.execute()