yaltik_odoo_custom/yaltik_dsl/tests/test_xml_base.py

173 lines
7.6 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# __coconut_hash__ = 0xfa849aa3
# Compiled with Coconut version 2.1.1 [The Spanish Inquisition]
""" XML Helpers tests """
# Coconut Header: -------------------------------------------------------------
from __future__ import generator_stop, annotations
import sys as _coconut_sys, os as _coconut_os
_coconut_file_dir = _coconut_os.path.dirname(_coconut_os.path.abspath(__file__))
_coconut_cached_module = _coconut_sys.modules.get("__coconut__")
if _coconut_cached_module is not None and _coconut_os.path.dirname(_coconut_cached_module.__file__) != _coconut_file_dir: # type: ignore
del _coconut_sys.modules["__coconut__"]
_coconut_sys.path.insert(0, _coconut_file_dir)
_coconut_module_name = _coconut_os.path.splitext(_coconut_os.path.basename(_coconut_file_dir))[0]
if _coconut_module_name and _coconut_module_name[0].isalpha() and all(c.isalpha() or c.isdigit() for c in _coconut_module_name) and "__init__.py" in _coconut_os.listdir(_coconut_file_dir):
_coconut_full_module_name = str(_coconut_module_name + ".__coconut__")
import __coconut__ as _coconut__coconut__
_coconut__coconut__.__name__ = _coconut_full_module_name
for _coconut_v in vars(_coconut__coconut__).values():
if getattr(_coconut_v, "__module__", None) == "__coconut__":
try:
_coconut_v.__module__ = _coconut_full_module_name
except AttributeError:
_coconut_v_type = type(_coconut_v)
if getattr(_coconut_v_type, "__module__", None) == "__coconut__":
_coconut_v_type.__module__ = _coconut_full_module_name
_coconut_sys.modules[_coconut_full_module_name] = _coconut__coconut__
from __coconut__ import *
from __coconut__ import _namedtuple_of, _coconut, _coconut_super, _coconut_MatchError, _coconut_iter_getitem, _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_star_pipe, _coconut_dubstar_pipe, _coconut_back_pipe, _coconut_back_star_pipe, _coconut_back_dubstar_pipe, _coconut_none_pipe, _coconut_none_star_pipe, _coconut_none_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_raise, _coconut_mark_as_match, _coconut_reiterable, _coconut_self_match_types, _coconut_dict_merge, _coconut_exec, _coconut_comma_op, _coconut_multi_dim_arr, _coconut_mk_anon_namedtuple, _coconut_matmul
_coconut_sys.path.pop(0)
# Compiled Coconut: -----------------------------------------------------------
2021-02-22 18:08:11 +01:00
# -*- 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/>.
2021-02-22 18:08:11 +01:00
from functools import partial
import unittest
import xml.etree.ElementTree as ET
from os import unlink
from src.xml_base import xmln
from src.xml_base import xmlroot
from src.xml_base import xmlchild
from src.xml_base import xml_write
2021-02-22 18:08:11 +01:00
class TestXMLBase(unittest.TestCase):
""" XML Helpers tests """
def test_xmln(self):
""" Text xmln """
# Tags
2021-02-22 18:08:11 +01:00
self.assertDictEqual(xmln()._asdict(), {'tag': '', 'attrs': {}, 'children': []})
self.assertEqual(xmln('a tag').tag, 'a tag')
# Attrs
2021-02-22 18:08:11 +01:00
self.assertDictEqual(xmln(attrs={'a good': 'one'}).attrs, {'a good': 'one'})
# Childrens
2021-02-22 18:08:11 +01:00
self.assertListEqual(xmln(children=[1, 2, 3]).children, [1, 2, 3])
self.assertListEqual(xmln(children='Some text').children, ['Some text',])
2021-02-22 18:08:11 +01:00
with self.assertRaisesRegex(TypeError, 'Invalid arguments'):
xmln(children=False)
# Ensure that only children after tags is managed
2021-02-22 18:08:11 +01:00
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)
2021-02-22 18:08:11 +01:00
def test_xmlchild(self):
""" Test xmlchild """
parent = xmlroot({'tag': 'root', 'attrs': {}, 'children': []})
xmlc_par = partial(xmlchild, parent)
# Bad arguments
2021-02-22 18:08:11 +01:00
with self.assertRaisesRegex(TypeError, 'Invalid arguments for xmlchild'):
xmlc_par(False)
# Need XMLDictElement, not dict
2021-02-22 18:08:11 +01:00
with self.assertRaisesRegex(TypeError, 'Invalid arguments for xmlchild'):
xmlc_par([{'tag': 't', 'attrs': {'a': 'b'}, 'children': []},])
2021-02-22 18:08:11 +01:00
xmlc_par(['some text',])
2021-02-22 18:08:11 +01:00
self.assertEqual(parent.text, 'some text')
xmlc_par([xmln('t', {'a': 'b'}, []),])
2021-02-22 18:08:11 +01:00
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}, []),])
2021-02-22 18:08:11 +01:00
child = parent.iter('t2').__next__()
self.assertDictEqual(child.attrib, {'1': '2'})
xmlc_par([xmln('tchildren', {}, [xmln('subchild', {}, []),]),])
2021-02-22 18:08:11 +01:00
child = parent.iter('tchildren').__next__()
subchildren = list(child)
self.assertEqual(len(subchildren), 1)
self.assertEqual(subchildren[0].tag, 'subchild')
2021-02-22 18:08:11 +01:00
def test_xmlroot(self):
""" Test xmlroot """
root = xmlroot({'tag': 'root', 'attrs': {}, 'children': []})
self.assertIsInstance(root, ET.Element)
with self.assertRaisesRegex(TypeError, 'not subscriptable'):
xmlroot(False)
with self.assertRaisesRegex(KeyError, 'tag'):
xmlroot({})
with self.assertRaisesRegex(KeyError, 'attrs'):
xmlroot({'tag': 'root'})
2021-02-22 18:08:11 +01:00
def test_xml_write(self):
""" test xml_write """
children = [xmln('child1', {'attr': 'value'}, []), xmln('child2', {}, "Some text")]
2021-02-22 18:08:11 +01:00
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('<?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)
xml_write(__file__, tree, suffix='_data')
filepath = __file__.replace('.py', '_data.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)
2021-02-22 18:08:11 +01:00
2021-02-22 18:08:11 +01:00
if __name__ == '__main__':
unittest.main()