Skip to content
Snippets Groups Projects
Verified Commit da66ac22 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Test for xml (de)serialization.

parent 6ed0398a
No related branches found
No related tags found
No related merge requests found
Pipeline #33750 failed
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
# #
# Copyright (C) 2018 Research Group Biomedical Physics, # Copyright (C) 2018 Research Group Biomedical Physics,
# Max-Planck-Institute for Dynamics and Self-Organization Göttingen # 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 # This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as # it under the terms of the GNU Affero General Public License as
...@@ -25,9 +27,20 @@ ...@@ -25,9 +27,20 @@
@author: tf @author: tf
""" """
import os
import warnings
import caosdb as db
import pytest import pytest
def teardown_function(function):
d = db.execute_query("FIND Test*")
if len(d) > 0:
d.delete()
@pytest.mark.local_server @pytest.mark.local_server
def test_parse_xml(): def test_parse_xml():
from lxml import etree from lxml import etree
...@@ -120,3 +133,27 @@ def test_parse_xml(): ...@@ -120,3 +133,27 @@ def test_parse_xml():
assert isinstance(msg, Message) assert isinstance(msg, Message)
assert_equal(msg.type.lower(), tmp_msg_type.lower()) assert_equal(msg.type.lower(), tmp_msg_type.lower())
assert_equal(msg.body, tmp_msg_body) 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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment