2013-09-26 13:38:50 +00:00
|
|
|
# -*- encoding: utf-8 -*-
|
2013-10-07 14:53:46 +00:00
|
|
|
import json
|
2013-09-26 13:38:50 +00:00
|
|
|
import os
|
2013-10-07 14:53:46 +00:00
|
|
|
import xml.dom.minidom
|
2013-09-26 13:38:50 +00:00
|
|
|
|
|
|
|
from openerp.tests import common
|
|
|
|
|
|
|
|
directory = os.path.dirname(__file__)
|
|
|
|
|
2013-10-07 14:53:46 +00:00
|
|
|
impl = xml.dom.minidom.getDOMImplementation()
|
|
|
|
doc = impl.createDocument(None, None, None)
|
2013-10-07 13:25:54 +00:00
|
|
|
|
|
|
|
class TestExport(common.TransactionCase):
|
|
|
|
_model = None
|
|
|
|
|
2013-09-26 13:38:50 +00:00
|
|
|
def setUp(self):
|
2013-10-07 13:25:54 +00:00
|
|
|
super(TestExport, self).setUp()
|
|
|
|
self.Model = self.registry(self._model)
|
2013-10-07 09:15:56 +00:00
|
|
|
self.columns = self.Model._all_columns
|
2013-09-26 13:38:50 +00:00
|
|
|
|
|
|
|
def get_column(self, name):
|
|
|
|
return self.Model._all_columns[name].column
|
|
|
|
|
|
|
|
def get_converter(self, name):
|
2013-10-07 09:15:56 +00:00
|
|
|
column = self.get_column(name)
|
|
|
|
try:
|
|
|
|
model = self.registry('ir.qweb.field.' + column._type)
|
|
|
|
except KeyError:
|
|
|
|
model = self.registry('ir.qweb.field')
|
|
|
|
|
|
|
|
return lambda value: model.value_to_html(
|
|
|
|
self.cr, self.uid, value, column)
|
2013-09-26 13:38:50 +00:00
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestBasicExport(TestExport):
|
|
|
|
_model = 'test_converter.test_model'
|
|
|
|
|
|
|
|
class TestCharExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_char(self):
|
|
|
|
converter = self.get_converter('char')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter('foo')
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, 'foo')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter("foo<bar>")
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "foo<bar>")
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestIntegerExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_integer(self):
|
|
|
|
converter = self.get_converter('integer')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42)
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "42")
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestFloatExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_float(self):
|
|
|
|
converter = self.get_converter('float')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42.0)
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "42.0")
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42.0100)
|
2013-09-27 11:34:24 +00:00
|
|
|
self.assertEqual(value, "42.01")
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42.01234)
|
2013-09-27 11:34:24 +00:00
|
|
|
self.assertEqual(value, "42.01234")
|
|
|
|
|
|
|
|
def test_numeric(self):
|
|
|
|
converter = self.get_converter('numeric')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42.0)
|
2013-09-27 11:34:24 +00:00
|
|
|
self.assertEqual(value, '42.00')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(42.01234)
|
2013-09-27 11:34:24 +00:00
|
|
|
self.assertEqual(value, '42.01')
|
|
|
|
|
2013-10-07 14:53:46 +00:00
|
|
|
class TestCurrencyExport(TestExport):
|
2013-10-08 12:20:06 +00:00
|
|
|
_model = 'test_converter.monetary'
|
2013-10-07 14:53:46 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
super(TestCurrencyExport, self).setUp()
|
|
|
|
self.Currency = self.registry('res.currency')
|
2013-10-11 11:16:20 +00:00
|
|
|
self.base = self.create(self.Currency, name="Source", symbol=u'source')
|
|
|
|
self.create_rate(self.base)
|
2013-10-07 14:53:46 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
def convert(self, obj, dest):
|
2013-10-08 12:20:06 +00:00
|
|
|
converter = self.registry('ir.qweb.field.monetary')
|
2013-10-11 11:16:20 +00:00
|
|
|
options = {
|
|
|
|
'widget': 'monetary',
|
|
|
|
'source_currency': 'c1',
|
|
|
|
'display_currency': 'c2'
|
|
|
|
}
|
2013-10-07 14:53:46 +00:00
|
|
|
converted = converter.to_html(
|
|
|
|
self.cr, self.uid, 'value', obj, options,
|
|
|
|
doc.createElement('span'),
|
|
|
|
{'field': 'obj.value', 'field-options': json.dumps(options)},
|
2013-10-11 11:16:20 +00:00
|
|
|
'', {'obj': obj, 'c1': self.base, 'c2': dest, })
|
2013-10-07 14:53:46 +00:00
|
|
|
return converted
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
def create_rate(self, currency, rate=1.0):
|
|
|
|
Rate = self.registry('res.currency.rate')
|
|
|
|
self.create(Rate, name='1970-01-01', rate=rate, currency_id=currency.id)
|
|
|
|
|
2013-10-07 14:53:46 +00:00
|
|
|
def test_currency_post(self):
|
|
|
|
currency = self.create(self.Currency, name="Test", symbol=u"test")
|
2013-10-11 11:16:20 +00:00
|
|
|
self.create_rate(currency)
|
2013-10-07 14:53:46 +00:00
|
|
|
obj = self.create(self.Model, value=0.12, currency_id=currency.id)
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
converted = self.convert(obj, dest=currency)
|
2013-10-07 14:53:46 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
converted,
|
|
|
|
'<span data-oe-model="{obj._model._name}" data-oe-id="{obj.id}" '
|
2013-10-08 12:20:06 +00:00
|
|
|
'data-oe-field="value" data-oe-type="monetary" '
|
2013-10-08 09:22:07 +00:00
|
|
|
'data-oe-expression="obj.value">'
|
2013-10-07 15:03:30 +00:00
|
|
|
'<span class="oe_currency_value">0.12</span>'
|
|
|
|
' {symbol}</span>'.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')
|
2013-10-11 11:16:20 +00:00
|
|
|
self.create_rate(currency)
|
2013-10-07 15:03:30 +00:00
|
|
|
obj = self.create(self.Model, value=0.12, currency_id=currency.id)
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
converted = self.convert(obj, dest=currency)
|
2013-10-07 15:03:30 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
converted,
|
|
|
|
'<span data-oe-model="{obj._model._name}" data-oe-id="{obj.id}" '
|
2013-10-08 12:20:06 +00:00
|
|
|
'data-oe-field="value" data-oe-type="monetary" '
|
2013-10-08 09:22:07 +00:00
|
|
|
'data-oe-expression="obj.value">'
|
2013-10-07 15:03:30 +00:00
|
|
|
'{symbol} '
|
|
|
|
'<span class="oe_currency_value">0.12</span>'
|
|
|
|
'</span>'.format(
|
2013-10-07 14:53:46 +00:00
|
|
|
obj=obj,
|
|
|
|
symbol=currency.symbol.encode('utf-8')
|
|
|
|
),)
|
|
|
|
|
2013-10-08 06:42:26 +00:00
|
|
|
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",)
|
2013-10-11 11:16:20 +00:00
|
|
|
self.create_rate(currency)
|
2013-10-08 06:42:26 +00:00
|
|
|
obj = self.create(self.Model, value=0.1234567, currency_id=currency.id)
|
|
|
|
|
2013-10-11 11:16:20 +00:00
|
|
|
converted = self.convert(obj, dest=currency)
|
2013-10-08 06:42:26 +00:00
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
converted,
|
|
|
|
'<span data-oe-model="{obj._model._name}" data-oe-id="{obj.id}" '
|
2013-10-08 12:20:06 +00:00
|
|
|
'data-oe-field="value" data-oe-type="monetary" '
|
2013-10-08 09:22:07 +00:00
|
|
|
'data-oe-expression="obj.value">'
|
2013-10-08 06:42:26 +00:00
|
|
|
'<span class="oe_currency_value">0.12</span>'
|
|
|
|
' {symbol}</span>'.format(
|
|
|
|
obj=obj,
|
|
|
|
symbol=currency.symbol.encode('utf-8')
|
|
|
|
),)
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestTextExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_text(self):
|
|
|
|
converter = self.get_converter('text')
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter("This is my text-kai")
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "This is my text-kai")
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter("""
|
2013-09-26 13:38:50 +00:00
|
|
|
. 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, """<br>
|
|
|
|
. The current line (address) in the buffer.<br>
|
|
|
|
$ The last line in the buffer.<br>
|
|
|
|
n The nth, line in the buffer where n is a number in the range [0,$].<br>
|
|
|
|
$ The last line in the buffer.<br>
|
|
|
|
- The previous line. This is equivalent to -1 and may be repeated with cumulative effect.<br>
|
|
|
|
-n The nth previous line, where n is a non-negative number.<br>
|
|
|
|
+ The next line. This is equivalent to +1 and may be repeated with cumulative effect.<br>
|
|
|
|
""")
|
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter("""
|
2013-09-26 13:38:50 +00:00
|
|
|
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>
|
|
|
|
""")
|
|
|
|
self.assertEqual(value, """<br>
|
|
|
|
fgdkls;hjas;lj <b>fdslkj</b> d;lasjfa lkdja <a href=http://spam.com>lfks</a><br>
|
|
|
|
fldkjsfhs <i style="color: red"><a href="http://spamspam.com">fldskjh</a></i><br>
|
|
|
|
""")
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestMany2OneExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_many2one(self):
|
|
|
|
converter = self.get_converter('many2one')
|
|
|
|
Sub = self.registry('test_converter.test_model.sub')
|
|
|
|
|
|
|
|
id0 = Sub.create(self.cr, self.uid, {'name': "Foo"})
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(Sub.browse(self.cr, self.uid, id0))
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "Foo")
|
|
|
|
|
|
|
|
id1 = Sub.create(self.cr, self.uid, {'name': "Fo<b>o</b>"})
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(Sub.browse(self.cr, self.uid, id1))
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "Fo<b>o</b>")
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestBinaryExport(TestBasicExport):
|
2013-10-07 11:31:33 +00:00
|
|
|
def test_image(self):
|
|
|
|
column = self.get_column('binary')
|
|
|
|
converter = self.registry('ir.qweb.field.image')
|
|
|
|
|
2013-09-26 13:38:50 +00:00
|
|
|
with open(os.path.join(directory, 'test_vectors', 'image'), 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
|
2013-10-07 12:51:55 +00:00
|
|
|
encoded_content = content.encode('base64')
|
2013-10-07 11:31:33 +00:00
|
|
|
value = converter.value_to_html(
|
2013-10-07 12:51:55 +00:00
|
|
|
self.cr, self.uid, encoded_content, column)
|
2013-10-07 11:31:33 +00:00
|
|
|
self.assertEqual(
|
|
|
|
value, '<img src="data:image/jpeg;base64,%s">' % (
|
2013-10-07 12:51:55 +00:00
|
|
|
encoded_content
|
2013-10-07 11:31:33 +00:00
|
|
|
))
|
2013-09-26 13:38:50 +00:00
|
|
|
|
|
|
|
with open(os.path.join(directory, 'test_vectors', 'pdf'), 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
|
2013-10-07 11:31:33 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
converter.value_to_html(
|
|
|
|
self.cr, self.uid, 'binary', content.encode('base64'), column)
|
2013-09-26 13:38:50 +00:00
|
|
|
|
|
|
|
with open(os.path.join(directory, 'test_vectors', 'pptx'), 'rb') as f:
|
|
|
|
content = f.read()
|
|
|
|
|
2013-10-07 11:31:33 +00:00
|
|
|
with self.assertRaises(ValueError):
|
|
|
|
converter.value_to_html(
|
|
|
|
self.cr, self.uid, 'binary', content.encode('base64'), column)
|
2013-09-26 13:38:50 +00:00
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestSelectionExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_selection(self):
|
2013-10-07 09:15:56 +00:00
|
|
|
[record] = self.Model.browse(self.cr, self.uid, [self.Model.create(self.cr, self.uid, {
|
|
|
|
'selection': 2,
|
|
|
|
'selection_str': 'C',
|
|
|
|
})])
|
2013-09-26 13:38:50 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
column_name = 'selection'
|
|
|
|
column = self.get_column(column_name)
|
|
|
|
converter = self.registry('ir.qweb.field.selection')
|
2013-09-26 13:38:50 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter.record_to_html(
|
|
|
|
self.cr, self.uid, column_name, record, column)
|
|
|
|
self.assertEqual(value, "réponse B")
|
2013-09-26 13:38:50 +00:00
|
|
|
|
2013-10-07 09:15:56 +00:00
|
|
|
column_name = 'selection_str'
|
|
|
|
column = self.get_column(column_name)
|
|
|
|
value = converter.record_to_html(
|
|
|
|
self.cr, self.uid, column_name, record, column)
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, "Qu'est-ce qu'il fout ce maudit pancake, tabernacle ?")
|
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
class TestHTMLExport(TestBasicExport):
|
2013-09-26 13:38:50 +00:00
|
|
|
def test_html(self):
|
|
|
|
converter = self.get_converter('html')
|
|
|
|
|
|
|
|
input = '<span>span</span>'
|
2013-10-07 09:15:56 +00:00
|
|
|
value = converter(input)
|
2013-09-26 13:38:50 +00:00
|
|
|
self.assertEqual(value, input)
|
2013-10-07 14:53:46 +00:00
|
|
|
|
2013-10-07 13:25:54 +00:00
|
|
|
# o2m, m2m?
|
|
|
|
# reference?
|