Example #1
0
 def test_xml2dict_ident_error(self):
     """Ensure that exceptions raised while parsing bad idents include location information."""
     test_xml = "<foo>_bad</foo>"
     schema = {'type': 'ident', 'name': 'foo'}
     with self.assertRaises(SystemParseError) as exc:
         xml2dict(xml_parse_string(test_xml), schema)
     self.assertIn('<string>:1.0', str(exc.exception))
Example #2
0
def test_xml2dict_object():
    schema = {
        "type": "dict",
        "name": "foo",
        "dict_type": (
            [
                {
                    "type": "list",
                    "name": "bars",
                    "list_type": {
                        "type": "dict",
                        "name": "bar",
                        "dict_type": ([{"name": "name", "type": "string"}], []),
                    },
                },
                {
                    "type": "list",
                    "name": "bazs",
                    "list_type": {
                        "type": "dict",
                        "name": "baz",
                        "dict_type": (
                            [
                                {"name": "name", "type": "string"},
                                {"name": "bar", "type": "object", "object_group": "bars"},
                            ],
                            [],
                        ),
                    },
                },
            ],
            [],
        ),
    }
    test_xml = """<foo>
<bars>
 <bar><name>A</name></bar>
 <bar><name>B</name></bar>
</bars>
<bazs>
 <baz><name>X</name><bar>A</bar></baz>
</bazs>
</foo>
"""
    parsed = xml2dict(xml_parse_string(test_xml), schema)
    assert parsed["bazs"][0]["bar"]["name"] == "A"

    bad_xml = """<foo>
<bars>
 <bar><name>A</name></bar>
 <bar><name>B</name></bar>
</bars>
<bazs>
 <baz><name>X</name><bar>AA</bar></baz>
</bazs>
</foo>
"""
    with assert_raises(SystemParseError) as e:
        parsed = xml2dict(xml_parse_string(bad_xml), schema)
Example #3
0
 def test_schema_default_none(self):
     test_xml = "<foo></foo>"
     schema = {
         'type': 'dict',
         'name': 'foo',
         'dict_type': ([{'type': 'string',
                         'name': 'foo'}], [])
     }
     with self.assertRaises(SystemParseError):
         xml2dict(xml_parse_string(test_xml), schema)
Example #4
0
 def test_schema_default_value(self):
     schema = {
         'type': 'dict',
         'name': 'foo',
         'dict_type': ([{'type': 'string',
                         'name': 'bar',
                         'default': 'FOO'}], [])
     }
     self.assertEqual(xml2dict(xml_parse_string("<foo></foo>"), schema), {'bar': 'FOO'})
     self.assertEqual(xml2dict(xml_parse_string("<foo><bar>BAZ</bar></foo>"), schema), {'bar': 'BAZ'})
Example #5
0
 def test_xml2dict_autoindex(self):  # pylint: disable=no-self-use
     test_xml = "<list><x><name>foo</name></x><x><name>bar</name></x><x><name>x</name></x></list>"
     schema = {
         'type': 'list',
         'name': 'list',
         'auto_index_field': 'idx',
         'list_type': {'type': 'dict',
                       'name': 'x',
                       'dict_type': ([{'name': 'name', 'type': 'string'}], [])}
     }
     xml2dict(xml_parse_string(test_xml), schema)
Example #6
0
def test_xml2dict_optional():
    schema = {"type": "dict", "name": "x", "dict_type": ([{"type": "bool", "name": "b"}], [])}

    test_xml = "<x><b>true</b></x>"
    x = xml2dict(xml_parse_string(test_xml), schema)
    assert x["b"]

    test_xml = "<x></x>"
    with assert_raises(SystemParseError):
        x = xml2dict(xml_parse_string(test_xml), schema)

    # Make the child element optional instead.
    schema["dict_type"][0][0]["optional"] = True
    x = xml2dict(xml_parse_string(test_xml), schema)
    assert x["b"] is None
Example #7
0
def test_xml2dict_bool():
    schema = {"type": "bool", "name": "b"}

    test_xml = "<b>true</b>"
    assert xml2dict(xml_parse_string(test_xml), schema)

    test_xml = "<b>false</b>"
    assert not xml2dict(xml_parse_string(test_xml), schema)

    test_xml = "<b>TrUe</b>"
    assert xml2dict(xml_parse_string(test_xml), schema)

    with assert_raises(SystemParseError):
        test_xml = "<b>bad</b>"
        assert xml2dict(xml_parse_string(test_xml), schema)
def test_xml2dict_bool():
    schema = {'type': 'bool', 'name': 'b'}

    test_xml = "<b>true</b>"
    assert xml2dict(xml_parse_string(test_xml), schema)

    test_xml = "<b>false</b>"
    assert not xml2dict(xml_parse_string(test_xml), schema)

    test_xml = "<b>TrUe</b>"
    assert xml2dict(xml_parse_string(test_xml), schema)

    with assert_raises(SystemParseError):
        test_xml = "<b>bad</b>"
        assert xml2dict(xml_parse_string(test_xml), schema)
Example #9
0
    def test_xml2dict_bool(self):
        schema = {'type': 'bool', 'name': 'b'}

        test_xml = "<b>true</b>"
        self.assertTrue(xml2dict(xml_parse_string(test_xml), schema))

        test_xml = "<b>false</b>"
        self.assertFalse(xml2dict(xml_parse_string(test_xml), schema))

        test_xml = "<b>TrUe</b>"
        self.assertTrue(xml2dict(xml_parse_string(test_xml), schema))

        with self.assertRaises(SystemParseError):
            test_xml = "<b>bad</b>"
            self.assertTrue(xml2dict(xml_parse_string(test_xml), schema))
def test_xml2dict_ident_error():
    """Ensure that exceptions raised while parsing bad idents include location information."""
    test_xml = "<foo>_bad</foo>"
    schema = {'type': 'ident', 'name': 'foo'}
    with assert_raises(SystemParseError) as e:
        x = xml2dict(xml_parse_string(test_xml), schema)
    assert '<string>:1.0' in str(e.exception)
Example #11
0
def test_xml2dict_ident_error():
    """Ensure that exceptions raised while parsing bad idents include location information."""
    test_xml = "<foo>_bad</foo>"
    schema = {"type": "ident", "name": "foo"}
    with assert_raises(SystemParseError) as e:
        x = xml2dict(xml_parse_string(test_xml), schema)
    assert "<string>:1.0" in str(e.exception)
Example #12
0
 def test_xml2dict_simple_list(self):
     xml = """<list>
            <li>foo</li>
            <li>bar</li>
            <li>baz</li>
           </list>"""
     expected = ['foo', 'bar', 'baz']
     self.assertEqual(xml2dict(xml_parse_string(xml)), expected)
Example #13
0
 def test_xml2dict_simple_dict(self):
     xml = """<dict>
       <a>foo</a>
       <b>bar</b>
       <c>baz</c>
      </dict>"""
     expected = {'a': 'foo', 'b': 'bar', 'c': 'baz'}
     self.assertEqual(xml2dict(xml_parse_string(xml)), expected)
Example #14
0
def test_xml2dict_autoindex():
    test_xml = "<list><x><name>foo</name></x><x><name>bar</name></x><x><name>x</name></x></list>"
    schema = {
        "type": "list",
        "name": "list",
        "auto_index_field": "idx",
        "list_type": {"type": "dict", "name": "x", "dict_type": ([{"name": "name", "type": "string"}], [])},
    }
    x = xml2dict(xml_parse_string(test_xml), schema)
Example #15
0
    def test_xml2dict_object(self):
        schema = {
            'type': 'dict',
            'name': 'foo',
            'dict_type': (
                [{'type': 'list',
                  'name': 'bars',
                  'list_type': {'type': 'dict',
                                'name': 'bar',
                                'dict_type': ([{'name': 'name', 'type': 'string'}], [])}},
                 {'type': 'list',
                  'name': 'bazs',
                  'list_type': {'type': 'dict',
                                'name': 'baz',
                                'dict_type': ([{'name': 'name', 'type': 'string'},
                                               {'name': 'bar', 'type': 'object', 'object_group': 'bars'}], [])}}],
                [])
        }
        test_xml = """<foo>
<bars>
 <bar><name>A</name></bar>
 <bar><name>B</name></bar>
</bars>
<bazs>
 <baz><name>X</name><bar>A</bar></baz>
</bazs>
</foo>
"""
        parsed = xml2dict(xml_parse_string(test_xml), schema)
        self.assertEqual(parsed['bazs'][0]['bar']['name'], 'A')

        bad_xml = """<foo>
<bars>
 <bar><name>A</name></bar>
 <bar><name>B</name></bar>
</bars>
<bazs>
 <baz><name>X</name><bar>AA</bar></baz>
</bazs>
</foo>
"""
        with self.assertRaises(SystemParseError):
            parsed = xml2dict(xml_parse_string(bad_xml), schema)
Example #16
0
    def test_xml2dict_optional(self):
        schema = {
            'type': 'dict',
            'name': 'x',
            'dict_type': ([{'type': 'bool', 'name': 'b'}], [])
        }

        test_xml = "<x><b>true</b></x>"
        test_dict = xml2dict(xml_parse_string(test_xml), schema)
        self.assertTrue(test_dict['b'])

        test_xml = "<x></x>"
        with self.assertRaises(SystemParseError):
            test_dict = xml2dict(xml_parse_string(test_xml), schema)

        # Make the child element optional instead.
        schema['dict_type'][0][0]['optional'] = True
        test_dict = xml2dict(xml_parse_string(test_xml), schema)
        self.assertIsNone(test_dict['b'])
Example #17
0
    def configure(self, xml_config):
        """Configure a module.

        `xml_config` is an XML element describing the configuration of the module.
        The method should return an object, which will later be passed to `prepare` and `validate`.
        This method can raise exceptions if the `xml_config` is invalid.

        """
        if self.schema is NOTHING:
            return None
        return xml2dict(xml_config, self.schema)
Example #18
0
    def configure(self, xml_config):
        """Configure a module.

        `xml_config` is an XML element describing the configuration of the module.
        The method should return an object, which will later be passed to `prepare` and `validate`.
        This method can raise exceptions if the `xml_config` is invalid.

        """
        if self.schema is NOTHING:
            return None
        else:
            return xml2dict(xml_config, self.schema)
def test_xml2dict_optional():
    schema = {
        'type': 'dict',
        'name': 'x',
        'dict_type': ([{
            'type': 'bool',
            'name': 'b'
        }], [])
    }

    test_xml = "<x><b>true</b></x>"
    x = xml2dict(xml_parse_string(test_xml), schema)
    assert x['b']

    test_xml = "<x></x>"
    with assert_raises(SystemParseError):
        x = xml2dict(xml_parse_string(test_xml), schema)

    # Make the child element optional instead.
    schema['dict_type'][0][0]['optional'] = True
    x = xml2dict(xml_parse_string(test_xml), schema)
    assert x['b'] is None
def test_xml2dict_autoindex():
    test_xml = "<list><x><name>foo</name></x><x><name>bar</name></x><x><name>x</name></x></list>"
    schema = {
        'type': 'list',
        'name': 'list',
        'auto_index_field': 'idx',
        'list_type': {
            'type': 'dict',
            'name': 'x',
            'dict_type': ([{
                'name': 'name',
                'type': 'string'
            }], [])
        }
    }
    x = xml2dict(xml_parse_string(test_xml), schema)
Example #21
0
 def check(xml, result):
     assert xml2dict(xml_parse_string(xml)) == result
Example #22
0
 def test_xml2dict_length_prop(self):
     test_xml = "<list><li>foo</li><li>bar</li><li>baz</li></list>"
     test_dict = xml2dict(xml_parse_string(test_xml))
     self.assertEqual(test_dict.length, 3)
Example #23
0
def test_schema_default_none():
    test_xml = "<foo></foo>"
    schema = {"type": "dict", "name": "foo", "dict_type": ([{"type": "string", "name": "foo"}], [])}
    with assert_raises(SystemParseError):
        xml2dict(xml_parse_string(test_xml), schema)
Example #24
0
def test_schema_default_value():
    schema = {"type": "dict", "name": "foo", "dict_type": ([{"type": "string", "name": "bar", "default": "FOO"}], [])}
    assert xml2dict(xml_parse_string("<foo></foo>"), schema) == {"bar": "FOO"}
    assert xml2dict(xml_parse_string("<foo><bar>BAZ</bar></foo>"), schema) == {"bar": "BAZ"}
Example #25
0
 def test_xml2dict_empty_node(self):
     xml = """<list></list>"""
     expected = ''
     self.assertEqual(xml2dict(xml_parse_string(xml)), expected)
Example #26
0
 def test_xml2dict_single_item_list(self):
     xml = """<list>
       <a>foo</a>
      </list>"""
     expected = ['foo']
     self.assertEqual(xml2dict(xml_parse_string(xml)), expected)
Example #27
0
def test_xml2dict_length_prop():
    test_xml = "<list><li>foo</li><li>bar</li><li>baz</li></list>"
    x = xml2dict(xml_parse_string(test_xml))
    assert x.length == 3
 def check(xml, result):
     assert xml2dict(xml_parse_string(xml)) == result
def test_xml2dict_length_prop():
    test_xml = "<list><li>foo</li><li>bar</li><li>baz</li></list>"
    x = xml2dict(xml_parse_string(test_xml))
    assert x.length == 3