# -*- encoding: utf-8 -*- import json import os import xml.dom.minidom import datetime from werkzeug.utils import escape as e from openerp.tests import common from openerp.addons.base.ir import ir_qweb directory = os.path.dirname(__file__) impl = xml.dom.minidom.getDOMImplementation() doc = impl.createDocument(None, None, None) class TestExport(common.TransactionCase): _model = None def setUp(self): super(TestExport, self).setUp() self.Model = self.registry(self._model) self.columns = self.Model._all_columns def get_column(self, name): return self.Model._all_columns[name].column def get_converter(self, name, type=None): column = self.get_column(name) for postfix in type, column._type, '': fs = ['ir', 'qweb', 'field'] if postfix is None: continue if postfix: fs.append(postfix) try: model = self.registry('.'.join(fs)) break except KeyError: pass return lambda value, options=None, context=None: e(model.value_to_html( self.cr, self.uid, value, column, options=options, context=context)) class TestBasicExport(TestExport): _model = 'test_converter.test_model' class TestCharExport(TestBasicExport): def test_char(self): converter = self.get_converter('char') value = converter('foo') self.assertEqual(value, 'foo') value = converter("foo") self.assertEqual(value, "foo<bar>") class TestIntegerExport(TestBasicExport): def test_integer(self): converter = self.get_converter('integer') value = converter(42) self.assertEqual(value, "42") class TestFloatExport(TestBasicExport): def setUp(self): super(TestFloatExport, self).setUp() self.registry('res.lang').write(self.cr, self.uid, [1], { 'grouping': '[3,0]' }) def test_float(self): converter = self.get_converter('float') value = converter(42.0) self.assertEqual(value, "42.0") value = converter(42.0100) self.assertEqual(value, "42.01") value = converter(42.01234) self.assertEqual(value, "42.01234") value = converter(1234567.89) self.assertEqual(value, '1,234,567.89') def test_numeric(self): converter = self.get_converter('numeric') value = converter(42.0) self.assertEqual(value, '42.00') value = converter(42.01234) self.assertEqual(value, '42.01') class TestCurrencyExport(TestExport): _model = 'test_converter.monetary' def setUp(self): super(TestCurrencyExport, self).setUp() self.Currency = self.registry('res.currency') self.base = self.create(self.Currency, name="Source", symbol=u'source') def create(self, model, context=None, **values): return model.browse( self.cr, self.uid, model.create(self.cr, self.uid, values, context=context), context=context) def convert(self, obj, dest): converter = self.registry('ir.qweb.field.monetary') options = { 'widget': 'monetary', 'display_currency': 'c2' } converted = converter.to_html( self.cr, self.uid, 'value', obj, options, doc.createElement('span'), {'field': 'obj.value', 'field-options': json.dumps(options)}, '', ir_qweb.QWebContext(self.cr, self.uid, {'obj': obj, 'c2': dest, })) return converted def test_currency_post(self): currency = self.create(self.Currency, name="Test", symbol=u"test") obj = self.create(self.Model, value=0.12) converted = self.convert(obj, dest=currency) self.assertEqual( converted, '' '0.12' ' {symbol}'.format( obj=obj, symbol=currency.symbol.encode('utf-8') ),) def test_currency_pre(self): currency = self.create( self.Currency, name="Test", symbol=u"test", position='before') obj = self.create(self.Model, value=0.12) converted = self.convert(obj, dest=currency) self.assertEqual( converted, '' '{symbol} ' '0.12' ''.format( obj=obj, symbol=currency.symbol.encode('utf-8') ),) def test_currency_precision(self): """ Precision should be the currency's, not the float field's """ currency = self.create(self.Currency, name="Test", symbol=u"test",) obj = self.create(self.Model, value=0.1234567) converted = self.convert(obj, dest=currency) self.assertEqual( converted, '' '0.12' ' {symbol}'.format( obj=obj, symbol=currency.symbol.encode('utf-8') ),) class TestTextExport(TestBasicExport): def test_text(self): converter = self.get_converter('text') value = converter("This is my text-kai") self.assertEqual(value, "This is my text-kai") value = converter(""" . The current line (address) in the buffer. $ The last line in the buffer. n The nth, line in the buffer where n is a number in the range [0,$]. $ The last line in the buffer. - The previous line. This is equivalent to -1 and may be repeated with cumulative effect. -n The nth previous line, where n is a non-negative number. + The next line. This is equivalent to +1 and may be repeated with cumulative effect. """) self.assertEqual(value, """
. The current line (address) in the buffer.
$ The last line in the buffer.
n The nth, line in the buffer where n is a number in the range [0,$].
$ The last line in the buffer.
- The previous line. This is equivalent to -1 and may be repeated with cumulative effect.
-n The nth previous line, where n is a non-negative number.
+ The next line. This is equivalent to +1 and may be repeated with cumulative effect.
""") value = converter(""" fgdkls;hjas;lj fdslkj d;lasjfa lkdja lfks fldkjsfhs fldskjh """) self.assertEqual(value, """
fgdkls;hjas;lj <b>fdslkj</b> d;lasjfa lkdja <a href=http://spam.com>lfks</a>
fldkjsfhs <i style="color: red"><a href="http://spamspam.com">fldskjh</a></i>
""") class TestMany2OneExport(TestBasicExport): def test_many2one(self): Sub = self.registry('test_converter.test_model.sub') id0 = self.Model.create(self.cr, self.uid, { 'many2one': Sub.create(self.cr, self.uid, {'name': "Foo"}) }) id1 = self.Model.create(self.cr, self.uid, { 'many2one': Sub.create(self.cr, self.uid, {'name': "Foo"}) }) def converter(record): column = self.get_column('many2one') model = self.registry('ir.qweb.field.many2one') return e(model.record_to_html( self.cr, self.uid, 'many2one', record, column)) value = converter(self.Model.browse(self.cr, self.uid, id0)) self.assertEqual(value, "Foo") value = converter(self.Model.browse(self.cr, self.uid, id1)) self.assertEqual(value, "Fo<b>o</b>") class TestBinaryExport(TestBasicExport): def test_image(self): column = self.get_column('binary') converter = self.registry('ir.qweb.field.image') with open(os.path.join(directory, 'test_vectors', 'image'), 'rb') as f: content = f.read() encoded_content = content.encode('base64') value = e(converter.value_to_html( self.cr, self.uid, encoded_content, column)) self.assertEqual( value, '' % ( encoded_content )) with open(os.path.join(directory, 'test_vectors', 'pdf'), 'rb') as f: content = f.read() with self.assertRaises(ValueError): e(converter.value_to_html( self.cr, self.uid, 'binary', content.encode('base64'), column)) with open(os.path.join(directory, 'test_vectors', 'pptx'), 'rb') as f: content = f.read() with self.assertRaises(ValueError): e(converter.value_to_html( self.cr, self.uid, 'binary', content.encode('base64'), column)) class TestSelectionExport(TestBasicExport): def test_selection(self): [record] = self.Model.browse(self.cr, self.uid, [self.Model.create(self.cr, self.uid, { 'selection': 2, 'selection_str': 'C', })]) column_name = 'selection' column = self.get_column(column_name) converter = self.registry('ir.qweb.field.selection') value = converter.record_to_html( self.cr, self.uid, column_name, record, column) self.assertEqual(value, "réponse B") column_name = 'selection_str' column = self.get_column(column_name) value = converter.record_to_html( self.cr, self.uid, column_name, record, column) self.assertEqual(value, "Qu'est-ce qu'il fout ce maudit pancake, tabernacle ?") class TestHTMLExport(TestBasicExport): def test_html(self): converter = self.get_converter('html') input = 'span' value = converter(input) self.assertEqual(value, input) class TestDatetimeExport(TestBasicExport): def setUp(self): super(TestDatetimeExport, self).setUp() # set user tz to known value Users = self.registry('res.users') Users.write(self.cr, self.uid, self.uid, { 'tz': 'Pacific/Niue' }, context=None) def test_date(self): converter = self.get_converter('date') value = converter('2011-05-03') # default lang/format is US self.assertEqual(value, '05/03/2011') def test_datetime(self): converter = self.get_converter('datetime') value = converter('2011-05-03 11:12:13') # default lang/format is US self.assertEqual(value, '05/03/2011 00:12:13') def test_custom_format(self): converter = self.get_converter('datetime') converter2 = self.get_converter('date') opts = {'format': 'MMMM d'} value = converter('2011-03-02 11:12:13', options=opts) value2 = converter2('2001-03-02', options=opts) self.assertEqual( value, 'March 2' ) self.assertEqual( value2, 'March 2' ) class TestDurationExport(TestBasicExport): def setUp(self): super(TestDurationExport, self).setUp() # needs to have lang installed otherwise falls back on en_US self.registry('res.lang').load_lang(self.cr, self.uid, 'fr_FR') def test_negative(self): converter = self.get_converter('float', 'duration') with self.assertRaises(ValueError): converter(-4) def test_missing_unit(self): converter = self.get_converter('float', 'duration') with self.assertRaises(ValueError): converter(4) def test_basic(self): converter = self.get_converter('float', 'duration') result = converter(4, {'unit': 'hour'}, {'lang': 'fr_FR'}) self.assertEqual(result, u'4 heures') result = converter(50, {'unit': 'second'}, {'lang': 'fr_FR'}) self.assertEqual(result, u'50 secondes') def test_multiple(self): converter = self.get_converter('float', 'duration') result = converter(1.5, {'unit': 'hour'}, {'lang': 'fr_FR'}) self.assertEqual(result, u"1 heure 30 minutes") result = converter(72, {'unit': 'second'}, {'lang': 'fr_FR'}) self.assertEqual(result, u"1 minute 12 secondes") class TestRelativeDatetime(TestBasicExport): # not sure how a test based on "current time" should be tested. Even less # so as it would mostly be a test of babel... def setUp(self): super(TestRelativeDatetime, self).setUp() # needs to have lang installed otherwise falls back on en_US self.registry('res.lang').load_lang(self.cr, self.uid, 'fr_FR') def test_basic(self): converter = self.get_converter('datetime', 'relative') t = datetime.datetime.utcnow() - datetime.timedelta(hours=1) result = converter(t, context={'lang': 'fr_FR'}) self.assertEqual(result, u"il y a 1 heure")