Skip to content

Commit

Permalink
Remove parse_xml_records function
Browse files Browse the repository at this point in the history
* Remove parse_xml_records function from helpers module and corresponding unit test
* Replace all calls of that function with the appropriate class method
  • Loading branch information
ehanson8 committed Nov 21, 2023
1 parent b2d915d commit 3dbb15f
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 99 deletions.
10 changes: 6 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

import transmogrifier.models as timdex
from transmogrifier.config import SOURCES, load_external_config
from transmogrifier.helpers import parse_xml_records
from transmogrifier.sources.datacite import Datacite
from transmogrifier.sources.transformer import XmlTransformer


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -48,12 +48,14 @@ def runner():

@pytest.fixture()
def datacite_records():
return parse_xml_records("tests/fixtures/datacite/datacite_records.xml")
return XmlTransformer.parse_source_file(
"tests/fixtures/datacite/datacite_records.xml"
)


@pytest.fixture()
def datacite_record_all_fields():
source_records = parse_xml_records(
source_records = XmlTransformer.parse_source_file(
"tests/fixtures/datacite/datacite_record_all_fields.xml"
)
return Datacite("cool-repo", source_records)
Expand All @@ -71,7 +73,7 @@ def marc_content_type_crosswalk():

@pytest.fixture()
def oai_pmh_records():
return parse_xml_records("tests/fixtures/oai_pmh_records.xml")
return XmlTransformer.parse_source_file("tests/fixtures/oai_pmh_records.xml")


@pytest.fixture()
Expand Down
9 changes: 4 additions & 5 deletions tests/test_datacite.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from transmogrifier.helpers import parse_xml_records
from transmogrifier.models import (
AlternateTitle,
Contributor,
Expand Down Expand Up @@ -170,7 +169,7 @@ def test_datacite_transform_with_all_fields_transforms_correctly(


def test_datacite_transform_missing_required_datacite_fields_logs_warning(caplog):
source_records = parse_xml_records(
source_records = Datacite.parse_source_file(
"tests/fixtures/datacite/datacite_record_missing_datacite_required_fields.xml"
)
output_records = Datacite("cool-repo", source_records)
Expand All @@ -195,7 +194,7 @@ def test_datacite_transform_missing_required_datacite_fields_logs_warning(caplog


def test_datacite_transform_with_optional_fields_blank_transforms_correctly():
source_records = parse_xml_records(
source_records = Datacite.parse_source_file(
"tests/fixtures/datacite/datacite_record_optional_fields_blank.xml"
)
output_records = Datacite("cool-repo", source_records)
Expand All @@ -218,7 +217,7 @@ def test_datacite_transform_with_optional_fields_blank_transforms_correctly():


def test_datacite_transform_with_optional_fields_missing_transforms_correctly():
source_records = parse_xml_records(
source_records = Datacite.parse_source_file(
"tests/fixtures/datacite/datacite_record_optional_fields_missing.xml"
)
output_records = Datacite("cool-repo", source_records)
Expand All @@ -241,7 +240,7 @@ def test_datacite_transform_with_optional_fields_missing_transforms_correctly():


def test_datacite_with_attribute_and_subfield_variations_transforms_correctly():
source_records = parse_xml_records(
source_records = Datacite.parse_source_file(
"tests/fixtures/datacite/datacite_record_attribute_and_subfield_variations.xml"
)
output_records = Datacite("cool-repo", source_records)
Expand Down
9 changes: 4 additions & 5 deletions tests/test_dspace_dim.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import transmogrifier.models as timdex
from transmogrifier.helpers import parse_xml_records
from transmogrifier.sources.dspace_dim import DspaceDim


def test_dspace_dim_transform_with_all_fields_transforms_correctly():
source_records = parse_xml_records(
source_records = DspaceDim.parse_source_file(
"tests/fixtures/dspace/dspace_dim_record_all_fields.xml"
)
output_records = DspaceDim("cool-repo", source_records)
Expand Down Expand Up @@ -133,7 +132,7 @@ def test_dspace_dim_transform_with_all_fields_transforms_correctly():


def test_dspace_dim_transform_with_attribute_variations_transforms_correctly():
source_records = parse_xml_records(
source_records = DspaceDim.parse_source_file(
"tests/fixtures/dspace/dspace_dim_record_attribute_variations.xml"
)
output_records = DspaceDim("cool-repo", source_records)
Expand Down Expand Up @@ -188,7 +187,7 @@ def test_dspace_dim_transform_with_attribute_variations_transforms_correctly():


def test_dspace_dim_transform_with_optional_fields_blank_transforms_correctly():
source_records = parse_xml_records(
source_records = DspaceDim.parse_source_file(
"tests/fixtures/dspace/dspace_dim_record_optional_fields_blank.xml"
)
output_records = DspaceDim("cool-repo", source_records)
Expand All @@ -204,7 +203,7 @@ def test_dspace_dim_transform_with_optional_fields_blank_transforms_correctly():


def test_dspace_dim_transform_with_optional_fields_missing_transforms_correctly():
source_records = parse_xml_records(
source_records = DspaceDim.parse_source_file(
"tests/fixtures/dspace/dspace_dim_record_optional_fields_missing.xml"
)
output_records = DspaceDim("cool-repo", source_records)
Expand Down
9 changes: 4 additions & 5 deletions tests/test_dspace_mets.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import transmogrifier.models as timdex
from transmogrifier.helpers import parse_xml_records
from transmogrifier.sources.dspace_mets import DspaceMets


def test_dspace_mets_transform_with_missing_optional_fields_transforms_correctly():
dspace_xml_records = parse_xml_records(
dspace_xml_records = DspaceMets.parse_source_file(
"tests/fixtures/dspace/dspace_mets_record_optional_fields_missing.xml"
)
output_records = DspaceMets("dspace", dspace_xml_records)
Expand All @@ -20,7 +19,7 @@ def test_dspace_mets_transform_with_missing_optional_fields_transforms_correctly


def test_dspace_mets_transform_with_blank_optional_fields_transforms_correctly():
dspace_xml_records = parse_xml_records(
dspace_xml_records = DspaceMets.parse_source_file(
"tests/fixtures/dspace/dspace_mets_record_optional_fields_blank.xml"
)
output_records = DspaceMets("dspace", dspace_xml_records)
Expand All @@ -36,7 +35,7 @@ def test_dspace_mets_transform_with_blank_optional_fields_transforms_correctly()


def test_dspace_mets_with_attribute_and_subfield_variations_transforms_correctly():
dspace_xml_records = parse_xml_records(
dspace_xml_records = DspaceMets.parse_source_file(
"tests/fixtures/dspace/dspace_mets_record_attribute_and_subfield_variations.xml"
)
output_records = DspaceMets("dspace", dspace_xml_records)
Expand Down Expand Up @@ -85,7 +84,7 @@ def test_dspace_mets_with_attribute_and_subfield_variations_transforms_correctly


def test_dspace_mets_transform_with_all_fields_transforms_correctly():
dspace_xml_records = parse_xml_records(
dspace_xml_records = DspaceMets.parse_source_file(
"tests/fixtures/dspace/dspace_mets_record_all_fields.xml"
)
output_records = DspaceMets("dspace", dspace_xml_records)
Expand Down
19 changes: 10 additions & 9 deletions tests/test_ead.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import logging

import transmogrifier.models as timdex
from transmogrifier.helpers import parse_xml_records
from transmogrifier.sources.ead import Ead


def test_ead_record_all_fields_transform_correctly():
ead_xml_records = parse_xml_records("tests/fixtures/ead/ead_record_all_fields.xml")
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_all_fields.xml"
)
output_records = Ead("aspace", ead_xml_records)
assert next(output_records) == timdex.TimdexRecord(
source="MIT ArchivesSpace",
Expand Down Expand Up @@ -216,7 +217,7 @@ def test_ead_record_all_fields_transform_correctly():


def test_ead_record_with_missing_archdesc_logs_error(caplog):
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_missing_archdesc.xml"
)
output_records = Ead("aspace", ead_xml_records)
Expand All @@ -230,7 +231,7 @@ def test_ead_record_with_missing_archdesc_logs_error(caplog):


def test_ead_record_with_missing_archdesc_did_logs_error(caplog):
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_missing_archdesc_did.xml"
)
output_records = Ead("aspace", ead_xml_records)
Expand All @@ -244,7 +245,7 @@ def test_ead_record_with_missing_archdesc_did_logs_error(caplog):


def test_ead_record_with_attribute_and_subfield_variations_transforms_correctly():
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_attribute_and_subfield_variations.xml"
)
output_records = Ead("aspace", ead_xml_records)
Expand Down Expand Up @@ -470,7 +471,7 @@ def test_ead_record_with_attribute_and_subfield_variations_transforms_correctly(


def test_ead_record_with_blank_optional_fields_transforms_correctly():
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_blank_optional_fields.xml"
)
output_records = Ead("aspace", ead_xml_records)
Expand All @@ -488,7 +489,7 @@ def test_ead_record_with_blank_optional_fields_transforms_correctly():


def test_ead_record_invalid_date_and_date_range_are_omitted(caplog):
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_attribute_and_subfield_variations.xml"
)
output_record = next(Ead("aspace", ead_xml_records))
Expand All @@ -507,7 +508,7 @@ def test_ead_record_invalid_date_and_date_range_are_omitted(caplog):


def test_ead_record_correct_identifiers_from_multiple_unitid(caplog):
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_attribute_and_subfield_variations.xml"
)
output_record = next(Ead("aspace", ead_xml_records))
Expand All @@ -516,7 +517,7 @@ def test_ead_record_correct_identifiers_from_multiple_unitid(caplog):


def test_ead_record_with_missing_optional_fields_transforms_correctly():
ead_xml_records = parse_xml_records(
ead_xml_records = Ead.parse_source_file(
"tests/fixtures/ead/ead_record_missing_optional_fields.xml"
)
output_records = Ead("aspace", ead_xml_records)
Expand Down
6 changes: 0 additions & 6 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from transmogrifier.helpers import (
generate_citation,
parse_date_from_string,
parse_xml_records,
validate_date,
validate_date_range,
)
Expand Down Expand Up @@ -189,11 +188,6 @@ def test_generate_citation_with_all_fields():
)


def test_parse_xml_records_returns_record_iterator():
records = parse_xml_records("tests/fixtures/datacite/datacite_records.xml")
assert len(list(records)) == 38


def test_parse_date_from_string_success():
for date in [
"1930",
Expand Down
37 changes: 20 additions & 17 deletions tests/test_marc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
from bs4 import BeautifulSoup

import transmogrifier.models as timdex
from transmogrifier.helpers import parse_xml_records
from transmogrifier.sources.marc import Marc


def test_marc_record_all_fields_transform_correctly():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
output_records = Marc("alma", marc_xml_records)
Expand Down Expand Up @@ -489,7 +488,7 @@ def test_marc_record_all_fields_transform_correctly():


def test_marc_record_attribute_and_subfield_variations_transforms_correctly():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_attribute_and_subfield_variations.xml",
)
output_records = Marc("alma", marc_xml_records)
Expand Down Expand Up @@ -706,7 +705,7 @@ def test_marc_record_attribute_and_subfield_variations_transforms_correctly():


def test_marc_record_with_blank_optional_fields_transforms_correctly():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_blank_optional_fields.xml"
)
output_records = Marc("alma", marc_xml_records)
Expand All @@ -727,7 +726,7 @@ def test_marc_record_with_blank_optional_fields_transforms_correctly():


def test_marc_record_with_missing_optional_fields_transforms_correctly():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_missing_optional_fields.xml"
)
output_records = Marc("alma", marc_xml_records)
Expand All @@ -746,7 +745,7 @@ def test_marc_record_with_missing_optional_fields_transforms_correctly():


def test_marc_record_missing_leader_logs_error(caplog):
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_missing_leader.xml"
)
output_records = Marc("alma", marc_xml_records)
Expand All @@ -760,7 +759,7 @@ def test_marc_record_missing_leader_logs_error(caplog):


def test_marc_record_missing_008_logs_error(caplog):
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_missing_008.xml"
)
output_records = Marc("alma", marc_xml_records)
Expand All @@ -774,7 +773,7 @@ def test_marc_record_missing_008_logs_error(caplog):


def test_create_subfield_value_list_from_datafield_with_values():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
datafield = next(marc_xml_records).find_all("datafield", tag="130")[0]
Expand All @@ -786,15 +785,15 @@ def test_create_subfield_value_list_from_datafield_with_values():


def test_create_subfield_value_list_from_datafield_with_blank_values():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_blank_optional_fields.xml"
)
datafield = next(marc_xml_records).find_all("datafield", tag="130")[0]
assert Marc.create_subfield_value_list_from_datafield(datafield, "ad") == []


def test_create_subfield_value_string_from_datafield_with_values():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
datafield = next(marc_xml_records).find_all("datafield", tag="130")[0]
Expand All @@ -805,7 +804,7 @@ def test_create_subfield_value_string_from_datafield_with_values():


def test_create_subfield_value_string_from_datafield_with_blank_values():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_blank_optional_fields.xml"
)
datafield = next(marc_xml_records).find_all("datafield", tag="130")[0]
Expand Down Expand Up @@ -894,7 +893,7 @@ def test_loc_crosswalk_code_to_name_returns_name(caplog, loc_country_crosswalk):


def test_get_main_titles_record_with_title():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
assert Marc.get_main_titles(next(marc_xml_records)) == [
Expand All @@ -903,31 +902,35 @@ def test_get_main_titles_record_with_title():


def test_get_main_titles_record_with_blank_title():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_blank_optional_fields.xml"
)
assert Marc.get_main_titles(next(marc_xml_records)) == []


def test_get_main_titles_record_without_title():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_missing_optional_fields.xml"
)
assert Marc.get_main_titles(next(marc_xml_records)) == []


def test_get_source_record_id():
marc_xml_records = parse_xml_records(
marc_xml_records = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
assert Marc.get_source_record_id(next(marc_xml_records)) == "990027185640106761"


def test_record_is_deleted_returns_true_if_deleted():
deleted_record = parse_xml_records("tests/fixtures/marc/marc_record_deleted.xml")
deleted_record = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_deleted.xml"
)
assert Marc.record_is_deleted(next(deleted_record)) is True


def test_record_is_deleted_returns_false_if_not_deleted():
marc_record = parse_xml_records("tests/fixtures/marc/marc_record_all_fields.xml")
marc_record = Marc.parse_source_file(
"tests/fixtures/marc/marc_record_all_fields.xml"
)
assert Marc.record_is_deleted(next(marc_record)) is False
Loading

0 comments on commit 3dbb15f

Please sign in to comment.