changeset e034ceb4a736 in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset;node=e034ceb4a736
description:
        Add test on target of relation fields

        issue9132
        review291211004
diffstat:

 CHANGELOG                         |   1 +
 doc/ref/models/fields.rst         |   8 +++++++
 trytond/model/fields/many2many.py |  13 +++++++----
 trytond/model/fields/one2many.py  |   7 ++---
 trytond/tests/test_tryton.py      |  43 +++++++++++++++++++++++++++++++++++++++
 5 files changed, 63 insertions(+), 9 deletions(-)

diffs (156 lines):

diff -r c93004ca174a -r e034ceb4a736 CHANGELOG
--- a/CHANGELOG Wed Apr 01 23:58:49 2020 +0200
+++ b/CHANGELOG Thu Apr 02 00:01:14 2020 +0200
@@ -1,3 +1,4 @@
+* Add test on target of relation fields
 * Add width and height attributes on calendar view
 * Add default database name configuration
 * Add retry option to report convert
diff -r c93004ca174a -r e034ceb4a736 doc/ref/models/fields.rst
--- a/doc/ref/models/fields.rst Wed Apr 01 23:58:49 2020 +0200
+++ b/doc/ref/models/fields.rst Thu Apr 02 00:01:14 2020 +0200
@@ -817,6 +817,10 @@
 
 Instance methods:
 
+.. method:: Many2Many.get_relation()
+
+    Return the relation :class:`~trytond.model.Model`.
+
 .. method:: Many2Many.get_target()
 
     Return the target :class:`~trytond.model.Model`.
@@ -851,6 +855,10 @@
 
 Instance methods:
 
+.. method:: Many2Many.get_target()
+
+    Return the target :class:`~trytond.model.Model`.
+
 .. method:: One2One.get_target()
 
     Return the target :class:`~trytond.model.Model`.
diff -r c93004ca174a -r e034ceb4a736 trytond/model/fields/many2many.py
--- a/trytond/model/fields/many2many.py Wed Apr 01 23:58:49 2020 +0200
+++ b/trytond/model/fields/many2many.py Thu Apr 02 00:01:14 2020 +0200
@@ -125,7 +125,7 @@
         else:
             order = self.order
 
-        Relation = Pool().get(self.relation_name)
+        Relation = self.get_relation()
         origin_field = Relation._fields[self.origin]
 
         relations = []
@@ -158,8 +158,7 @@
             (``add``, ``<ids>``),
             (``copy``, ``<ids>``, ``[{<field name>: value}, ...]``)
         '''
-        pool = Pool()
-        Relation = pool.get(self.relation_name)
+        Relation = self.get_relation()
         Target = self.get_target()
         origin_field = Relation._fields[self.origin]
         relation_to_create = []
@@ -269,9 +268,13 @@
         if relation_to_create:
             Relation.create(relation_to_create)
 
+    def get_relation(self):
+        "Return the relation model"
+        return Pool().get(self.relation_name)
+
     def get_target(self):
         'Return the target model'
-        Relation = Pool().get(self.relation_name)
+        Relation = self.get_relation()
         if not self.target:
             return Relation
         return Relation._fields[self.target].get_target()
@@ -324,7 +327,7 @@
         pool = Pool()
         Rule = pool.get('ir.rule')
         Target = self.get_target()
-        Relation = pool.get(self.relation_name)
+        Relation = self.get_relation()
         transaction = Transaction()
         table, _ = tables[None]
         name, operator, value = domain[:3]
diff -r c93004ca174a -r e034ceb4a736 trytond/model/fields/one2many.py
--- a/trytond/model/fields/one2many.py  Wed Apr 01 23:58:49 2020 +0200
+++ b/trytond/model/fields/one2many.py  Thu Apr 02 00:01:14 2020 +0200
@@ -119,9 +119,8 @@
         '''
         Return target records ordered.
         '''
-        pool = Pool()
-        Relation = pool.get(self.model_name)
-        field = Relation._fields[self.field]
+        Target = self.get_target()
+        field = Target._fields[self.field]
         res = {}
         for i in ids:
             res[i] = []
@@ -135,7 +134,7 @@
                 clause = [(self.field, 'in', list(sub_ids))]
             if self.filter:
                 clause.append(self.filter)
-            targets.append(Relation.search(clause, order=self.order))
+            targets.append(Target.search(clause, order=self.order))
         targets = list(chain(*targets))
 
         for target in targets:
diff -r c93004ca174a -r e034ceb4a736 trytond/tests/test_tryton.py
--- a/trytond/tests/test_tryton.py      Wed Apr 01 23:58:49 2020 +0200
+++ b/trytond/tests/test_tryton.py      Thu Apr 02 00:01:14 2020 +0200
@@ -440,6 +440,49 @@
                         getattr(record, attr)()
 
     @with_transaction()
+    def test_field_relation_target(self):
+        "Test field relation and target"
+        pool = Pool()
+        for mname, model in pool.iterobject():
+            if not isregisteredby(model, self.module):
+                continue
+            for fname, field in model._fields.items():
+                if isinstance(field, fields.One2Many):
+                    Relation = field.get_target()
+                    rfield = field.field
+                elif isinstance(field, fields.Many2Many):
+                    Relation = field.get_relation()
+                    rfield = field.origin
+                else:
+                    continue
+                if rfield:
+                    self.assertIn(rfield, Relation._fields,
+                        msg=('Missing relation field "%s" on "%s" '
+                            'for "%s"."%s"') % (
+                            rfield, Relation.__name__, mname, fname))
+                    reverse_field = Relation._fields[rfield]
+                    self.assertIn(
+                        reverse_field._type, [
+                            'reference', 'many2one', 'one2one'],
+                        msg=('Wrong type for relation field "%s" on "%s" '
+                            'for "%s"."%s"') % (
+                            rfield, Relation.__name__, mname, fname))
+                    if (reverse_field._type == 'many2one'
+                            and issubclass(model, ModelSQL)
+                            # Do not test table_query models
+                            # as they can manipulate their id
+                            and not callable(model.table_query)):
+                        self.assertEqual(
+                            reverse_field.model_name, model.__name__,
+                            msg=('Wrong model for relation field "%s" on "%s" '
+                                'for "%s"."%s"') % (
+                                rfield, Relation.__name__, mname, fname))
+                Target = field.get_target()
+                self.assertTrue(
+                    Target,
+                    msg='Missing target for "%s"."%s"' % (mname, fname))
+
+    @with_transaction()
     def test_menu_action(self):
         'Test that menu actions are accessible to menu\'s group'
         pool = Pool()

Reply via email to