Пример #1
0
    def testSimplest(self):
        '''\
        <?xml version="1.0" ?>
        <user>
          <name>Alpha</name>
          <login>Bravo</login>
        </user>'''
        factory = XmlFactory('user')
        factory['name'] = 'Alpha'
        factory['login'] = '******'

        assert (factory.GetContents(xml_header=True) == dedent(
            self.testSimplest.__doc__))
        assert factory.AsDict() == {"login": "******", "name": "Alpha"}
        assert factory.AsJson() == '{"name": "Alpha", "login": "******"}'
Пример #2
0
    def testRepeatingTags(self):
        '''\
        <root>
          <elements>
            <name>Alpha</name>
            <name>Bravo</name>
            <name>Charlie</name>
          </elements>
          <components>
            <component>
              <name>Alpha</name>
            </component>
            <component>
              <name>Bravo</name>
            </component>
            <component>
              <name>Charlie</name>
            </component>
          </components>
        </root>'''
        factory = XmlFactory('root')
        factory['elements/name'] = 'Alpha'
        factory['elements/name+'] = 'Bravo'
        factory['elements/name+'] = 'Charlie'

        factory['components/component+/name'] = 'Alpha'
        factory['components/component+/name'] = 'Bravo'
        factory['components/component+/name'] = 'Charlie'

        assert (factory.GetContents() == dedent(
            self.testRepeatingTags.__doc__))
        assert factory.AsDict() == {
            "elements": {
                "name": ["Alpha", "Bravo", "Charlie"]
            },
            "components": {
                "component": [{
                    "name": "Alpha"
                }, {
                    "name": "Bravo"
                }, {
                    "name": "Charlie"
                }]
            }
        }
        assert factory.AsJson(
        ) == '{"elements": {"name": ["Alpha", "Bravo", "Charlie"]}, "components": {"component": [{"name": "Alpha"}, {"name": "Bravo"}, {"name": "Charlie"}]}}'
Пример #3
0
    def testAttributes(self):
        '''\
        <root>
          <alpha one="1" two="2">Alpha</alpha>
          <bravo>
            <charlie three="3"/>
          </bravo>
        </root>'''
        factory = XmlFactory('root')
        factory['alpha'] = 'Alpha'
        factory['alpha@one'] = '1'
        factory['alpha@two'] = '2'
        factory['bravo/charlie@three'] = '3'

        assert (factory.GetContents() == dedent(self.testAttributes.__doc__))
        # We're ignoring attributes and empty tags for now.
        assert factory.AsDict() == {
            "alpha": "Alpha",
            "bravo": {
                "charlie": None
            }
        }
        assert factory.AsJson(
        ) == '{"alpha": "Alpha", "bravo": {"charlie": null}}'
Пример #4
0
    def testSimple(self):
        '''\
        <user>
          <name>Alpha</name>
          <login>Bravo</login>
          <location>
            <city>Charlie</city>
          </location>
        </user>'''
        factory = XmlFactory('user')
        factory['name'] = 'Alpha'
        factory['login'] = '******'
        factory['location/city'] = 'Charlie'

        assert (factory.GetContents() == dedent(self.testSimple.__doc__))
        assert factory.AsDict() == {
            "login": "******",
            "name": "Alpha",
            "location": {
                "city": "Charlie"
            }
        }
        assert factory.AsJson(
        ) == '{"name": "Alpha", "login": "******", "location": {"city": "Charlie"}}'