# -*- coding: utf-8 -*- # # Copyright 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 """ from functools import partial import unittest import xml.etree.ElementTree as ET from os import unlink from src.xml_base import xmln, xmlroot, xmlchild, xml_write class TestXMLBase(unittest.TestCase): """ XML Helpers tests """ def test_xmln(self): """ Text xmln """ # Tags self.assertDictEqual(xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}) self.assertEqual(xmln('a tag').tag, 'a tag') # Attrs self.assertDictEqual(xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}) # Childrens self.assertListEqual(xmln(children=[1, 2, 3]).children, [1, 2, 3]) self.assertListEqual(xmln(children='Some text').children, ['Some text']) with self.assertRaisesRegexp(TypeError, 'Invalid arguments'): xmln(children=False) # Ensure that only children after tags is managed element = xmln('tag', {'something': 'inside'}) self.assertIsInstance(element.attrs, dict) self.assertIsInstance(element.children, list) element = xmln('tag', ['something', 'inside']) self.assertIsInstance(element.attrs, dict) self.assertIsInstance(element.children, list) def test_xmlchild(self): """ Test xmlchild """ parent = xmlroot({'tag': 'root', 'attrs': {}, 'children': []}) xmlc_par = partial(xmlchild, parent) # Bad arguments with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): xmlc_par(False) # Need XMLDictElement, not dict with self.assertRaisesRegexp(TypeError, 'Invalid arguments for xmlchild'): xmlc_par([{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}]) xmlc_par(['some text']) self.assertEqual(parent.text, 'some text') xmlc_par([xmln('t', {'a': 'b'}, [])]) child = parent.iter('t').next() self.assertEqual(child.tag, 't') self.assertDictEqual(child.attrib, {'a': 'b'}) self.assertListEqual(list(child), []) xmlc_par([xmln('t2', {1: 2}, [])]) child = parent.iter('t2').next() self.assertDictEqual(child.attrib, {'1': '2'}) xmlc_par([xmln('tchildren', {}, [xmln('subchild', {}, [])])]) child = parent.iter('tchildren').next() subchildren = list(child) self.assertEqual(len(subchildren), 1) self.assertEqual(subchildren[0].tag, 'subchild') def test_xmlroot(self): """ Test xmlroot """ root = xmlroot({'tag': 'root', 'attrs': {}, 'children': []}) self.assertIsInstance(root, ET.Element) with self.assertRaisesRegexp(TypeError, 'has no attribute'): xmlroot(False) with self.assertRaisesRegexp(KeyError, 'tag'): xmlroot({}) with self.assertRaisesRegexp(KeyError, 'attrs'): xmlroot({'tag': 'root'}) def test_xml_write(self): """ test xml_write """ children = [xmln('child1', {'attr': 'value'}, []), xmln('child2', {}, "Some text")] tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children}) xmlw = lambda p: xml_write(p, tree) self.assertIsNone(xmlw('/badpath')) self.assertIsNone(xmlw('/bad.ext')) xmlw(__file__) filepath = __file__.replace('.py', '_views.xml') with open(filepath, 'r') as output_file: output_xml = output_file.read() self.assertIn('', output_xml) self.assertIn('', output_xml) self.assertIn('Some text', output_xml) unlink(filepath) if __name__ == '__main__': unittest.main()