Skip to content

Commit

Permalink
Fixed collectYearManuscriptCode and its tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asaltveit committed Dec 2, 2024
1 parent ea2cf64 commit 3027103
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
31 changes: 17 additions & 14 deletions parse_info_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,26 @@


def collectYearManuscriptCode(file_name, output):
space_sections = file_name.split(" ")
numbers4digits = re.findall(r"[0-9]{4}", file_name)
numbersAllDigits = re.findall(r"[0-9]{3,9}", file_name)
posYear = int(numbers4digits[0])
if (
numbers4digits
and len(numbers4digits) >= 1
and posYear > 0
and posYear < 2050
and space_sections[1] == numbers4digits[0]
):
output["year"] = numbers4digits[0]
additionalNums = [x for x in numbersAllDigits if x != numbers4digits[0]]
if additionalNums:
output["number_of_volumes"] = additionalNums[0]
else:
secondItem = True

if numbers4digits:
# Do I need this check?
if " " in file_name:
space_sections = file_name.split(" ")
secondItem = space_sections[1] == numbers4digits[0]
intYear = int(numbers4digits[0])
if len(numbers4digits) >= 1 and intYear > 0 and intYear < 2050 and secondItem:
output["year"] = numbers4digits[0]
print("Update: Year found: " + numbers4digits[0])
additionalNums = [x for x in numbersAllDigits if x != numbers4digits[0]]
if additionalNums:
output["number_of_volumes"] = additionalNums[0]
print("Update: Manuscript code found")
elif numbersAllDigits:
output["number_of_volumes"] = numbersAllDigits[0]
print("Update: Manuscript code found")
return output


Expand Down
21 changes: 15 additions & 6 deletions tests/test_parse_info_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,31 @@

# collectYearManuscriptCode
@pytest.mark.parametrize(
"file_name,output,expected",
"file_name,output,expected,numPrints",
[
(
"Ammannati 2023 Lupus in fabula - Sulla vera mano di Lupo di Ferrières",
{},
{"year": "2023"},
1,
),
(
"Zurli 1998 Il cod Vindobonensis Palatinus 9401 asterisk dell Anthologia Latina",
{},
{"year": "1998", "number_of_volumes": 9401},
{"year": "1998", "number_of_volumes": "9401"},
2,
),
("Levitan-DancingEndRope-1985", {}, {"year": 1985}), # fails
("Les manuscrits de Loup de Ferrières", {}, {}), # fails
("Levitan-DancingEndRope-1985", {}, {"year": "1985"}, 1), # fails
("Les manuscrits de Loup de Ferrières", {}, {}, 0), # fails
],
)
def test_collectYearManuscriptCode(file_name, output, expected):
assert collectYearManuscriptCode(file_name, output) == expected
def test_collectYearManuscriptCode(file_name, output, expected, numPrints, capsys):
result = collectYearManuscriptCode(file_name, output)
captured = capsys.readouterr().out
assert result == expected
# Splitting on the 'U' adds an extra empty item to the list for the first occurence
# captured = ['Update: Year foundUpdate: Manuscript code found']
assert len(captured.split("U")[1:]) == numPrints


# getInfoFromFileName
Expand Down Expand Up @@ -59,6 +66,7 @@ def test_dashes():
)


# SHhuld the 2 below just be tested by the direct function?
def test_multiple_numbers_format():
assert getInfoFromFileName(
"Zurli 1998 Il cod Vindobonensis Palatinus 9401 asterisk dell Anthologia Latina"
Expand All @@ -67,6 +75,7 @@ def test_multiple_numbers_format():
"title": "Il cod Vindobonensis Palatinus",
"authors": ["Zurli"],
"year": "1998",
"number_of_volumes": "9401",
},
2,
)
Expand Down

0 comments on commit 3027103

Please sign in to comment.