changeset 33738efe709c in trytond:default
details: https://hg.tryton.org/trytond?cmd=changeset&node=33738efe709c
description:
        Support PYSON size for Char field

        issue11315
        review390241005
diffstat:

 doc/ref/models/fields.rst        |   1 +
 trytond/model/fields/char.py     |   5 ++++-
 trytond/tests/field_char.py      |  11 +++++++++++
 trytond/tests/test_field_char.py |  25 ++++++++++++++++++++++++-
 4 files changed, 40 insertions(+), 2 deletions(-)

diffs (103 lines):

diff -r 1e4006f86e3f -r 33738efe709c doc/ref/models/fields.rst
--- a/doc/ref/models/fields.rst Thu Mar 24 09:59:09 2022 +0000
+++ b/doc/ref/models/fields.rst Sat Mar 26 12:00:33 2022 +0100
@@ -288,6 +288,7 @@
 
     The maximum length (in characters) of the field. The size is enforced at
     the storage level and in the client input.
+    The value can be a :class:`~trytond.pyson.PYSON` statement.
 
 .. attribute:: Char.translate
 
diff -r 1e4006f86e3f -r 33738efe709c trytond/model/fields/char.py
--- a/trytond/model/fields/char.py      Thu Mar 24 09:59:09 2022 +0000
+++ b/trytond/model/fields/char.py      Sat Mar 26 12:00:33 2022 +0100
@@ -57,7 +57,10 @@
 
     @property
     def _sql_type(self):
-        return 'VARCHAR(%s)' % self.size if self.size else 'VARCHAR'
+        if isinstance(self.size, int):
+            return 'VARCHAR(%s)' % self.size
+        else:
+            return 'VARCHAR'
 
     def set_rpc(self, model):
         super(Char, self).set_rpc(model)
diff -r 1e4006f86e3f -r 33738efe709c trytond/tests/field_char.py
--- a/trytond/tests/field_char.py       Thu Mar 24 09:59:09 2022 +0000
+++ b/trytond/tests/field_char.py       Sat Mar 26 12:00:33 2022 +0100
@@ -3,6 +3,7 @@
 
 from trytond.model import ModelSQL, fields
 from trytond.pool import Pool
+from trytond.pyson import Eval
 
 
 class Char(ModelSQL):
@@ -33,6 +34,15 @@
     char = fields.Char("Char", size=5)
 
 
+class CharSizePYSON(ModelSQL):
+    "Char PYSON Size"
+    __name__ = 'test.char_size_pyson'
+    char = fields.Char(
+        "Char", size=Eval('size', 0),
+        depends=['size'])
+    size = fields.Integer("Size")
+
+
 class CharTranslate(ModelSQL):
     'Char Translate'
     __name__ = 'test.char_translate'
@@ -68,6 +78,7 @@
         CharDefault,
         CharRequired,
         CharSize,
+        CharSizePYSON,
         CharTranslate,
         CharUnaccentedOn,
         CharUnaccentedOff,
diff -r 1e4006f86e3f -r 33738efe709c trytond/tests/test_field_char.py
--- a/trytond/tests/test_field_char.py  Thu Mar 24 09:59:09 2022 +0000
+++ b/trytond/tests/test_field_char.py  Sat Mar 26 12:00:33 2022 +0100
@@ -7,7 +7,7 @@
 
 from trytond import backend
 from trytond.model.exceptions import (
-    ForbiddenCharValidationError, RequiredValidationError)
+    ForbiddenCharValidationError, RequiredValidationError, SizeValidationError)
 from trytond.pool import Pool
 from trytond.tests.test_tryton import (
     ExtensionTestCase, activate_module, with_transaction)
@@ -423,6 +423,29 @@
                         }])
 
     @with_transaction()
+    def test_create_size_pyson_valid(self):
+        "Test create char with PYSON size"
+        Char = Pool().get('test.char_size_pyson')
+
+        char, = Char.create([{
+                    'char': "Test",
+                    'size': 5,
+                    }])
+
+        self.assertEqual(char.char, "Test")
+
+    @with_transaction()
+    def test_create_size_pyson_invalid(self):
+        "Test create char with invalid PYSON size"
+        Char = Pool().get('test.char_size_pyson')
+
+        with self.assertRaises(SizeValidationError):
+            Char.create([{
+                        'char': "foobar",
+                        'size': 5,
+                        }])
+
+    @with_transaction()
     def test_create_invalid_char(self):
         "Test create char with invalid char"
         Char = Pool().get('test.char')

Reply via email to