From 3f9ca546f5a6734095054035ac01d5f436092fe4 Mon Sep 17 00:00:00 2001 From: jonavellecuerdo Date: Wed, 24 Jul 2024 09:30:41 -0400 Subject: [PATCH] get_dates --- tests/sources/xml/test_marc.py | 16 ++++++++++++++++ transmogrifier/sources/xml/marc.py | 13 ++++++++----- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/tests/sources/xml/test_marc.py b/tests/sources/xml/test_marc.py index 53ecf6e..ffff54a 100644 --- a/tests/sources/xml/test_marc.py +++ b/tests/sources/xml/test_marc.py @@ -1116,6 +1116,22 @@ def test_get_contributors_transforms_correctly_if_kind_not_specified(): ] +def test_get_dates_success(): + source_record = create_marc_source_record_stub() + assert Marc.get_dates(source_record) == [ + timdex.Date(kind="Publication date", value="2016") + ] + + +def test_get_dates_transforms_correctly_if_char_positions_blank(): + source_record = create_marc_source_record_stub( + control_field_insert=( + '170906s fr mun| o e zxx d' + ) + ) + assert Marc.get_dates(source_record) is None + + def test_marc_record_missing_leader_skips_record(caplog): marc_xml_records = Marc.parse_source_file( "tests/fixtures/marc/marc_record_missing_leader.xml" diff --git a/transmogrifier/sources/xml/marc.py b/transmogrifier/sources/xml/marc.py index 7c5eef8..ce7dd58 100644 --- a/transmogrifier/sources/xml/marc.py +++ b/transmogrifier/sources/xml/marc.py @@ -61,11 +61,7 @@ def get_optional_fields(self, source_record: Tag) -> dict | None: fields["contributors"] = self.get_contributors(source_record) # dates - publication_year = self._get_control_field(source_record)[7:11].strip() - if validate_date(publication_year, source_record_id): - fields["dates"] = [ - timdex.Date(kind="Publication date", value=publication_year) - ] + fields["dates"] = self.get_dates(source_record) # edition edition_values = [] @@ -857,6 +853,13 @@ def get_contributors(cls, source_record: Tag) -> list[timdex.Contributor] | None ) return contributors or None + @classmethod + def get_dates(cls, source_record: Tag) -> list[timdex.Date] | None: + publication_year = cls._get_control_field(source_record)[7:11].strip() + if validate_date(publication_year, cls.get_source_record_id(source_record)): + return [timdex.Date(kind="Publication date", value=publication_year)] + return None + @staticmethod def get_main_titles(xml: Tag) -> list[str]: """