yaltik_odoo_custom/yaltik_dsl/test_xml_base.py

157 lines
6.8 KiB
Python

#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# __coconut_hash__ = 0x731dbc72
# Compiled with Coconut version 1.4.3 [Ernest Scribbler]
""" XML Helpers tests """
# Coconut Header: -------------------------------------------------------------
from __future__ import print_function, absolute_import, unicode_literals, division
import sys as _coconut_sys, os.path as _coconut_os_path
_coconut_file_path = _coconut_os_path.dirname(_coconut_os_path.abspath(__file__))
_coconut_cached_module = _coconut_sys.modules.get(b"__coconut__")
if _coconut_cached_module is not None and _coconut_os_path.dirname(_coconut_cached_module.__file__) != _coconut_file_path:
del _coconut_sys.modules[b"__coconut__"]
_coconut_sys.path.insert(0, _coconut_file_path)
from __coconut__ import *
from __coconut__ import _coconut, _coconut_MatchError, _coconut_igetitem, _coconut_base_compose, _coconut_forward_compose, _coconut_back_compose, _coconut_forward_star_compose, _coconut_back_star_compose, _coconut_forward_dubstar_compose, _coconut_back_dubstar_compose, _coconut_pipe, _coconut_back_pipe, _coconut_star_pipe, _coconut_back_star_pipe, _coconut_dubstar_pipe, _coconut_back_dubstar_pipe, _coconut_bool_and, _coconut_bool_or, _coconut_none_coalesce, _coconut_minus, _coconut_map, _coconut_partial, _coconut_get_function_match_error, _coconut_base_pattern_func, _coconut_addpattern, _coconut_sentinel, _coconut_assert, _coconut_mark_as_match
# Compiled Coconut: -----------------------------------------------------------
# -*- 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/>.
import unittest
import xml.etree.ElementTree as ET
from os import unlink
from xml_base import xmln
from xml_base import xmlroot
from xml_base import xmlchild
from xml_base import xml_write
class TestXMLBase(unittest.TestCase):
""" XML Helpers tests """
def test_xmln(self):
# Tags
(self.assertEquals)(*(xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []}))
(self.assertEquals)(*((_coconut_partial(getattr, {1: 'tag'}, 2))((xmln)('a tag')), 'a tag'))
# Attrs
(self.assertEquals)(*(xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'}))
(self.assertEquals)(*((_coconut_partial(getattr, {1: 'attrs'}, 2))((xmln)(**{'attrs': {'a good': 'one'}})), {'a good': 'one'}))
# Childrens
attrs = {'children': [1, 2, 3]}
(self.assertTrue)(((_coconut_partial(getattr, {1: 'children'}, 2))((xmln)(**attrs)) == [1, 2, 3]))
attrs = {'children': 'Some text'}
(self.assertTrue)(((_coconut_partial(getattr, {1: 'children'}, 2))((xmln)(**attrs)) == ['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):
parent = (xmlroot)({'tag': 'root', 'attrs': {}, 'children': []})
xmlc_par = (_coconut.functools.partial(_coconut.functools.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.assertEquals)(parent.text, 'some text')
(xmlc_par)([(xmln)(**{'tag': 't', 'attrs': {'a': 'b'}, 'children': []})])
child = (next)((parent.iter)('t'))
(self.assertEquals)(child.tag, 't')
(self.assertEquals)(child.attrib, {'a': 'b'})
(self.assertEquals)(((list)(child)), [])
(xmlc_par)([(xmln)(**{'tag': 't2', 'attrs': {1: 2}, 'children': []})])
child = (next)((parent.iter)('t2'))
(self.assertEquals)(child.attrib, {'1': '2'})
(xmlc_par)([(xmln)(**{'tag': 'tchildren', 'attrs': {}, 'children': [(xmln)(**{'tag': 'subchild', 'attrs': {}, 'children': []})]})])
child = (next)((parent.iter)('tchildren'))
subchildren = ((list)(child))
(self.assertEquals)(((len)(subchildren)), 1)
(self.assertEquals)(subchildren[0].tag, 'subchild')
def test_xmlroot(self):
root = (xmlroot)({'tag': 'root', 'attrs': {}, 'children': []})
(self.assertTrue)((isinstance)(*(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):
children = [(xmln)(*('child1', {'attr': 'value'}, [])), (xmln)(*('child2', {}, "Some text"))]
tree = xmlroot({'tag': 'root', 'attrs': {}, 'children': children})
xmlw = _coconut_partial(xml_write, {1: tree}, 2)
(self.assertEquals)(((xmlw)('/badpath')), None)
(self.assertEquals)(((xmlw)('/bad.ext')), None)
xmlw(__file__) # Default to pretty
filepath = __file__.replace('.py', '_views.xml')
with open(filepath, 'r') as output_file:
output_xml = output_file.read()
(self.assertIn)('<?xml version', output_xml)
(self.assertIn)('<root>', output_xml)
(self.assertIn)('<child1 attr="value"/>', output_xml)
(self.assertIn)('<child2>Some text</child2>', 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()
(self.assertNotIn)('<?xml version', output_xml)
(self.assertIn)('<root>', output_xml)
(self.assertIn)('<child1 attr="value" />', output_xml)
(self.assertIn)('<child2>Some text</child2>', output_xml)
unlink(filepath)
if __name__ == '__main__':
unittest.main()