diff --git a/tests/test_xmlparsing.py b/tests/test_xmlparsing.py index 50fbbfc07d37aeda1982c7db5c2fc256826a027c..70d8c792af781d95c36b20b075a5382ef019b70c 100644 --- a/tests/test_xmlparsing.py +++ b/tests/test_xmlparsing.py @@ -5,6 +5,8 @@ # # Copyright (C) 2018 Research Group Biomedical Physics, # Max-Planck-Institute for Dynamics and Self-Organization Göttingen +# Copyright (C) 2023 Indiscale GmbH <info@indiscale.com> +# Copyright (C) 2023 Daniel Hornung <d.hornung@indiscale.com> # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as @@ -25,9 +27,20 @@ @author: tf """ + +import os +import warnings + +import caosdb as db import pytest +def teardown_function(function): + d = db.execute_query("FIND Test*") + if len(d) > 0: + d.delete() + + @pytest.mark.local_server def test_parse_xml(): from lxml import etree @@ -120,3 +133,27 @@ def test_parse_xml(): assert isinstance(msg, Message) assert_equal(msg.type.lower(), tmp_msg_type.lower()) assert_equal(msg.body, tmp_msg_body) + + +def test_container_xml(tmp_path): + """Test if after writing to xml, the result can be read without problems. + +For issue 89: https://gitlab.com/caosdb/caosdb-pylib/-/issues/89 +""" + with warnings.catch_warnings(): + + # Create XML with container with one RecordType. + db.RecordType(name="TestRT").insert() + cont = db.execute_query("FIND RecordType TestRT") + xml_file = os.path.join(tmp_path, "tmp.xml") + with open(xml_file, "w", encoding="utf-8") as opened: + opened.write(str(cont)) + + # Read from file again. + with open(xml_file, "r", encoding="utf-8") as opened: + xml_str = opened.read() + cont_restored = db.Container.from_xml(xml_str) + + # Test if the serialization and deserialization worked. + assert cont[0].id == cont_restored[0].id + assert cont[0].name == cont_restored[0].name