Fix message when a required field is missing

issue2657
review399002
This commit is contained in:
Pierre-Louis Bonicoli
2012-10-09 11:08:56 +02:00
parent c43e19c3eb
commit 4e4fe87dfa
5 changed files with 64 additions and 2 deletions
+1 -1
View File
@@ -347,7 +347,7 @@ class ModelSQL(ModelStorage):
if field.required and \
not hasattr(field, 'set') and \
field_name not in ('create_uid', 'create_date'):
if not values.get(field_name):
if values.get(field_name) is None:
cls.raise_user_error('required_field',
error_args=cls._get_error_args(
field_name))
+1
View File
@@ -89,6 +89,7 @@ def register():
ModelInheritsSubSubModel,
ModelInheritsSubSubSubModel,
ModelInheritsOverriddenFieldModel,
ModelSQLRequiredField,
MPTT,
ImportDataBoolean,
ImportDataInteger,
+9 -1
View File
@@ -5,7 +5,7 @@ from trytond.model import ModelSingleton, ModelSQL, fields
__all__ = [
'Singleton', 'URLObject', 'ModelInherits', 'ModelInheritsSubModel',
'ModelInheritsSubSubModel', 'ModelInheritsSubSubSubModel',
'ModelInheritsOverriddenFieldModel',
'ModelInheritsOverriddenFieldModel', 'ModelSQLRequiredField'
]
@@ -70,3 +70,11 @@ class ModelInheritsOverriddenFieldModel(ModelSQL):
subfield = fields.Integer('Overridden field')
parent_model = fields.Many2One('test.subsubmodel', 'Parent model',
required=True)
class ModelSQLRequiredField(ModelSQL):
'model with a required field'
__name__ = 'test.modelsql'
integer = fields.Integer(string="integer", required=True)
desc = fields.Char(string="desc", required=True)
+51
View File
@@ -0,0 +1,51 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This file is part of Tryton. The COPYRIGHT file at the top level of this
# repository contains the full copyright notices and license terms.
import unittest
from trytond.config import CONFIG
from trytond.exceptions import UserError
from trytond.transaction import Transaction
from trytond.tests.test_tryton import POOL, DB_NAME, USER, CONTEXT, \
install_module
class ModelSQLTestCase(unittest.TestCase):
'''
Test ModelSQL
'''
def setUp(self):
install_module('test')
self.modelsql = POOL.get('test.modelsql')
# SQLite not concerned because tryton don't set "NOT NULL" constraint:
# 'ALTER TABLE' don't support NOT NULL constraint without default value
@unittest.skipIf(CONFIG['db_type'] not in ('postgresql', 'mysql'),
'postgresql or mysql needed')
def test0010required_field_missing(self):
'''
Test error message when a required field is missing.
'''
fields = {
'desc': '',
'integer': 0,
}
for key, value in fields.iteritems():
with Transaction().start(DB_NAME, USER, context=CONTEXT):
try:
self.modelsql.create({key: value})
except UserError, err:
# message must not quote key
msg = "'%s' not missing but quoted in error: '%s'" % (key,
err.message)
self.assertTrue(key not in err.message, msg)
continue
self.fail('UserError should be caught')
def suite():
return unittest.TestLoader().loadTestsFromTestCase(ModelSQLTestCase)
if __name__ == '__main__':
unittest.TextTestRunner(verbosity=2).run(suite())
+2
View File
@@ -255,6 +255,8 @@ def all_suite():
suite_.addTests(test_wizard.suite())
import trytond.tests.test_model_inherits as test_inherits
suite_.addTests(test_inherits.suite())
import trytond.tests.test_modelsql as test_modelsql
suite_.addTests(test_modelsql.suite())
return suite_