diff --git a/tests/sources/xml/test_ead.py b/tests/sources/xml/test_ead.py index 07f57b4..6709580 100644 --- a/tests/sources/xml/test_ead.py +++ b/tests/sources/xml/test_ead.py @@ -1,7 +1,7 @@ -# ruff: noqa: E501 - import logging +from typing import Literal + from bs4 import BeautifulSoup import transmogrifier.models as timdex @@ -9,7 +9,9 @@ def create_ead_source_record_stub( - header_insert: str = "", metadata_insert: str = "" + header_insert: str = "", + metadata_insert: str = "", + parent_element: Literal["archdesc", "did"] = None, ) -> BeautifulSoup: """ Create source record for unit tests. @@ -17,21 +19,16 @@ def create_ead_source_record_stub( Args: header_insert (str): For EAD-formatted XML, the
element is used in the derivation of 'source_record_id'. - metadata_insert (str): For EAD-formatted XML, data for field methods - are located within the Archival Description () element. - Values set for this parameter must be formatted as follows: - - - - {elements} - - {elements} - - - Note: {elements} refer to XML elements nested under the and - elements. + metadata_insert (Literal["archdesc", "did"]): For EAD-formatted XML, + all of the information about a collection that is relevant to the + TIMDEX data model is encapsulated within the element. + Metadata will be formatted either as a direct descendant of the element + or a descendant of the element. + + Note: A source record for "missing" field method tests can be created by + leaving metadata_insert = "" (the default) and setting parent_element. """ - xml_string = f""" + xml_string = """ @@ -43,8 +40,12 @@ def create_ead_source_record_stub( xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:isbn:1-931666-22-9 http://www.loc.gov/ead/ead.xsd"> - VC-0002 + findaidstatus="completed" + langencoding="iso639-2b" + repositoryencoding="iso15511"> + + VC-0002 + {metadata_insert} @@ -52,7 +53,25 @@ def create_ead_source_record_stub( """ - return BeautifulSoup(xml_string, "xml") + if metadata_insert and parent_element is None: + message = ( + "Argument 'parent_element' cannot be of NoneType " + "if 'metadata_insert' is set." + ) + raise TypeError(message) + if parent_element == "archdesc": + _metadata_insert = f""" + {metadata_insert} + """ + elif parent_element == "did": + _metadata_insert = f""" + {metadata_insert} + """ + + return BeautifulSoup( + xml_string.format(header_insert=header_insert, metadata_insert=_metadata_insert), + "xml", + ) def test_ead_transform_with_all_fields_transforms_correctly(): @@ -580,25 +599,22 @@ def test_get_alternate_titles_success(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - Charles J. Connick Stained Glass - - Foundation - Collection - - VC.0002 - - - Title 2 - VC.0002 - - Title 3 - - + + Charles J. Connick Stained Glass + + Foundation + Collection + + VC.0002 + + + Title 2 + VC.0002 + + Title 3 """ - ) + ), + parent_element="did", ) assert Ead.get_alternate_titles(source_record) == [ timdex.AlternateTitle(value="Title 2"), @@ -610,22 +626,19 @@ def test_get_alternate_titles_transforms_correctly_if_fields_blank(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - Charles J. Connick Stained Glass - - Foundation - Collection - - VC.0002 - - - - - + + Charles J. Connick Stained Glass + + Foundation + Collection + + VC.0002 + + + """ - ) + ), + parent_element="did", ) assert Ead.get_alternate_titles(source_record) is None @@ -634,20 +647,17 @@ def test_get_alternate_titles_transforms_correctly_if_fields_missing(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - Charles J. Connick Stained Glass - - Foundation - Collection - - VC.0002 - - - + + Charles J. Connick Stained Glass + + Foundation + Collection + + VC.0002 + """ - ) + ), + parent_element="did", ) assert Ead.get_alternate_titles(source_record) is None @@ -655,18 +665,18 @@ def test_get_alternate_titles_transforms_correctly_if_fields_missing(): def test_get_citation_success(): source_record = create_ead_source_record_stub( metadata_insert=( - """ - - - Preferred Citation -

Charles J. Connick Stained Glass Foundation Collection, VC-0002, box X. Massachusetts Institute of Technology, Department of Distinctive Collections, Cambridge, Massachusetts.

-
-
- """ - ) + "Preferred Citation

" + "Charles J. Connick Stained Glass Foundation Collection, " + "VC-0002, box X. Massachusetts Institute of Technology, " + "Department of Distinctive Collections, Cambridge, Massachusetts." + "

" + ), + parent_element="archdesc", ) assert Ead.get_citation(source_record) == ( - "Charles J. Connick Stained Glass Foundation Collection, VC-0002, box X. Massachusetts Institute of Technology, Department of Distinctive Collections, Cambridge, Massachusetts." + "Charles J. Connick Stained Glass Foundation Collection, " + "VC-0002, box X. Massachusetts Institute of Technology, " + "Department of Distinctive Collections, Cambridge, Massachusetts." ) @@ -674,125 +684,101 @@ def test_get_citation_transforms_correctly_if_fields_blank(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - + prefercite> """ - ) + ), + parent_element="archdesc", ) assert Ead.get_citation(source_record) is None def test_get_citation_transforms_correctly_if_fields_missing(): - source_record = create_ead_source_record_stub( - metadata_insert=( - """ - - """ - ) - ) + source_record = create_ead_source_record_stub(parent_element="archdesc") assert Ead.get_citation(source_record) is None -def test_get_main_titles_success(): +def test_get_content_type_success(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - Charles J. Connick Stained Glass - - Foundation - Collection - - VC.0002 - - - Title 2 - VC.0002 - - Title 3 - - + + + Correspondence + + """ - ) + ), + parent_element="archdesc", ) - assert Ead.get_main_titles(source_record) == [ - "Charles J. Connick Stained Glass Foundation Collection", - "Title 2", - "Title 3", - ] + assert Ead.get_content_type(source_record) == ["Archival materials", "Correspondence"] -def test_get_main_titles_transforms_correctly_if_fields_blank(): +def test_get_content_type_transforms_correctly_if_fields_blank(): source_record = create_ead_source_record_stub( metadata_insert=( + """ + + + """ - - - - - - """ - ) + ), + parent_element="archdesc", ) - assert Ead.get_main_titles(source_record) == [] + assert Ead.get_content_type(source_record) == ["Archival materials"] -def test_get_main_titles_transforms_correctly_if_fields_missing(): +def test_get_content_type_transforms_correctly_if_fields_missing(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - + """ - ) + ), + parent_element="archdesc", ) - assert Ead.get_main_titles(source_record) == [] + assert Ead.get_content_type(source_record) == ["Archival materials"] -def test_get_content_type_success(): +def test_get_main_titles_success(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - Correspondence - - - + + Charles J. Connick Stained Glass + + Foundation + Collection + + VC.0002 + + + Title 2 + VC.0002 + + Title 3 """ - ) + ), + parent_element="did", ) - assert Ead.get_content_type(source_record) == ["Archival materials", "Correspondence"] + assert Ead.get_main_titles(source_record) == [ + "Charles J. Connick Stained Glass Foundation Collection", + "Title 2", + "Title 3", + ] -def test_get_content_type_transforms_correctly_if_fields_blank(): +def test_get_main_titles_transforms_correctly_if_fields_blank(): source_record = create_ead_source_record_stub( metadata_insert=( """ - - - - - + """ - ) + ), + parent_element="did", ) - assert Ead.get_content_type(source_record) == ["Archival materials"] + assert Ead.get_main_titles(source_record) == [] -def test_get_content_type_transforms_correctly_if_fields_missing(): - source_record = create_ead_source_record_stub( - metadata_insert=( - """ - - - - """ - ) - ) - assert Ead.get_content_type(source_record) == ["Archival materials"] +def test_get_main_titles_transforms_correctly_if_fields_missing(): + source_record = create_ead_source_record_stub(parent_element="did") + assert Ead.get_main_titles(source_record) == []