Skip to content

Commit

Permalink
Add test coverage for the Gramps' extension's configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
bartfeenstra committed May 7, 2024
1 parent e29a58d commit cf5f6b0
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 17 deletions.
15 changes: 8 additions & 7 deletions betty/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,12 +311,6 @@ def to_keys(self, *indices: int | slice) -> Iterator[ConfigurationKeyT]:
def _item_type(cls) -> type[ConfigurationT]:
raise NotImplementedError(repr(cls))

@classmethod
def _create_default_item(
cls, configuration_key: ConfigurationKeyT
) -> ConfigurationT:
raise NotImplementedError(repr(cls))

def keys(self) -> Iterator[ConfigurationKeyT]:
raise NotImplementedError(repr(self))

Expand Down Expand Up @@ -384,7 +378,8 @@ def values(self) -> Iterator[ConfigurationT]:
yield from self._configurations

def update(self, other: Self) -> None:
raise NotImplementedError(repr(self))
self._clear_without_dispatch()
self.append(*other)

@classmethod
def load(
Expand Down Expand Up @@ -633,6 +628,12 @@ def _load_key(
def _dump_key(self, item_dump: VoidableDump) -> tuple[VoidableDump, str]:
raise NotImplementedError(repr(self))

@classmethod
def _create_default_item(
cls, configuration_key: ConfigurationKeyT
) -> ConfigurationT:
raise NotImplementedError(repr(cls))


class Configurable(Generic[ConfigurationT]):
_configuration: ConfigurationT
Expand Down
14 changes: 4 additions & 10 deletions betty/extension/gramps/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,20 +55,14 @@ def load(
return configuration

def dump(self) -> VoidableDump:
return {
"file": str(self.file_path),
}
return {"file": str(self.file_path) if self.file_path else None}


class FamilyTreeConfigurationSequence(ConfigurationSequence[FamilyTreeConfiguration]):
def update(self, other: Self) -> None:
self._clear_without_dispatch()
self.append(*other)
self.file_path = other.file_path
self._dispatch_change()

@classmethod
def _create_default_item(cls, configuration_key: int) -> FamilyTreeConfiguration:
return FamilyTreeConfiguration()

class FamilyTreeConfigurationSequence(ConfigurationSequence[FamilyTreeConfiguration]):
@classmethod
def _item_type(cls) -> type[FamilyTreeConfiguration]:
return FamilyTreeConfiguration
Expand Down
106 changes: 106 additions & 0 deletions betty/tests/extension/gramps/test_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
from pathlib import Path
from typing import Any

from betty.extension.gramps.config import FamilyTreeConfiguration, GrampsConfiguration
from betty.serde.dump import Void, Dump
from betty.serde.load import AssertionFailed
from betty.tests.serde import raises_error


class TestFamilyTreeConfiguration:
async def test_load_with_minimal_configuration(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
dump: dict[str, Any] = {"file": str(file_path)}
FamilyTreeConfiguration().load(dump)

async def test_load_without_dict_should_error(self) -> None:
dump = None
with raises_error(error_type=AssertionFailed):
FamilyTreeConfiguration().load(dump)

async def test_dump_with_minimal_configuration(self) -> None:
sut = FamilyTreeConfiguration()
expected = {
"file": None,
}
assert expected == sut.dump()

async def test_dump_with_file_path(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
sut = FamilyTreeConfiguration()
sut.file_path = file_path
expected = {
"file": str(file_path),
}
assert expected == sut.dump()

async def test_update(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
sut = FamilyTreeConfiguration()
other = FamilyTreeConfiguration()
other.file_path = file_path
sut.update(other)
assert sut.file_path == file_path

async def test___eq___is_equal(self) -> None:
sut = FamilyTreeConfiguration()
other = FamilyTreeConfiguration()
assert sut == other

async def test___eq___is_not_equal_type(self) -> None:
sut = FamilyTreeConfiguration()
assert sut != 123

async def test___eq___is_not_equal(self, tmp_path: Path) -> None:
sut = FamilyTreeConfiguration()
sut.file_path = tmp_path / "ancestry.gramps"
other = FamilyTreeConfiguration()
assert sut != other


class TestGrampsConfiguration:
async def test_load_with_minimal_configuration(self) -> None:
dump: dict[str, Any] = {}
GrampsConfiguration().load(dump)

async def test_load_without_dict_should_error(self) -> None:
dump = None
with raises_error(error_type=AssertionFailed):
GrampsConfiguration().load(dump)

async def test_load_with_family_tree(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
dump: Dump = {
"family_trees": [
{
"file": str(file_path),
},
],
}
sut = GrampsConfiguration.load(dump)
assert sut.family_trees[0].file_path == file_path

async def test_dump_with_minimal_configuration(self) -> None:
sut = GrampsConfiguration()
assert sut.dump() is Void

async def test_dump_with_family_tree(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
sut = GrampsConfiguration()
sut.family_trees.append(FamilyTreeConfiguration(file_path=file_path))
expected = {
"family_trees": [
{
"file": str(file_path),
},
],
}
assert expected == sut.dump()

async def test_update(self, tmp_path: Path) -> None:
file_path = tmp_path / "ancestry.gramps"
sut = GrampsConfiguration()
other = GrampsConfiguration()
other.family_trees.append(FamilyTreeConfiguration(file_path=file_path))
sut.update(other)
assert sut.family_trees[0].file_path == file_path

0 comments on commit cf5f6b0

Please sign in to comment.