yaltik_odoo_custom/yaltik_dsl/tests/test_odoo.py

272 lines
14 KiB
Python
Raw Normal View History

2021-02-22 18:08:11 +01:00
# 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/>.
""" Odoo Helpers tests """
2021-02-22 18:08:11 +01:00
import unittest
import xml.etree.ElementTree as ET
from src.xml_base import XMLDictElement
import src.odoo_dsl as od
class TestOdooBase(unittest.TestCase):
""" Odoo Helpers tests """
def test_odoo(self):
""" Test odoo function """
element = od.odoo([])
self.assertIsInstance(element, ET.Element)
self.assertEqual(element.tag, 'odoo')
def test_data(self):
""" Test data function """
element = od.data([])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'data')
self.assertEqual(element.attrs, {})
element = od.data({"one": "attribute"}, [])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'data')
self.assertEqual(element.attrs, {"one": "attribute"})
def test_aliases(self):
""" Test simple aliases to xmln """
element = od.record({"one": "attribute"}, 'A child')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs, {"one": "attribute"})
self.assertEqual(element.children, ['A child'])
2021-02-22 18:08:11 +01:00
element = od.tree()
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'tree')
def test_act_window_model(self):
""" Test act_window function """
element = od.act_window_model('sample.model', {'view_type': 'form'})
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'act_window')
self.assertEqual(element.attrs['view_type'], 'form')
self.assertEqual(element.attrs['id'], 'sample_model_view_action')
self.assertEqual(element.attrs['res_model'], 'sample.model')
self.assertEqual(element.attrs['name'], 'Sample Model Action')
def test_menunitem_model(self):
""" Test menuitem function """
element = od.menuitem_model('sample.model', {'groups': 'base.user_employee'})
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'menuitem')
self.assertEqual(element.attrs['groups'], 'base.user_employee')
self.assertEqual(element.attrs['id'], 'sample_model_menu')
self.assertEqual(element.attrs['action'], 'sample_model_view_action')
def test_attribute(self):
""" Test attribute function """
element = od.attribute('invisible', "1")
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'attribute')
self.assertEqual(element.attrs['name'], 'invisible')
self.assertEqual(element.children, ["1"])
2021-02-22 18:08:11 +01:00
def test_fields(self):
""" Test fields function """
element = od.field({"one": "attribute"}, 'A child')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'field')
element = od.field_name('A name')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'field')
self.assertEqual(element.attrs['name'], 'name')
self.assertEqual(element.children, ['A name'])
2021-02-22 18:08:11 +01:00
element = od.field_model('sample.model')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'field')
self.assertEqual(element.attrs['name'], 'model')
self.assertEqual(element.children, ['sample.model'])
2021-02-22 18:08:11 +01:00
element = od.field_inherit('module.xml_view')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'field')
self.assertEqual(element.attrs['name'], 'inherit_id')
self.assertEqual(element.attrs['ref'], 'module.xml_view')
self.assertFalse(element.children)
element = od.field_arch()
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'field')
self.assertEqual(element.attrs['name'], 'arch')
self.assertEqual(element.attrs['type'], 'xml')
self.assertFalse(element.children)
def test_filter_yes_no(self):
""" Test Filter Yes No """
elements = od.filter_yes_no('some_field')
self.assertIsInstance(elements, list)
self.assertFalse(elements)
elements = od.filter_yes_no('some_field', 'Some field')
self.assertEqual(len(elements), 1)
self.assertIsInstance(elements[0], XMLDictElement)
self.assertEqual(elements[0].tag, 'filter')
self.assertEqual(elements[0].attrs['name'], 'some_field_yes')
self.assertEqual(elements[0].attrs['string'], 'Some field')
self.assertEqual(elements[0].attrs['domain'], "[('some_field', '=', True)]")
elements = od.filter_yes_no('some_field', 'Some field', 'Not some field')
self.assertEqual(len(elements), 2)
self.assertIsInstance(elements[0], XMLDictElement)
self.assertEqual(elements[0].tag, 'filter')
self.assertEqual(elements[0].attrs['name'], 'some_field_yes')
self.assertEqual(elements[0].attrs['string'], 'Some field')
self.assertEqual(elements[0].attrs['domain'], "[('some_field', '=', True)]")
self.assertIsInstance(elements[1], XMLDictElement)
self.assertEqual(elements[1].tag, 'filter')
self.assertEqual(elements[1].attrs['name'], 'some_field_no')
self.assertEqual(elements[1].attrs['string'], 'Not some field')
self.assertEqual(elements[1].attrs['domain'], "[('some_field', '=', False)]")
2021-02-22 18:08:11 +01:00
def test_view(self):
""" Test view function """
element = od.view('view_xmlid', [])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'view_xmlid')
self.assertEqual(element.attrs['model'], 'ir.ui.view')
self.assertFalse(element.children)
def test_view_def(self):
""" Test view_def function """
element = od.view_def('view_xmlid', 'View', 'sample.model', [])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'view_xmlid')
self.assertEqual(element.attrs['model'], 'ir.ui.view')
self.assertEqual((len(element.children)), 3)
self.assertIsInstance(element.children[0], XMLDictElement)
self.assertEqual(element.children[0].tag, 'field')
self.assertEqual(element.children[0].attrs['name'], 'name')
self.assertEqual(element.children[0].children, ['View'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[1], XMLDictElement)
self.assertEqual(element.children[1].tag, 'field')
self.assertEqual(element.children[1].attrs['name'], 'model')
self.assertEqual(element.children[1].children, ['sample.model'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[2], XMLDictElement)
self.assertEqual(element.children[2].tag, 'field')
self.assertEqual(element.children[2].attrs['name'], 'arch')
self.assertEqual(element.children[2].attrs['type'], 'xml')
self.assertFalse(element.children[2].children)
def test_view_new(self):
""" Test view_new function """
element = od.view_new('tree', 'sample.model', [])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'sample_model_view_tree')
self.assertEqual(element.attrs['model'], 'ir.ui.view')
self.assertEqual((len(element.children)), 3)
self.assertIsInstance(element.children[0], XMLDictElement)
self.assertEqual(element.children[0].tag, 'field')
self.assertEqual(element.children[0].attrs['name'], 'name')
self.assertEqual(element.children[0].children, ['Sample Model Tree'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[1], XMLDictElement)
self.assertEqual(element.children[1].tag, 'field')
self.assertEqual(element.children[1].attrs['name'], 'model')
self.assertEqual(element.children[1].children, ['sample.model'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[2], XMLDictElement)
self.assertEqual(element.children[2].tag, 'field')
self.assertEqual(element.children[2].attrs['name'], 'arch')
self.assertEqual(element.children[2].attrs['type'], 'xml')
self.assertFalse(element.children[2].children)
def test_view_inherit(self):
""" Test view_inherit function """
element = od.view_inherit('odoo.addons.module', 'sample.model', 'parent.view', [])
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'view_inherit_module')
self.assertEqual(element.attrs['model'], 'ir.ui.view')
self.assertEqual((len(element.children)), 4)
self.assertIsInstance(element.children[0], XMLDictElement)
self.assertEqual(element.children[0].tag, 'field')
self.assertEqual(element.children[0].attrs['name'], 'name')
self.assertEqual(element.children[0].children, ['Sample Model Adaptations'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[1], XMLDictElement)
self.assertEqual(element.children[1].tag, 'field')
self.assertEqual(element.children[1].attrs['name'], 'model')
self.assertEqual(element.children[1].children, ['sample.model'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[2], XMLDictElement)
self.assertEqual(element.children[2].tag, 'field')
self.assertEqual(element.children[2].attrs['name'], 'inherit_id')
self.assertFalse(element.children[2].children)
self.assertIsInstance(element.children[3], XMLDictElement)
self.assertEqual(element.children[3].tag, 'field')
self.assertEqual(element.children[3].attrs['name'], 'arch')
self.assertEqual(element.children[3].attrs['type'], 'xml')
self.assertFalse(element.children[3].children)
def test_action_server_code(self):
""" Test action_server_code function """
element = od.action_server_code('sample.xmlid', 'Code', 'sample.model',
'''record.do_something()''')
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'sample.xmlid')
self.assertEqual(element.attrs['model'], 'ir.actions.server')
self.assertEqual((len(element.children)), 4)
self.assertIsInstance(element.children[0], XMLDictElement)
self.assertEqual(element.children[0].tag, 'field')
self.assertEqual(element.children[0].attrs['name'], 'name')
self.assertEqual(element.children[0].children, ['Code'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[1], XMLDictElement)
self.assertEqual(element.children[1].tag, 'field')
self.assertEqual(element.children[1].attrs['name'], 'model_id')
self.assertEqual(element.children[1].attrs['ref'], 'sample.model')
self.assertFalse(element.children[1].children)
self.assertEqual(element.children[2].tag, 'field')
self.assertEqual(element.children[2].attrs['name'], 'state')
self.assertEqual(element.children[2].children, ['code'])
2021-02-22 18:08:11 +01:00
self.assertEqual(element.children[3].tag, 'field')
self.assertEqual(element.children[3].attrs['name'], 'code')
self.assertEqual(element.children[3].children, ['record.do_something()'])
2021-02-22 18:08:11 +01:00
def test_client_action_multi(self):
""" Test client_action_multi function """
element = od.client_action_multi('sample.xmlid', 'Multi', 'sample.model', 'sample.action')
self.assertIsInstance(element, XMLDictElement)
self.assertEqual(element.tag, 'record')
self.assertEqual(element.attrs['id'], 'sample.xmlid')
self.assertEqual(element.attrs['model'], 'ir.values')
self.assertEqual((len(element.children)), 4)
self.assertIsInstance(element.children[0], XMLDictElement)
self.assertEqual(element.children[0].tag, 'field')
self.assertEqual(element.children[0].attrs['name'], 'name')
self.assertEqual(element.children[0].children, ['Multi'])
2021-02-22 18:08:11 +01:00
self.assertIsInstance(element.children[1], XMLDictElement)
self.assertEqual(element.children[1].tag, 'field')
self.assertEqual(element.children[1].attrs['name'], 'key2')
self.assertEqual(element.children[1].attrs['eval'], "'client_action_multi'")
self.assertFalse(element.children[1].children)
self.assertEqual(element.children[2].tag, 'field')
self.assertEqual(element.children[2].attrs['name'], 'model')
self.assertEqual(element.children[2].attrs['eval'], "'sample.model'")
self.assertEqual(element.children[3].tag, 'field')
self.assertEqual(element.children[3].attrs['name'], 'value')
self.assertEqual(element.children[3].attrs['eval'], "'ir.actions.server,%d'%sample.action")
if __name__ == '__main__':
unittest.main()