def test_035_enhancement_task_adds_035(monkeypatch, identifier_type, subdirectory, identifier, volume): with patch('builtins.open', mock_open(read_data=SAMPLE_RECORD)) as mock_955: workflow_get_marc.MarcEnhancement955Task(added_value=subdirectory, xml_file="dummy").work() xml_data_with_955 = mock_955().write.call_args[0][0] with patch('builtins.open', mock_open(read_data=xml_data_with_955)) as mock_035: task = workflow_get_marc.MarcEnhancement035Task(xml_file="dummy") assert task.work() is True with_035_data = mock_035().write.call_args[0][0] tree = ET.fromstring(with_035_data) ns = {"marc": "http://www.loc.gov/MARC21/slim"} def sub_only(x) -> bool: return any("(UIU)Voyager" in t.text for t in x) fields = list( filter(sub_only, tree.findall(".//marc:datafield/[@tag='035']", ns))) assert len(fields) == 1, "No 035 datafields found" assert fields[0][0].text == '(UIU)Voyager170427'
def test_failing_to_parse_provides_input(monkeypatch): def mock_parse(filename): raise ET.ParseError( "not well-formed (invalid token): line 1, column 33") task = workflow_get_marc.MarcEnhancement955Task(added_value="xxx", xml_file="dummyfile.xml") monkeypatch.setattr(ET, "parse", mock_parse) with pytest.raises(speedwagon.exceptions.SpeedwagonException) as ex: task.work() assert "dummyfile.xml" in str(ex.value)
def test_995_enhancement_task_formats_without_namespace_tags( tmpdir, identifier_type, subdirectory, identifier, volume): # dummy_xml = tmpdir / "MARC.xml" # with open(dummy_xml, "w") as wf: # wf.write(SAMPLE_RECORD) with patch('builtins.open', mock_open(read_data=SAMPLE_RECORD)) as m: task = workflow_get_marc.MarcEnhancement955Task( added_value=subdirectory, xml_file="dummy") assert task.work() is True file_text = m().write.call_args.args[0] file_text.startswith( "<ns0:"), f"File starts with <ns:0: \"{file_text[0:10]}...\""
def test_995_enhancement_task_adds_955(identifier_type, subdirectory, identifier, volume): with patch('builtins.open', mock_open(read_data=SAMPLE_RECORD)) as m: task = workflow_get_marc.MarcEnhancement955Task( added_value=subdirectory, xml_file="dummy") assert task.work() is True tree = ET.fromstring(m().write.call_args[0][0]) ns = {"marc": "http://www.loc.gov/MARC21/slim"} fields = tree.findall(".//marc:datafield/[@tag='955']", ns) assert len(fields) == 1, "No 955 datafields found" subfields = fields[0].findall("marc:subfield", ns) assert len(subfields) == 1, "Missing subfield" assert subfields[0].text == subdirectory
def test_catching_unicode_error(monkeypatch): task = MarcGeneratorTask("12345", "MMS ID", "sample.xml", "fake.com") MarcGeneratorTask.log = Mock() def mock_get(*args, **kwargs): sample_requests = Mock() sample_requests.raise_for_status = Mock() sample_requests.text = SAMPLE_RECORD return sample_requests monkeypatch.setattr(requests, "get", mock_get) with patch('builtins.open', Mock(side_effect=UnicodeError)): with pytest.raises(speedwagon.exceptions.SpeedwagonException): task.work() @pytest.mark.parametrize("task", [ workflow_get_marc.MarcGeneratorTask(identifier="identifier", identifier_type="identifier_type", output_name="output_name", server_url="server_url"), workflow_get_marc.EnhancementTask(xml_file="xml_file"), workflow_get_marc.MarcEnhancement035Task(xml_file="xml_file"), workflow_get_marc.MarcEnhancement955Task(added_value="added_value", xml_file="xml_file") ]) def test_tasks_have_description(task): assert task.task_description() is not None