Make tests to work, on 10.0

Fix small bugs
This commit is contained in:
Carlos Almeida 2017-06-06 11:48:55 +01:00
parent 0c2740de22
commit 21526bd236
2 changed files with 23 additions and 19 deletions

View File

@ -126,7 +126,10 @@ OCR_LANGUAGE = [('afr', 'Afrikaans'),
class IrAttachment(models.Model): class IrAttachment(models.Model):
_inherit = 'ir.attachment' _inherit = 'ir.attachment'
language = fields.Selection(OCR_LANGUAGE, 'Language') language = fields.Selection(OCR_LANGUAGE, 'Language',
default=lambda self:
self.env['ir.config_parameter'].get_param(
'document_ocr.language', 'eng'))
# We need to redefine index_content field to be able to update it # We need to redefine index_content field to be able to update it
# on the onchange_language() # on the onchange_language()
index_content = fields.Text('Indexed Content', index_content = fields.Text('Indexed Content',
@ -151,17 +154,15 @@ class IrAttachment(models.Model):
bin_data = self._file_read(self.store_fname) bin_data = self._file_read(self.store_fname)
else: else:
bin_data = self.db_datas bin_data = self.db_datas
index_content = self._index( if bin_data:
bin_data.decode('base64'), self.datas_fname, self.mimetype) index_content = self._index(
return {'value': { bin_data.decode('base64'), self.datas_fname, self.mimetype)
'index_content': index_content}} return {'value': {
'index_content': index_content}}
return {'value': {}}
@api.model @api.model
def _index(self, bin_data, datas_fname, mimetype): def _index(self, bin_data, datas_fname, mimetype):
if not self.language:
# Set default language
self.language = self.env['ir.config_parameter'].get_param(
'document_ocr.language', 'eng')
content = super(IrAttachment, self)._index( content = super(IrAttachment, self)._index(
bin_data, datas_fname, mimetype) bin_data, datas_fname, mimetype)
if not content or content == 'image': if not content or content == 'image':

View File

@ -4,10 +4,11 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from StringIO import StringIO from StringIO import StringIO
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont, PdfImagePlugin, PalmImagePlugin
from ..models.ir_attachment import _MARKER_PHRASE
from odoo.tests.common import TransactionCase from odoo.tests.common import TransactionCase
from ..models.ir_attachment import _MARKER_PHRASE
class TestDocumentOcr(TransactionCase): class TestDocumentOcr(TransactionCase):
def test_document_ocr(self): def test_document_ocr(self):
@ -20,15 +21,17 @@ class TestDocumentOcr(TransactionCase):
# test a plain image # test a plain image
data = StringIO() data = StringIO()
test_image.save(data, 'png') test_image.save(data, 'png')
result = self.env['ir.attachment']._index( attachment = self.env['ir.attachment'].create({
'name': 'testattachment'})
result = attachment._index(
data.getvalue(), 'test.png', None) data.getvalue(), 'test.png', None)
self.assertEqual(result[1].strip(), 'Hello world') self.assertEqual(result.strip(), 'Hello world')
# should also work for pdfs # should also work for pdfs
data = StringIO() data = StringIO()
test_image.save(data, 'pdf', resolution=300) test_image.save(data, 'pdf', resolution=300)
result = self.env['ir.attachment']._index( result = attachment._index(
data.getvalue(), 'test.pdf', None) data.getvalue(), 'test.pdf', None)
self.assertEqual(result[1].strip(), 'Hello world') self.assertEqual(result.strip(), 'Hello world')
# check cron # check cron
self.env['ir.config_parameter'].set_param( self.env['ir.config_parameter'].set_param(
'document_ocr.synchronous', 'False') 'document_ocr.synchronous', 'False')
@ -39,12 +42,12 @@ class TestDocumentOcr(TransactionCase):
self.assertEqual(attachment.index_content, _MARKER_PHRASE) self.assertEqual(attachment.index_content, _MARKER_PHRASE)
attachment._ocr_cron() attachment._ocr_cron()
self.assertEqual(attachment.index_content.strip(), 'Hello world') self.assertEqual(attachment.index_content.strip(), 'Hello world')
# and for an unreadable image, we expect an error # and for an unreadable image, we expect an empty string
self.env['ir.config_parameter'].set_param( self.env['ir.config_parameter'].set_param(
'document_ocr.synchronous', 'True') 'document_ocr.synchronous', 'True')
data = StringIO() data = StringIO()
test_image = Image.new('1', (200, 30)) test_image = Image.new('1', (200, 30))
test_image.save(data, 'Palm') test_image.save(data, 'palm')
result = self.env['ir.attachment']._index( result = attachment._index(
data.getvalue(), 'test.palm', None) data.getvalue(), 'test.palm', None)
self.assertEqual(result[1], None) self.assertEqual(result, '')