The new command ImportCommand takes a Serializable type, from which it can call the deserialize method to generate a DB ORM type. The Serializable interface also declares the serialize method. (De)serialization moved to the ORM types, due to the inability to properly use overloading. Possible improvement for the future is to merge serialization information on JSONDecoder/Encoder classes. Adds a MockClient with the in-memory SQLite DB which can be used by tests. Most types export/import functionally tested using two DBs and comparing entries.
61 lines
1.6 KiB
Python
61 lines
1.6 KiB
Python
from pathlib import Path
|
|
from typing import Any, Sequence, Type
|
|
import pytest
|
|
|
|
from mocks import banks, categories, transactions
|
|
from mocks.client import MockClient
|
|
|
|
from pfbudget.common.types import ExportFormat
|
|
from pfbudget.core.command import ExportCommand, ImportCommand
|
|
from pfbudget.db.client import Client
|
|
from pfbudget.db.model import Bank, Category, CategoryGroup, Tag, Transaction
|
|
|
|
|
|
@pytest.fixture
|
|
def client() -> Client:
|
|
return MockClient()
|
|
|
|
|
|
params = [
|
|
(transactions.simple, Transaction),
|
|
(transactions.simple_transformed, Transaction),
|
|
(transactions.bank, Transaction),
|
|
(transactions.money, Transaction),
|
|
# (transactions.split, Transaction), NotImplemented
|
|
([banks.checking, banks.cc], Bank),
|
|
([categories.category_null, categories.category1, categories.category2], Category),
|
|
(
|
|
[
|
|
categories.categorygroup1,
|
|
categories.category_null,
|
|
categories.category1,
|
|
categories.category2,
|
|
],
|
|
CategoryGroup,
|
|
),
|
|
([categories.tag_1], Tag),
|
|
]
|
|
|
|
|
|
class TestBackup:
|
|
@pytest.mark.parametrize("input, what", params)
|
|
def test_import(self, tmp_path: Path, input: Sequence[Any], what: Type[Any]):
|
|
file = tmp_path / "test.json"
|
|
|
|
client = MockClient()
|
|
client.insert(input)
|
|
originals = client.select(what)
|
|
|
|
assert originals
|
|
|
|
command = ExportCommand(client, what, file, ExportFormat.JSON)
|
|
command.execute()
|
|
|
|
other = MockClient()
|
|
command = ImportCommand(other, what, file, ExportFormat.JSON)
|
|
command.execute()
|
|
|
|
imported = other.select(what)
|
|
|
|
assert originals == imported
|