133 lines
4.6 KiB
Plaintext
133 lines
4.6 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
|
|
|
|
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
|
|
|
|
# Bad arguments
|
|
try:
|
|
xmlc_par <| False
|
|
except TypeError as err:
|
|
'Invalid arguments for xmlchild' `self.assertIn` err.message
|
|
try:
|
|
xmlc_par <| []
|
|
except TypeError as err:
|
|
'Invalid arguments for xmlchild' `self.assertIn` err.message
|
|
try: # Need XMLDictElement, not dict
|
|
xmlc_par <| [{'tag': 't', 'attrs': {'a': 'b'}, 'children': []}]
|
|
except TypeError as err:
|
|
'Invalid arguments for xmlchild' `self.assertIn` err.message
|
|
|
|
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
|
|
|
|
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):
|
|
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__)
|
|
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)
|
|
|
|
if __name__ == '__main__':
|
|
unittest.main()
|