Skip to content

Commit 8e14026

Browse files
committed
Fix birthplace encoding/decoding when the municipality has been created after the birthdate. #210 #213
1 parent 1520826 commit 8e14026

3 files changed

Lines changed: 100 additions & 6 deletions

File tree

src/codicefiscale/codicefiscale.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,9 @@ def _get_birthplace_fallback(
180180
) -> dict[str, dict[str, Any]] | None:
181181
# avoid wrong birthplace code error when birthdate falls in
182182
# missing date-range in the data-source even if birthplace code is valid
183-
if len(birthplaces_options) > 1:
184-
for index in range(len(birthplaces_options) - 1):
183+
birthplaces_options_count = len(birthplaces_options)
184+
if birthplaces_options_count > 1:
185+
for index in range(birthplaces_options_count - 1):
185186
birthplace_option = birthplaces_options[index]
186187
birthplace_option_next = birthplaces_options[(index + 1)]
187188
date_deleted = _get_date(birthplace_option["date_deleted"])
@@ -199,6 +200,16 @@ def _get_birthplace_fallback(
199200
return birthplace_option.copy()
200201
return birthplace_option_next.copy()
201202

203+
# Fix issues #210, #213
204+
# sometimes the code has been assigned after date of birth and
205+
# the municipality was not yet active at birthdate time,
206+
# let's return the first municipality created after birthdate_date
207+
for index in range(birthplaces_options_count):
208+
birthplace_option = birthplaces_options[index]
209+
date_created = _get_date(birthplace_option["date_created"])
210+
if date_created:
211+
if birthdate_date <= date_created:
212+
return birthplace_option.copy()
202213
return None
203214

204215

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import datetime
2+
3+
from codicefiscale import codicefiscale
4+
5+
6+
def test_issue_0210():
7+
"""
8+
Test for municipality activated after birthdate.
9+
https://github.com/fabiocaccamo/python-codicefiscale/issues/210
10+
"""
11+
code = "CCCFBA30H66F991T"
12+
assert codicefiscale.is_valid(code)
13+
code_data = codicefiscale.decode(code)
14+
code_data.pop("omocodes")
15+
expected_code_data = {
16+
"code": "CCCFBA30H66F991T",
17+
"gender": "F",
18+
"birthdate": datetime.datetime(1930, 6, 26, 0, 0),
19+
"birthplace": {
20+
"active": False,
21+
"code": "F991",
22+
"date_created": "1958-01-26T00:00:00",
23+
"date_deleted": "1965-01-07T00:00:00",
24+
"name": "Nuxis",
25+
"name_alt": "",
26+
"name_alt_trans": "",
27+
"name_slugs": ["nuxis"],
28+
"name_trans": "Nuxis",
29+
"province": "CA",
30+
},
31+
"raw": {
32+
"code": "CCCFBA30H66F991T",
33+
"lastname": "CCC",
34+
"firstname": "FBA",
35+
"birthdate": "30H66",
36+
"birthdate_year": "30",
37+
"birthdate_month": "H",
38+
"birthdate_day": "66",
39+
"birthplace": "F991",
40+
"cin": "T",
41+
},
42+
}
43+
assert code_data == expected_code_data
44+
45+
46+
def test_issue_0213():
47+
"""
48+
Test for municipality activated after birthdate.
49+
https://github.com/fabiocaccamo/python-codicefiscale/issues/213
50+
"""
51+
code = "DBRGRZ53R66F059C"
52+
assert codicefiscale.is_valid(code)
53+
code_data = codicefiscale.decode(code)
54+
code_data.pop("omocodes")
55+
expected_code_data = {
56+
"code": "DBRGRZ53R66F059C",
57+
"gender": "F",
58+
"birthdate": datetime.datetime(1953, 10, 26, 0, 0),
59+
"birthplace": {
60+
"active": True,
61+
"code": "F059",
62+
"date_created": "1955-08-19T00:00:00",
63+
"date_deleted": "",
64+
"name": "Mattinata",
65+
"name_alt": "",
66+
"name_alt_trans": "",
67+
"name_slugs": ["mattinata"],
68+
"name_trans": "Mattinata",
69+
"province": "FG",
70+
},
71+
"raw": {
72+
"code": "DBRGRZ53R66F059C",
73+
"lastname": "DBR",
74+
"firstname": "GRZ",
75+
"birthdate": "53R66",
76+
"birthdate_year": "53",
77+
"birthdate_month": "R",
78+
"birthdate_day": "66",
79+
"birthplace": "F059",
80+
"cin": "C",
81+
},
82+
}
83+
assert code_data == expected_code_data

tests/test_encode.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ def test_encode_birthplace_invalid_arguments():
246246
codicefiscale.encode_birthplace("Area 51")
247247

248248

249-
def test_encode_birthplace_invalid_birthdate():
250-
"""Test invalid birthdate for encoding birthplaces."""
251-
with pytest.raises(ValueError):
252-
codicefiscale.encode_birthplace("Torino", "01/01/1888")
249+
def test_encode_birthplace_created_after_birthdate():
250+
"""Test encoding birthplace created after birthdate."""
251+
result = codicefiscale.encode_birthplace("Torino", "01/01/1888")
252+
assert result == "L219"
253253

254254

255255
def test_encode_cin(cin_test_cases):

0 commit comments

Comments
 (0)