[IMP] tests: improve code of Form

Move its code to file odoo/tests/form.py and make the code more
readable.

Part-of: odoo/odoo#116779
This commit is contained in:
Raphael Collet
2023-03-31 17:02:43 +02:00
parent 024fadfd0e
commit 91b3813550
6 changed files with 1167 additions and 1095 deletions
+11 -10
View File
@@ -80,23 +80,24 @@ class TestSequenceMixin(TestSequenceMixinCommon):
new_journal.code = "MISC2"
copy2.journal_id = new_journal
self.assertEqual(copy2.name, 'MISC2/2016/01/0001')
with Form(copy2) as move_form: # It is editable in the form
with mute_logger('odoo.tests.common.onchange'):
with self.assertLogs('odoo.tests.form') as cm:
move_form.name = 'MyMISC/2016/0001'
self.assertIn(
'The sequence will restart at 1 at the start of every year',
move_form._perform_onchange(['name'])['warning']['message'],
)
self.assertTrue(cm.output[0].startswith('WARNING:odoo.tests.form.onchange:'))
self.assertIn('The sequence will restart at 1 at the start of every year', cm.output[0])
move_form.journal_id = self.test_move.journal_id
self.assertEqual(move_form.name, '/')
move_form.journal_id = new_journal
self.assertEqual(move_form.name, 'MISC2/2016/01/0001')
with mute_logger('odoo.tests.common.onchange'):
with self.assertLogs('odoo.tests.form') as cm:
move_form.name = 'MyMISC/2016/0001'
self.assertIn(
'The sequence will restart at 1 at the start of every year',
move_form._perform_onchange(['name'])['warning']['message'],
)
self.assertTrue(cm.output[0].startswith('WARNING:odoo.tests.form.onchange:'))
self.assertIn('The sequence will restart at 1 at the start of every year', cm.output[0])
copy2.action_post()
self.assertEqual(copy2.name, 'MyMISC/2016/0001')
+16 -1
View File
@@ -194,7 +194,22 @@
<field name="arch" type="xml">
<field name="subs" position="inside">
<form>
<field name="value" attrs="{'readonly': [('v', '!=', False)]}"/>
<field name="value" attrs="{'readonly': [('v', '=', 42)]}"/>
<field name="v"/>
</form>
</field>
</field>
</record>
<record id="o2m_modifier_parent" model="ir.ui.view">
<field name="name">Modifier check</field>
<field name="model">test_testing_utilities.parent</field>
<field name="inherit_id" ref="o2m_parent"/>
<field name="mode">primary</field>
<field name="arch" type="xml">
<field name="subs" position="inside">
<form>
<field name="value" attrs="{'readonly': [('parent.value', '=', 42)]}"/>
<field name="v"/>
</form>
</field>
@@ -9,7 +9,7 @@ business objects.
from lxml import etree
from operator import itemgetter
from odoo.tests.common import TransactionCase, Form
from odoo.tests import TransactionCase, Form
from odoo import Command
@@ -35,11 +35,16 @@ class TestBasic(TransactionCase):
self.assertEqual(f.f3, 4)
self.assertEqual(f.f4, 2)
# f.record cannot be accessed yet
with self.assertRaises(AssertionError):
f.record
r = f.save()
self.assertEqual(
(r.f1, r.f2, r.f3, r.f4),
('4', 8, 4, 2),
)
self.assertEqual(f.record, r)
def test_required(self):
f = Form(self.env['test_testing_utilities.a'])
@@ -161,54 +166,45 @@ class TestM2O(TransactionCase):
class TestM2M(TransactionCase):
def test_add(self):
Sub = self.env['test_testing_utilities.sub2']
f = Form(self.env['test_testing_utilities.e'])
r1 = Sub.create({'name': "Item"})
r2 = Sub.create({'name': "Item2"})
f.m2m.add(r1)
f.m2m.add(r2)
r = f.save()
with Form(self.env['test_testing_utilities.e']) as f:
f.m2m.add(r1)
f.m2m.add(r2)
self.assertEqual(
r.m2m,
f.record.m2m,
r1 | r2
)
def test_remove_by_index(self):
Sub = self.env['test_testing_utilities.sub2']
f = Form(self.env['test_testing_utilities.e'])
r1 = Sub.create({'name': "Item"})
r2 = Sub.create({'name': "Item2"})
f.m2m.add(r1)
f.m2m.add(r2)
f.m2m.remove(index=0)
r = f.save()
with Form(self.env['test_testing_utilities.e']) as f:
f.m2m.add(r1)
f.m2m.add(r2)
f.m2m.remove(index=0)
self.assertEqual(
r.m2m,
f.record.m2m,
r2
)
def test_remove_by_id(self):
Sub = self.env['test_testing_utilities.sub2']
f = Form(self.env['test_testing_utilities.e'])
r1 = Sub.create({'name': "Item"})
r2 = Sub.create({'name': "Item2"})
f.m2m.add(r1)
f.m2m.add(r2)
f.m2m.remove(id=r1.id)
r = f.save()
with Form(self.env['test_testing_utilities.e']) as f:
f.m2m.add(r1)
f.m2m.add(r2)
f.m2m.remove(id=r1.id)
self.assertEqual(
r.m2m,
f.record.m2m,
r2
)
@@ -284,14 +280,13 @@ class TestO2M(TransactionCase):
""" Tests that the o2m proxy allows adding, removing and editing o2m
records
"""
f = Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_parent')
with Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_parent') as f:
f.subs.new().save()
f.subs.new().save()
f.subs.new().save()
f.subs.remove(index=0)
f.subs.new().save()
f.subs.new().save()
f.subs.new().save()
f.subs.remove(index=0)
r = f.save()
r = f.record
self.assertEqual(
[get(s) for s in r.subs],
@@ -358,12 +353,11 @@ class TestO2M(TransactionCase):
""" Tests the o2m proxy when the list and form views are provided
inline rather than fetched separately
"""
f = Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_parent_inline')
with Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_parent_inline') as f:
with f.subs.new() as s:
s.value = 42
with f.subs.new() as s:
s.value = 42
r = f.save()
r = f.record
self.assertEqual(
[get(s) for s in r.subs],
@@ -374,13 +368,12 @@ class TestO2M(TransactionCase):
def test_o2m_default(self):
""" Tests that default_get can return defaults for the o2m
"""
f = Form(self.env['test_testing_utilities.default'])
with Form(self.env['test_testing_utilities.default']) as f:
with f.subs.edit(index=0) as s:
self.assertEqual(s.v, 5)
self.assertEqual(s.value, 2)
with f.subs.edit(index=0) as s:
self.assertEqual(s.v, 5)
self.assertEqual(s.value, 2)
r = f.save()
r = f.record
self.assertEqual(
[get(s) for s in r.subs],
@@ -390,11 +383,10 @@ class TestO2M(TransactionCase):
def test_o2m_inner_default(self):
""" Tests that creating an o2m record will get defaults for it
"""
f = Form(self.env['test_testing_utilities.default'])
with f.subs.new() as s:
self.assertEqual(s.value, 2)
self.assertEqual(s.v, 2, "should have onchanged value to v")
with Form(self.env['test_testing_utilities.default']) as f:
with f.subs.new() as s:
self.assertEqual(s.value, 2)
self.assertEqual(s.v, 2, "should have onchanged value to v")
def test_o2m_onchange_parent(self):
""" Tests that changing o2m content triggers onchange in the parent
@@ -454,11 +446,11 @@ class TestO2M(TransactionCase):
""" Tests that readonly is applied to the field of the o2m = not sent
as part of the create / write values
"""
f = Form(self.env['o2m_readonly_subfield_parent'])
with f.line_ids.new() as new_line:
new_line.name = "ok"
self.assertEqual(new_line.f, 2)
r = f.save()
with Form(self.env['o2m_readonly_subfield_parent']) as f:
with f.line_ids.new() as new_line:
new_line.name = "ok"
self.assertEqual(new_line.f, 2)
r = f.record
self.assertEqual(
(r.line_ids.name, r.line_ids.f),
('ok', 2)
@@ -477,22 +469,22 @@ class TestO2M(TransactionCase):
def test_o2m_remove(self):
def commands():
return [c[0] for c in f._values['line_ids']]
f = Form(self.env['test_testing_utilities.onchange_count'])
self.assertEqual(f.count, 0)
self.assertEqual(len(f.line_ids), 0)
with Form(self.env['test_testing_utilities.onchange_count']) as f:
self.assertEqual(f.count, 0)
self.assertEqual(len(f.line_ids), 0)
f.count = 5
self.assertEqual(f.count, 5)
self.assertEqual(len(f.line_ids), 5)
f.count = 5
self.assertEqual(f.count, 5)
self.assertEqual(len(f.line_ids), 5)
f.count = 2
self.assertEqual(f.count, 2)
self.assertEqual(len(f.line_ids), 2)
f.count = 2
self.assertEqual(f.count, 2)
self.assertEqual(len(f.line_ids), 2)
f.count = 4
f.count = 4
r = f.save()
r = f.record
previous = r.line_ids
self.assertEqual(len(previous), 4)
@@ -513,25 +505,36 @@ class TestO2M(TransactionCase):
Form(self.env['test_testing_utilities.recursive'], view='test_testing_utilities.o2m_recursive_relation_view')
def test_o2m_attrs(self):
Model = self.env['test_testing_utilities.parent'].with_context(
default_subs=[{
'value': 5,
}, {
'value': 7,
}]
)
f = Form(Model, view='test_testing_utilities.o2m_modifier')
f.save()
Model = self.env['test_testing_utilities.parent']
with Form(Model, view='test_testing_utilities.o2m_modifier') as form:
with form.subs.new() as line:
line.value = 5
# this makes 'value' readonly
line.v = 42
with self.assertRaises(AssertionError):
line.value = 7
def test_o2m_attrs_parent(self):
Model = self.env['test_testing_utilities.parent']
with Form(Model, view='test_testing_utilities.o2m_modifier_parent') as form:
with form.subs.new() as line:
line.value = 5
# this makes 'value' readonly on lines
form.value = 42
with form.subs.new() as line:
with self.assertRaises(AssertionError):
line.value = 7
def test_o2m_widget(self):
create = self.env['test_testing_utilities.sub'].create
a, b, c = create({'v': 1}), create({'v': 2}), create({'v': 3})
f = Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_widget_m2m')
f.subs.add(a)
f.subs.add(b)
f.subs.add(c)
r = f.save()
with Form(self.env['test_testing_utilities.parent'], view='test_testing_utilities.o2m_widget_m2m') as f:
f.subs.add(a)
f.subs.add(b)
f.subs.add(c)
r = f.record
self.assertEqual(
r.subs,
@@ -545,14 +548,14 @@ class TestO2M(TransactionCase):
"""
# create record: line created before v is updated should reflect it,
# line created after should not
f = Form(self.env['o2m_changes_children'])
with f.line_ids.new() as line:
line.v = 1
line.vv = 5
f.v = 5
with f.line_ids.new() as line:
...
r = f.save()
with Form(self.env['o2m_changes_children']) as f:
with f.line_ids.new() as line:
line.v = 1
line.vv = 5
f.v = 5
with f.line_ids.new() as line:
...
r = f.record
self.assertEqual(r.v, 5)
self.assertEqual(r.mapped('line_ids.vv'), [5, 0])
self.assertEqual(r.line_ids[0].v, 5, "onchange should have updated the existing lines")
+1
View File
@@ -7,4 +7,5 @@ See https://github.com/python/cpython/tree/3.10/Lib/unittest for reference files
from . import common
from .common import *
from .form import Form, O2MProxy, M2MProxy
from . import test_parse_inline_template
+18 -999
View File
File diff suppressed because it is too large Load Diff
+1033
View File
File diff suppressed because it is too large Load Diff