File: test_models_xmlmodel.py

package info (click to toggle)
osc 1.15.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,616 kB
  • sloc: python: 33,563; sh: 1,846; xml: 157; makefile: 36; csh: 14
file content (244 lines) | stat: -rw-r--r-- 7,296 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
import io
import textwrap
import unittest

from osc.util.models import *
from osc.obs_api.simple_flag import SimpleFlag


class TestXmlModel(unittest.TestCase):
    def test_attribute(self):
        class TestModel(XmlModel):
            XML_TAG = "tag"
            value: str = Field(xml_attribute=True)

        m = TestModel(value="FOO")
        self.assertEqual(m.dict(), {"value": "FOO"})
        expected = """<tag value="FOO" />"""
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = TestModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_element(self):
        class TestModel(XmlModel):
            XML_TAG = "tag"
            value: str = Field()

        m = TestModel(value="FOO")
        self.assertEqual(m.dict(), {"value": "FOO"})
        expected = textwrap.dedent(
            """
            <tag>
              <value>FOO</value>
            </tag>
            """
        ).strip()
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = TestModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_element_list(self):
        class TestModel(XmlModel):
            XML_TAG = "tag"
            value_list: List[str] = Field(xml_name="value")

        m = TestModel(value_list=["FOO", "BAR"])
        self.assertEqual(m.dict(), {"value_list": ["FOO", "BAR"]})
        expected = textwrap.dedent(
            """
            <tag>
              <value>FOO</value>
              <value>BAR</value>
            </tag>
            """
        ).strip()
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = TestModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_child_model(self):
        class ChildModel(XmlModel):
            XML_TAG = "child"
            value: str = Field()

        class ParentModel(XmlModel):
            XML_TAG = "parent"
            text: str = Field()
            child: ChildModel = Field()

        m = ParentModel(text="TEXT", child={"value": "FOO"})
        expected = textwrap.dedent(
            """
            <parent>
              <text>TEXT</text>
              <child>
                <value>FOO</value>
              </child>
            </parent>
            """
        ).strip()
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = ParentModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_child_model_list(self):
        class ChildModel(XmlModel):
            XML_TAG = "child"
            value: str = Field()

        class ParentModel(XmlModel):
            XML_TAG = "parent"
            text: str = Field()
            child: List[ChildModel] = Field()

        m = ParentModel(text="TEXT", child=[{"value": "FOO"}, {"value": "BAR"}])
        expected = textwrap.dedent(
            """
            <parent>
              <text>TEXT</text>
              <child>
                <value>FOO</value>
              </child>
              <child>
                <value>BAR</value>
              </child>
            </parent>
            """
        ).strip()
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = ParentModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_child_model_list_wrapped(self):
        class ChildModel(XmlModel):
            XML_TAG = "child"
            value: str = Field()

        class ParentModel(XmlModel):
            XML_TAG = "parent"
            text: str = Field()
            child: List[ChildModel] = Field(xml_wrapped=True, xml_name="children")

        m = ParentModel(text="TEXT", child=[{"value": "FOO"}, {"value": "BAR"}])
        expected = textwrap.dedent(
            """
            <parent>
              <text>TEXT</text>
              <children>
                <child>
                  <value>FOO</value>
                </child>
                <child>
                  <value>BAR</value>
                </child>
              </children>
            </parent>
            """
        ).strip()
        self.assertEqual(m.to_string(), expected)

        # verify that we can also load the serialized data
        m = ParentModel.from_string(expected)
        self.assertEqual(m.to_string(), expected)

    def test_apiurl(self):
        class ChildModel(XmlModel):
            XML_TAG = "child"
            value: str = Field()

        class ParentModel(XmlModel):
            XML_TAG = "parent"
            text: str = Field()
            child: List[ChildModel] = Field(xml_wrapped=True, xml_name="children")

        # serialize the model and load it with apiurl set
        m = ParentModel(text="TEXT", child=[{"value": "FOO"}, {"value": "BAR"}])
        xml = m.to_string()

        apiurl = "https://api.example.com"

        m = ParentModel.from_string(xml, apiurl=apiurl)
        m.child.append({"value": "BAZ"})

        self.assertEqual(m._apiurl, apiurl)
        self.assertEqual(m.child[0]._apiurl, apiurl)
        self.assertEqual(m.child[1]._apiurl, apiurl)
        self.assertEqual(m.child[2]._apiurl, apiurl)

        # test the same as above but with a file
        f = io.StringIO(xml)

        m = ParentModel.from_file(f, apiurl=apiurl)
        m.child.append({"value": "BAZ"})

        self.assertEqual(m._apiurl, apiurl)
        self.assertEqual(m.child[0]._apiurl, apiurl)
        self.assertEqual(m.child[1]._apiurl, apiurl)
        self.assertEqual(m.child[2]._apiurl, apiurl)

    def test_empty_int_optional(self):
        class TestModel(XmlModel):
            XML_TAG = "model"
            num_attr: Optional[int] = Field(xml_attribute=True)
            num_elem: Optional[int] = Field()

        data = textwrap.dedent(
            """
            <model num_attr="">
              <num_elem> </num_elem>
            </model>
            """
        ).strip()
        m = TestModel.from_string(data)
        self.assertEqual(m.num_attr, None)
        self.assertEqual(m.num_elem, None)

    def test_empty_int(self):
        class TestModel(XmlModel):
            XML_TAG = "model"
            num_attr: int = Field(xml_attribute=True)
            num_elem: int = Field()

        data = textwrap.dedent(
            """
            <model num_attr="">
              <num_elem> </num_elem>
            </model>
            """
        ).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(
            """
            <model>
              <simple_flag>
                <enable />
              </simple_flag>
            </model>
            """
        ).strip()

        m = TestModel.from_string(data)
        self.assertEqual(m.simple_flag, "enable")
        self.assertEqual(data, m.to_string())


if __name__ == "__main__":
    unittest.main()