[IMP] base: move TestViews assert methods to ViewCase

The goal is to easily re-use these 3 common methods
for unit tests in module implementing new type of views,
such as `web_cohort`

closes odoo/odoo#100021

Related: odoo/enterprise#31239
Signed-off-by: Denis Ledoux (dle) <dle@odoo.com>
This commit is contained in:
Denis Ledoux
2022-09-12 20:03:18 +02:00
parent 01aaca36b7
commit 26b1c3846d
+40 -39
View File
@@ -33,6 +33,46 @@ class ViewCase(common.TransactionCase):
super(ViewCase, self).setUp()
self.View = self.env['ir.ui.view']
def assertValid(self, arch, name='valid view', inherit_id=False):
return self.View.create({
'name': name,
'model': 'ir.ui.view',
'inherit_id': inherit_id,
'arch': arch,
})
def assertInvalid(self, arch, expected_message=None, name='invalid view', inherit_id=False):
with mute_logger('odoo.addons.base.models.ir_ui_view'):
with self.assertRaises(ValidationError) as catcher:
with self.cr.savepoint():
self.View.create({
'name': name,
'model': 'ir.ui.view',
'inherit_id': inherit_id,
'arch': arch,
})
message = str(catcher.exception.args[0])
self.assertEqual(catcher.exception.context['name'], name)
if expected_message:
self.assertIn(expected_message, message)
else:
_logger.warning(message)
def assertWarning(self, arch, expected_message=None, name='invalid view'):
with self.assertLogs('odoo.addons.base.models.ir_ui_view', level="WARNING") as log_catcher:
self.View.create({
'name': name,
'model': 'ir.ui.view',
'arch': arch,
})
self.assertEqual(len(log_catcher.output), 1, "Exactly one warning should be logged")
message = log_catcher.output[0]
self.assertIn('View error context', message)
self.assertIn("'name': '%s'" % name, message)
if expected_message:
self.assertIn(expected_message, message)
class TestNodeLocator(common.TransactionCase):
"""
The node locator returns None when it can not find a node, and the first
@@ -3166,45 +3206,6 @@ class TestViews(ViewCase):
"The view test_views_test_view_ref should not be in the views of the many2many field groups_id"
)
def assertValid(self, arch, name='valid view', inherit_id=False):
return self.View.create({
'name': name,
'model': 'ir.ui.view',
'inherit_id': inherit_id,
'arch': arch,
})
def assertInvalid(self, arch, expected_message=None, name='invalid view', inherit_id=False):
with mute_logger('odoo.addons.base.models.ir_ui_view'):
with self.assertRaises(ValidationError) as catcher:
with self.cr.savepoint():
self.View.create({
'name': name,
'model': 'ir.ui.view',
'inherit_id': inherit_id,
'arch': arch,
})
message = str(catcher.exception.args[0])
self.assertEqual(catcher.exception.context['name'], name)
if expected_message:
self.assertIn(expected_message, message)
else:
_logger.warning(message)
def assertWarning(self, arch, expected_message=None, name='invalid view'):
with self.assertLogs('odoo.addons.base.models.ir_ui_view', level="WARNING") as log_catcher:
self.View.create({
'name': name,
'model': 'ir.ui.view',
'arch': arch,
})
self.assertEqual(len(log_catcher.output), 1, "Exactly one warning should be logged")
message = log_catcher.output[0]
self.assertIn('View error context', message)
self.assertIn("'name': '%s'" % name, message)
if expected_message:
self.assertIn(expected_message, message)
class TestViewTranslations(common.TransactionCase):
# these tests are essentially the same as in test_translate.py, but they use