diff --git a/mdto/mdto.py b/mdto/mdto.py index 93125fc..e0a07a9 100755 --- a/mdto/mdto.py +++ b/mdto/mdto.py @@ -6,6 +6,7 @@ from typing import TextIO, List from datetime import datetime from dataclasses import dataclass +from pathlib import Path import lxml.etree as ET # Make into an optional dependency? @@ -26,8 +27,8 @@ def _process_file(file_or_filename) -> TextIO: The returned file-object is always in read-only mode. """ - # filename? - if isinstance(file_or_filename, str): + # filename or path? + if isinstance(file_or_filename, (str, Path)): return open(file_or_filename, "r") # file-like object? elif hasattr(file_or_filename, "read"): diff --git a/tests/test_mdto_xsd.py b/tests/test_mdto_xsd.py index 4493a49..892a392 100644 --- a/tests/test_mdto_xsd.py +++ b/tests/test_mdto_xsd.py @@ -1,17 +1,19 @@ import pytest import lxml.etree as ET +from pathlib import Path from mdto import ( Informatieobject, IdentificatieGegevens, VerwijzingGegevens, BeperkingGebruikGegevens, BegripGegevens, + create_bestand, ) -def test_xml_validity(mdto_xsd): - """Test if outpt XML is valid according to the MDTO XSD""" - # create a schema object from the MDTO XSD +def test_informatieobject_xml_validity(mdto_xsd): + """Test if running to_xml() on a informatieobject procudes valid MDTO XML""" + # create a schema object from the MDTO XSD mdto_schema = ET.XMLSchema(ET.parse(mdto_xsd)) # create informatieobject informatieobject = Informatieobject( @@ -31,7 +33,30 @@ def test_xml_validity(mdto_xsd): # As a workaround, we serialize the ElemenTree object to a string, and then deserialize this # namespaced string. There are other ways to fix this, but their decreased readability does not # outweigh mildly complicating this test. - mdto_xml = ET.fromstring(ET.tostring(informatieobject.to_xml())) + informatieobject_xml = ET.fromstring(ET.tostring(informatieobject.to_xml())) # validate against schema - assert mdto_schema.validate(mdto_xml) + assert mdto_schema.validate(informatieobject_xml) + + +def test_automatic_bestand_xml_validity(mdto_xsd, voorbeeld_archiefstuk_xml): + """Test if running to_xml() on a automatically generated Bestand procudes valid MDTO XML""" + # create a schema object from the MDTO XSD + mdto_schema = ET.XMLSchema(ET.parse(mdto_xsd)) + + # use this .py file for automatic metadata generation + example_file = Path(__file__) + # create Bestand object from README.md + existing informatieobject + bestand = create_bestand( + example_file, + "abcd-1234", + "Corsa", + voorbeeld_archiefstuk_xml, + url="https://example.com/", + ) + + # see comment about lxml above + bestand_xml = ET.fromstring(ET.tostring(bestand.to_xml())) + + # validate against schema + assert mdto_schema.validate(bestand_xml)