Skip to content

Commit 6e5d6f7

Browse files
committed
Implement (single) series deserialization
1 parent d5b8475 commit 6e5d6f7

3 files changed

Lines changed: 79 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
- Renamed `writing` subpackage to `serialization`.
1010

11-
- Implemented serialization of series.
11+
- Implemented serialization and deserialization of (single) series.
1212

1313
- Added suffix `_from_toml` to public deserialization functions with string
1414
input argument.

src/lanpartydb/deserialization.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from .models import Location, Party, PartyLinks, Resource, Series
1717

1818

19-
# series
19+
# series list
2020

2121

2222
def deserialize_series_list_from_toml_file(filename: Path) -> list[Series]:
@@ -36,6 +36,26 @@ def _deserialize_series_list_from_dict(data: dict[str, Any]) -> list[Series]:
3636
return [Series(**item) for item in data.get('series', [])]
3737

3838

39+
# series
40+
41+
42+
def deserialize_series_from_toml_file(filename: Path) -> Series:
43+
"""Deserialize series from a TOML file."""
44+
toml = filename.read_text()
45+
return deserialize_series_from_toml(toml)
46+
47+
48+
def deserialize_series_from_toml(toml: str) -> Series:
49+
"""Deserialize series from a TOML document."""
50+
data = _load_toml(toml)
51+
return _deserialize_series_from_dict(data)
52+
53+
54+
def _deserialize_series_from_dict(data: dict[str, Any]) -> Series:
55+
"""Build series from a dictionary."""
56+
return Series(**data)
57+
58+
3959
# party
4060

4161

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
:Copyright: 2024-2025 Jochen Kupperschmidt
3+
:License: MIT
4+
"""
5+
6+
import pytest
7+
8+
from lanpartydb.deserialization import deserialize_series_from_toml
9+
from lanpartydb.models import Series
10+
11+
12+
@pytest.mark.parametrize(
13+
('toml', 'expected'),
14+
[
15+
(
16+
"""
17+
slug = "megalan"
18+
name = "MegaLAN"
19+
""",
20+
Series(
21+
slug='megalan',
22+
name='MegaLAN',
23+
alternative_names=[],
24+
country_codes=[],
25+
),
26+
),
27+
(
28+
"""
29+
slug = "gammalan"
30+
name = "GammaLAN"
31+
country_codes = ["ca", "us"]
32+
""",
33+
Series(
34+
slug='gammalan',
35+
name='GammaLAN',
36+
alternative_names=[],
37+
country_codes=['ca', 'us'],
38+
),
39+
),
40+
(
41+
"""
42+
slug = "deltalan"
43+
name = "DeltaLAN"
44+
alternative_names = ["Δ LAN", "Δέλτα LAN"]
45+
country_codes = ["au"]
46+
""",
47+
Series(
48+
slug='deltalan',
49+
name='DeltaLAN',
50+
alternative_names=['Δ LAN', 'Δέλτα LAN'],
51+
country_codes=['au'],
52+
),
53+
),
54+
],
55+
)
56+
def test_deserialize_series_from_toml(toml: str, expected: Series):
57+
assert deserialize_series_from_toml(toml) == expected

0 commit comments

Comments
 (0)