[IMP] translation tests, translated acceptable values for boolean fields
bzr revid: xmo@openerp.com-20120920150443-l9lna4bnkta7n2o8
This commit is contained in:
parent
fdba99aaeb
commit
a9bc82c46d
|
@ -1,6 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
import functools
|
import functools
|
||||||
import operator
|
import operator
|
||||||
|
import itertools
|
||||||
import warnings
|
import warnings
|
||||||
from openerp.osv import orm, fields
|
from openerp.osv import orm, fields
|
||||||
from openerp.tools.translate import _
|
from openerp.tools.translate import _
|
||||||
|
@ -55,7 +56,29 @@ class ir_fields_converter(orm.Model):
|
||||||
converter, cr, uid, model, column, context=context)
|
converter, cr, uid, model, column, context=context)
|
||||||
|
|
||||||
def _str_to_boolean(self, cr, uid, model, column, value, context=None):
|
def _str_to_boolean(self, cr, uid, model, column, value, context=None):
|
||||||
return value.lower() not in ('', '0', 'false', 'off')
|
# all translatables used for booleans
|
||||||
|
true, yes, false, no = _(u"true"), _(u"yes"), _(u"false"), _(u"no")
|
||||||
|
# potentially broken casefolding? What about locales?
|
||||||
|
trues = set(word.lower() for word in itertools.chain(
|
||||||
|
[u'1', u"true", u"yes"], # don't use potentially translated values
|
||||||
|
self._get_translations(cr, uid, ['code'], u"true", context=context),
|
||||||
|
self._get_translations(cr, uid, ['code'], u"yes", context=context),
|
||||||
|
))
|
||||||
|
if value.lower() in trues: return True
|
||||||
|
|
||||||
|
# potentially broken casefolding? What about locales?
|
||||||
|
falses = set(word.lower() for word in itertools.chain(
|
||||||
|
[u'', u"0", u"false", u"no"],
|
||||||
|
self._get_translations(cr, uid, ['code'], u"false", context=context),
|
||||||
|
self._get_translations(cr, uid, ['code'], u"no", context=context),
|
||||||
|
))
|
||||||
|
if value.lower() in falses: return False
|
||||||
|
|
||||||
|
warnings.warn(
|
||||||
|
_(u"Unknown value '%s' for boolean field '%%(field)s', assuming '%s'")
|
||||||
|
% (value, yes),
|
||||||
|
orm.ImportWarning)
|
||||||
|
return True
|
||||||
|
|
||||||
def _str_to_integer(self, cr, uid, model, column, value, context=None):
|
def _str_to_integer(self, cr, uid, model, column, value, context=None):
|
||||||
if not value: return False
|
if not value: return False
|
||||||
|
@ -86,7 +109,7 @@ class ir_fields_converter(orm.Model):
|
||||||
selection = selection(model, cr, uid)
|
selection = selection(model, cr, uid)
|
||||||
for item, label in selection:
|
for item, label in selection:
|
||||||
labels = self._get_translations(
|
labels = self._get_translations(
|
||||||
cr, uid, ('selection', 'model'), label, context=context)
|
cr, uid, ('selection', 'model', 'code'), label, context=context)
|
||||||
labels.append(label)
|
labels.append(label)
|
||||||
if value == unicode(item) or value in labels:
|
if value == unicode(item) or value in labels:
|
||||||
return item
|
return item
|
||||||
|
|
|
@ -79,6 +79,27 @@ class ImporterCase(common.TransactionCase):
|
||||||
})
|
})
|
||||||
return '__test__.' + name
|
return '__test__.' + name
|
||||||
|
|
||||||
|
def add_translations(self, name, type, code, *tnx):
|
||||||
|
Lang = self.registry('res.lang')
|
||||||
|
if not Lang.search(self.cr, openerp.SUPERUSER_ID, [('code', '=', code)]):
|
||||||
|
Lang.create(self.cr, openerp.SUPERUSER_ID, {
|
||||||
|
'name': code,
|
||||||
|
'code': code,
|
||||||
|
'translatable': True,
|
||||||
|
'date_format': '%d.%m.%Y',
|
||||||
|
'decimal_point': ',',
|
||||||
|
})
|
||||||
|
Translations = self.registry('ir.translation')
|
||||||
|
for source, value in tnx:
|
||||||
|
Translations.create(self.cr, openerp.SUPERUSER_ID, {
|
||||||
|
'name': name,
|
||||||
|
'lang': code,
|
||||||
|
'type': type,
|
||||||
|
'src': source,
|
||||||
|
'value': value,
|
||||||
|
'state': 'translated',
|
||||||
|
})
|
||||||
|
|
||||||
class test_ids_stuff(ImporterCase):
|
class test_ids_stuff(ImporterCase):
|
||||||
model_name = 'export.integer'
|
model_name = 'export.integer'
|
||||||
|
|
||||||
|
@ -139,41 +160,39 @@ class test_boolean_field(ImporterCase):
|
||||||
], values(records))
|
], values(records))
|
||||||
|
|
||||||
def test_falses(self):
|
def test_falses(self):
|
||||||
ids, messages = self.import_(
|
for lang, source, value in [('fr_FR', 'no', u'non'),
|
||||||
['value'],
|
('de_DE', 'no', u'nein'),
|
||||||
[[u'0'], [u'off'],
|
('ru_RU', 'no', u'нет'),
|
||||||
[u'false'], [u'FALSE'],
|
('nl_BE', 'false', u'vals'),
|
||||||
[u'OFF'], [u''],
|
('lt_LT', 'false', u'klaidingas')]:
|
||||||
])
|
self.add_translations('test_import.py', 'code', lang, (source, value))
|
||||||
self.assertEqual(len(ids), 6)
|
falses = [[u'0'], [u'no'], [u'false'], [u'FALSE'], [u''],
|
||||||
|
[u'non'], # no, fr
|
||||||
|
[u'nein'], # no, de
|
||||||
|
[u'нет'], # no, ru
|
||||||
|
[u'vals'], # false, nl
|
||||||
|
[u'klaidingas'], # false, lt,
|
||||||
|
]
|
||||||
|
|
||||||
|
ids, messages = self.import_(['value'], falses)
|
||||||
self.assertFalse(messages)
|
self.assertFalse(messages)
|
||||||
self.assertEqual([
|
self.assertEqual(len(ids), len(falses))
|
||||||
False,
|
self.assertEqual([False] * len(falses), values(self.read()))
|
||||||
False,
|
|
||||||
False,
|
|
||||||
False,
|
|
||||||
False,
|
|
||||||
False,
|
|
||||||
],
|
|
||||||
values(self.read()))
|
|
||||||
|
|
||||||
def test_trues(self):
|
def test_trues(self):
|
||||||
ids, messages = self.import_(
|
trues = [['None'], ['nil'], ['()'], ['f'], ['#f'],
|
||||||
['value'],
|
|
||||||
[['no'],
|
|
||||||
['None'],
|
|
||||||
['nil'],
|
|
||||||
['()'],
|
|
||||||
['f'],
|
|
||||||
['#f'],
|
|
||||||
# Problem: OpenOffice (and probably excel) output localized booleans
|
# Problem: OpenOffice (and probably excel) output localized booleans
|
||||||
['VRAI'],
|
['VRAI'], ['ok'], ['true'], ['yes'], ['1'], ]
|
||||||
|
ids, messages = self.import_(['value'], trues)
|
||||||
|
self.assertEqual(len(ids), 10)
|
||||||
|
self.assertEqual(messages, [
|
||||||
|
message(u"Unknown value '%s' for boolean field 'value', assuming 'yes'" % v[0],
|
||||||
|
type='warning', from_=i, to_=i, record=i)
|
||||||
|
for i, v in enumerate(trues)
|
||||||
|
if v[0] != 'true' if v[0] != 'yes' if v[0] != '1'
|
||||||
])
|
])
|
||||||
self.assertEqual(len(ids), 7)
|
|
||||||
# FIXME: should warn for values which are not "true", "yes" or "1"
|
|
||||||
self.assertFalse(messages)
|
|
||||||
self.assertEqual(
|
self.assertEqual(
|
||||||
[True] * 7,
|
[True] * 10,
|
||||||
values(self.read()))
|
values(self.read()))
|
||||||
|
|
||||||
class test_integer_field(ImporterCase):
|
class test_integer_field(ImporterCase):
|
||||||
|
@ -399,9 +418,9 @@ class test_text(ImporterCase):
|
||||||
class test_selection(ImporterCase):
|
class test_selection(ImporterCase):
|
||||||
model_name = 'export.selection'
|
model_name = 'export.selection'
|
||||||
translations_fr = [
|
translations_fr = [
|
||||||
("Qux", "toto"),
|
|
||||||
("Bar", "titi"),
|
|
||||||
("Foo", "tete"),
|
("Foo", "tete"),
|
||||||
|
("Bar", "titi"),
|
||||||
|
("Qux", "toto"),
|
||||||
]
|
]
|
||||||
|
|
||||||
def test_imported(self):
|
def test_imported(self):
|
||||||
|
@ -416,23 +435,8 @@ class test_selection(ImporterCase):
|
||||||
self.assertEqual([3, 2, 1, 2], values(self.read()))
|
self.assertEqual([3, 2, 1, 2], values(self.read()))
|
||||||
|
|
||||||
def test_imported_translated(self):
|
def test_imported_translated(self):
|
||||||
self.registry('res.lang').create(self.cr, openerp.SUPERUSER_ID, {
|
self.add_translations(
|
||||||
'name': u'Français',
|
'export.selection,value', 'selection', 'fr_FR', *self.translations_fr)
|
||||||
'code': 'fr_FR',
|
|
||||||
'translatable': True,
|
|
||||||
'date_format': '%d.%m.%Y',
|
|
||||||
'decimal_point': ',',
|
|
||||||
'thousand_sep': ' ',
|
|
||||||
})
|
|
||||||
Translations = self.registry('ir.translation')
|
|
||||||
for source, value in self.translations_fr:
|
|
||||||
Translations.create(self.cr, openerp.SUPERUSER_ID, {
|
|
||||||
'name': 'export.selection,value',
|
|
||||||
'lang': 'fr_FR',
|
|
||||||
'type': 'selection',
|
|
||||||
'src': source,
|
|
||||||
'value': value
|
|
||||||
})
|
|
||||||
|
|
||||||
ids, messages = self.import_(['value'], [
|
ids, messages = self.import_(['value'], [
|
||||||
['toto'],
|
['toto'],
|
||||||
|
@ -474,7 +478,7 @@ class test_selection_function(ImporterCase):
|
||||||
translations_fr = [
|
translations_fr = [
|
||||||
("Corge", "toto"),
|
("Corge", "toto"),
|
||||||
("Grault", "titi"),
|
("Grault", "titi"),
|
||||||
("Whee", "tete"),
|
("Wheee", "tete"),
|
||||||
("Moog", "tutu"),
|
("Moog", "tutu"),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -483,7 +487,7 @@ class test_selection_function(ImporterCase):
|
||||||
be good news) *and* serializes the selection function to reverse it:
|
be good news) *and* serializes the selection function to reverse it:
|
||||||
import does not actually know that the selection field uses a function
|
import does not actually know that the selection field uses a function
|
||||||
"""
|
"""
|
||||||
# NOTE: conflict between a value and a label => ?
|
# NOTE: conflict between a value and a label => pick first
|
||||||
ids, messages = self.import_(['value'], [
|
ids, messages = self.import_(['value'], [
|
||||||
['3'],
|
['3'],
|
||||||
["Grault"],
|
["Grault"],
|
||||||
|
@ -497,38 +501,20 @@ class test_selection_function(ImporterCase):
|
||||||
def test_translated(self):
|
def test_translated(self):
|
||||||
""" Expects output of selection function returns translated labels
|
""" Expects output of selection function returns translated labels
|
||||||
"""
|
"""
|
||||||
self.registry('res.lang').create(self.cr, openerp.SUPERUSER_ID, {
|
self.add_translations(
|
||||||
'name': u'Français',
|
'export.selection,value', 'selection', 'fr_FR', *self.translations_fr)
|
||||||
'code': 'fr_FR',
|
|
||||||
'translatable': True,
|
|
||||||
'date_format': '%d.%m.%Y',
|
|
||||||
'decimal_point': ',',
|
|
||||||
'thousand_sep': ' ',
|
|
||||||
})
|
|
||||||
Translations = self.registry('ir.translation')
|
|
||||||
for source, value in self.translations_fr:
|
|
||||||
Translations.create(self.cr, openerp.SUPERUSER_ID, {
|
|
||||||
'name': 'export.selection,value',
|
|
||||||
'lang': 'fr_FR',
|
|
||||||
'type': 'selection',
|
|
||||||
'src': source,
|
|
||||||
'value': value
|
|
||||||
})
|
|
||||||
ids, messages = self.import_(['value'], [
|
ids, messages = self.import_(['value'], [
|
||||||
['toto'],
|
['titi'],
|
||||||
['tete'],
|
['tete'],
|
||||||
], context={'lang': 'fr_FR'})
|
], context={'lang': 'fr_FR'})
|
||||||
self.assertIs(ids, False)
|
|
||||||
self.assertEqual(messages, [{
|
|
||||||
'type': 'error',
|
|
||||||
'rows': {'from': 1, 'to': 1},
|
|
||||||
'record': 1,
|
|
||||||
'field': 'value',
|
|
||||||
'message': "Value 'tete' not found in selection field 'value'",
|
|
||||||
}])
|
|
||||||
ids, messages = self.import_(['value'], [['Wheee']], context={'lang': 'fr_FR'})
|
|
||||||
self.assertEqual(len(ids), 1)
|
|
||||||
self.assertFalse(messages)
|
self.assertFalse(messages)
|
||||||
|
self.assertEqual(len(ids), 2)
|
||||||
|
self.assertEqual(values(self.read()), ['1', '2'])
|
||||||
|
|
||||||
|
ids, messages = self.import_(['value'], [['Wheee']], context={'lang': 'fr_FR'})
|
||||||
|
self.assertFalse(messages)
|
||||||
|
self.assertEqual(len(ids), 1)
|
||||||
|
|
||||||
class test_m2o(ImporterCase):
|
class test_m2o(ImporterCase):
|
||||||
model_name = 'export.many2one'
|
model_name = 'export.many2one'
|
||||||
|
|
Loading…
Reference in New Issue