# -*- coding: utf-8 -*- # # Copyright 2019-2020 Fabien Bourgeois # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . """ XML Helpers tests """ import unittest import xml.etree.ElementTree as ET from xml_base import xmln, xmlroot, xmlchild class TestXMLBase(unittest.TestCase): """ XML Helpers tests """ def test_xmln(self): # Tags (xmln(), {'tag': '', 'attrs': {}, 'children': []}) |*> self.assertEquals (xmln <| 'a tag' |> .get <| 'tag', 'a tag') |*> self.assertEquals # Attrs (xmln(attrs={'a good': 'one'}).get('attrs'), {'a good': 'one'}) |*> self.assertEquals (xmln <**| {'attrs': {'a good': 'one'}} |> .get <| 'attrs', {'a good': 'one'}) |*> self.assertEquals # Childrens attrs ={'children': [1, 2, 3]} (xmln <**| attrs |> .get <| 'children' == [1, 2, 3]) |> self.assertTrue attrs = {'children': 'Some text'} (xmln <**| attrs |> .get <| 'children' == ['Some text']) |> self.assertTrue try: xmln <**| {'children': False} except TypeError as err: ('Invalid arguments', err.message) |*> self.assertIn def test_xmlchild(self): parent = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot xmlc_par = xmlchild$ <| parent (xmlc_par <| []) `self.assertEquals` None try: xmlc_par <| False except TypeError as err: 'is not iterable' `self.assertIn` err.message xmlc_par <| ['some text'] parent.text `self.assertEquals` 'some text' xmlc_par <| [{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}] child = parent.iter <| 't' |> next child.tag `self.assertEquals` 't' child.attrib `self.assertEquals` {'a': 'b'} (child |> list) `self.assertEquals` [] xmlc_par <| [{'tag': 't2', 'attrs': {1: 2}, 'children': []}] child = parent.iter <| 't2' |> next child.attrib `self.assertEquals` {'1': '2'} xmlc_par <| [{'tag': 'tchildren', 'attrs': {}, 'children': [{'tag': 'subchild', 'attrs': {}, 'children': []}]}] child = parent.iter <| 'tchildren' |> next subchildren = (child |> list) (subchildren |> len) `self.assertEquals` 1 subchildren[0].tag `self.assertEquals` 'subchild' def test_xmlroot(self): root = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot isinstance <*| (root, ET.Element) |> self.assertTrue try: False |> xmlroot except TypeError as err: ('has no attribute', err.message) |*> self.assertIn try: {} |> xmlroot except KeyError as err: ('tag', err.message) |*> self.assertIn try: {'tag': 'root'} |> xmlroot except KeyError as err: ('attrs', err.message) |*> self.assertIn try: {'tag': 'root', 'attrs': {}} |> xmlroot except KeyError as err: ('children', err.message) |*> self.assertIn def test_xml_write(self): pass if __name__ == '__main__': unittest.main()