From 79a1245db05486fa15492584e123cd40709a4f1c Mon Sep 17 00:00:00 2001 From: Daniel Mach Date: Fri, 29 Nov 2024 15:44:17 +0100 Subject: [PATCH] Fix handling SimpleFlag model --- osc/obs_api/simple_flag.py | 7 +++++-- tests/test_models_xmlmodel.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/osc/obs_api/simple_flag.py b/osc/obs_api/simple_flag.py index 6544bb35..e2ac74b5 100644 --- a/osc/obs_api/simple_flag.py +++ b/osc/obs_api/simple_flag.py @@ -1,9 +1,9 @@ from ..util.models import * # pylint: disable=wildcard-import,unused-wildcard-import +from xml.etree import ElementTree as ET class SimpleFlag(XmlModel): XML_TAG = None - XML_TAG_FIELD = "flag" def __init__(self, flag, **kwargs): super().__init__(flag=flag, **kwargs) @@ -13,7 +13,6 @@ class SimpleFlag(XmlModel): DISABLE = "disable" flag: SimpleFlagChoices = Field( - xml_wrapped=True, xml_set_tag=True, ) @@ -22,3 +21,7 @@ class SimpleFlag(XmlModel): return self.flag == other.flag # allow comparing with a string return self.flag == other + + @classmethod + def from_xml(cls, root: ET.Element, *, apiurl: Optional[str] = None): + return cls(flag=root[0].tag) diff --git a/tests/test_models_xmlmodel.py b/tests/test_models_xmlmodel.py index 4f63a0e4..f2bd6159 100644 --- a/tests/test_models_xmlmodel.py +++ b/tests/test_models_xmlmodel.py @@ -3,6 +3,7 @@ import textwrap import unittest from osc.util.models import * +from osc.obs_api.simple_flag import SimpleFlag class TestXmlModel(unittest.TestCase): @@ -217,6 +218,27 @@ class TestXmlModel(unittest.TestCase): ).strip() self.assertRaises(TypeError, TestModel.from_string, data) + def test_simple_flag(self): + class TestModel(XmlModel): + XML_TAG = "model" + simple_flag: Optional[SimpleFlag] = Field( + xml_wrapped=True, + ) + + data = textwrap.dedent( + """ + + + + + + """ + ).strip() + + m = TestModel.from_string(data) + self.assertEqual(m.simple_flag, "enable") + self.assertEqual(data, m.to_string()) + if __name__ == "__main__": unittest.main()