From da66ac2279bd3b61d23f9f8dacbc1d12c41fe5a4 Mon Sep 17 00:00:00 2001 From: Daniel <d.hornung@indiscale.com> Date: Fri, 17 Feb 2023 15:57:56 +0100 Subject: [PATCH] ENH: Test for xml (de)serialization. --- tests/test_xmlparsing.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/tests/test_xmlparsing.py b/tests/test_xmlparsing.py index 50fbbfc..70d8c79 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 -- GitLab