Raise user error when import data with wrong number of columns
This commit is contained in:
1 parent
57e59ac67d
commit
db97bdca70
4 files changed
+34
-6
No files matched your search
@@ -151,6 +151,12 @@ this repository contains the full copyright notices and license terms. -->
|
||||
<record model="ir.message" id="msg_base_config_record">
|
||||
<field name="text">This record is part of the base configuration.</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_import_data_missing_column">
|
||||
<field name="text">Missing values for last column.</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_import_data_missing_columns">
|
||||
<field name="text">Missing values for %(n)s last columns.</field>
|
||||
</record>
|
||||
<record model="ir.message" id="msg_relation_not_found">
|
||||
<field name="text">Relation not found: %(value)r in "%(model)s" (%(column)s).</field>
|
||||
</record>
|
||||
|
||||
@@ -1050,12 +1050,16 @@ class ModelStorage(Model):
|
||||
todo = set()
|
||||
prefix_len = len(prefix)
|
||||
many2many = set()
|
||||
if len(fields_names) > len(line):
|
||||
n = len(fields_names) - len(line)
|
||||
if n > 1:
|
||||
raise ImportDataError(gettext(
|
||||
'ir.msg_import_data_missing_columns', n=n))
|
||||
else:
|
||||
raise ImportDataError(gettext(
|
||||
'ir.msg_import_data_missing_column'))
|
||||
# Import normal fields_names
|
||||
for i, field in enumerate(fields_names):
|
||||
if i >= len(line):
|
||||
raise Exception('ImportError',
|
||||
'Please check that all your lines have %d cols.'
|
||||
% len(fields_names))
|
||||
is_prefix_len = (len(field) == (prefix_len + 1))
|
||||
value = line[i]
|
||||
column = '/'.join(field)
|
||||
@@ -1155,8 +1159,6 @@ class ModelStorage(Model):
|
||||
|
||||
ModelData = pool.get('ir.model.data')
|
||||
|
||||
len_fields_names = len(fields_names)
|
||||
assert all(len(x) == len_fields_names for x in data)
|
||||
fields_names = [x.split('/') for x in fields_names]
|
||||
fields_def = cls.fields_get()
|
||||
|
||||
|
||||
@@ -5,6 +5,13 @@ from trytond.model import ModelSQL, fields
|
||||
from trytond.pool import Pool
|
||||
|
||||
|
||||
class ImportData(ModelSQL):
|
||||
"Import Data"
|
||||
__name__ = 'test.import_data'
|
||||
name = fields.Char("Name")
|
||||
value = fields.Integer("Value")
|
||||
|
||||
|
||||
class ImportDataBoolean(ModelSQL):
|
||||
"Import Data Boolean"
|
||||
__name__ = 'test.import_data.boolean'
|
||||
@@ -176,6 +183,7 @@ class ImportDataUpdate(ModelSQL):
|
||||
|
||||
def register(module):
|
||||
Pool.register(
|
||||
ImportData,
|
||||
ImportDataBoolean,
|
||||
ImportDataInteger,
|
||||
ImportDataFloat,
|
||||
|
||||
@@ -18,6 +18,18 @@ class ImportDataTestCase(unittest.TestCase):
|
||||
def setUpClass(cls):
|
||||
activate_module('tests')
|
||||
|
||||
@with_transaction()
|
||||
def test_missing_values(self):
|
||||
"Test error is raised when missing values"
|
||||
pool = Pool()
|
||||
Model = pool.get('test.import_data')
|
||||
|
||||
with self.assertRaisesRegex(ImportDataError, "for last column"):
|
||||
Model.import_data(['name'], [["Test"], []])
|
||||
|
||||
with self.assertRaisesRegex(ImportDataError, "for 2 last column"):
|
||||
Model.import_data(['name', 'value'], [["Test", 1], []])
|
||||
|
||||
@with_transaction()
|
||||
def test_boolean(self):
|
||||
'Test boolean'
|
||||
|
||||
Reference in new issue
Block a user