yaltik_odoo_custom/yaltik_dsl/test_xml_base.coco

133 lines
5.2 KiB
Plaintext

# -*- coding: utf-8 -*-
#
# Copyright 2020 Fabien Bourgeois <fabien@yaltik.com>
#
# 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 <http://www.gnu.org/licenses/>.
""" XML Helpers tests """
import unittest
import xml.etree.ElementTree as ET
from os import unlink
from xml_base import xmln, xmlroot, xmlchild, xml_write
class TestXMLBase(unittest.TestCase):
""" XML Helpers tests """
def test_xmln(self):
# Tags
(xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}) |*> self.assertEquals
(xmln <| 'a tag' |> getattr$(?, 'tag'), 'a tag') |*> self.assertEquals
# Attrs
(xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}) |*> self.assertEquals
(xmln <**| {'attrs': {'a good': 'one'}} |> getattr$(?, 'attrs'), {'a good': 'one'}) |*> self.assertEquals
# Childrens
attrs ={'children': [1, 2, 3]}
(xmln <**| attrs |> getattr$(?, 'children') == [1, 2, 3]) |> self.assertTrue
attrs = {'children': 'Some text'}
(xmln <**| attrs |> getattr$(?, 'children') == ['Some text']) |> self.assertTrue
with self.assertRaisesRegexp(TypeError, 'Invalid arguments'):
xmln <**| {'children': False}
# Ensure that only children after tags is managed
element = xmln <*| ('tag', {'something': 'inside'})
element.attrs `self.assertIsInstance` dict
element.children `self.assertIsInstance` list
element = xmln <*| ('tag', ['something', 'inside'])
element.attrs `self.assertIsInstance` dict
element.children `self.assertIsInstance` list
def test_xmlchild(self):
parent = {'tag': 'root', 'attrs': {}, 'children': []} |> xmlroot
xmlc_par = 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']
parent.text `self.assertEquals` 'some text'
xmlc_par <| [xmln <**| {'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 <| [xmln <**| {'tag': 't2', 'attrs': {1: 2}, 'children': []}]
child = parent.iter <| 't2' |> next
child.attrib `self.assertEquals` {'1': '2'}
xmlc_par <| [xmln <**| {'tag': 'tchildren', 'attrs': {},
'children': [xmln <**| {'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
with self.assertRaisesRegexp(TypeError, 'has no attribute'):
False |> xmlroot
with self.assertRaisesRegexp(KeyError, 'tag'):
{} |> xmlroot
with self.assertRaisesRegexp(KeyError, 'attrs'):
{'tag': 'root'} |> xmlroot
def test_xml_write(self):
children = [('child1', {'attr': 'value'}, []) |*> xmln,
('child2', {}, "Some text") |*> xmln]
tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children})
xmlw = xml_write$(?, tree)
('/badpath' |> xmlw) `self.assertEquals` None
('/bad.ext' |> xmlw) `self.assertEquals` None
xmlw(__file__) # Default to pretty
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
'<?xml version' `self.assertIn` output_xml
'<root>' `self.assertIn` output_xml
'<child1 attr="value"/>' `self.assertIn` output_xml
'<child2>Some text</child2>' `self.assertIn` output_xml
unlink(filepath)
xmlw(__file__, pretty=False)
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
'<?xml version' `self.assertNotIn` output_xml
'<root>' `self.assertIn` output_xml
'<child1 attr="value" />' `self.assertIn` output_xml
'<child2>Some text</child2>' `self.assertIn` output_xml
unlink(filepath)
if __name__ == '__main__':
unittest.main()