From 21526bd236b7b4a3054aaee65ce8c3e7b28a3dbf Mon Sep 17 00:00:00 2001 From: Carlos Almeida Date: Tue, 6 Jun 2017 11:48:55 +0100 Subject: [PATCH] Make tests to work, on 10.0 Fix small bugs --- document_ocr/models/ir_attachment.py | 19 ++++++++++--------- document_ocr/tests/test_document_ocr.py | 23 +++++++++++++---------- 2 files changed, 23 insertions(+), 19 deletions(-) diff --git a/document_ocr/models/ir_attachment.py b/document_ocr/models/ir_attachment.py index 7247d3e4..efbd9b18 100644 --- a/document_ocr/models/ir_attachment.py +++ b/document_ocr/models/ir_attachment.py @@ -126,7 +126,10 @@ OCR_LANGUAGE = [('afr', 'Afrikaans'), class IrAttachment(models.Model): _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 # on the onchange_language() index_content = fields.Text('Indexed Content', @@ -151,17 +154,15 @@ class IrAttachment(models.Model): bin_data = self._file_read(self.store_fname) else: bin_data = self.db_datas - index_content = self._index( - bin_data.decode('base64'), self.datas_fname, self.mimetype) - return {'value': { - 'index_content': index_content}} + if bin_data: + index_content = self._index( + bin_data.decode('base64'), self.datas_fname, self.mimetype) + return {'value': { + 'index_content': index_content}} + return {'value': {}} @api.model 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( bin_data, datas_fname, mimetype) if not content or content == 'image': diff --git a/document_ocr/tests/test_document_ocr.py b/document_ocr/tests/test_document_ocr.py index 9765e291..fa5c6137 100644 --- a/document_ocr/tests/test_document_ocr.py +++ b/document_ocr/tests/test_document_ocr.py @@ -4,10 +4,11 @@ # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from StringIO import StringIO -from PIL import Image, ImageDraw, ImageFont -from ..models.ir_attachment import _MARKER_PHRASE +from PIL import Image, ImageDraw, ImageFont, PdfImagePlugin, PalmImagePlugin from odoo.tests.common import TransactionCase +from ..models.ir_attachment import _MARKER_PHRASE + class TestDocumentOcr(TransactionCase): def test_document_ocr(self): @@ -20,15 +21,17 @@ class TestDocumentOcr(TransactionCase): # test a plain image data = StringIO() 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) - self.assertEqual(result[1].strip(), 'Hello world') + self.assertEqual(result.strip(), 'Hello world') # should also work for pdfs data = StringIO() test_image.save(data, 'pdf', resolution=300) - result = self.env['ir.attachment']._index( + result = attachment._index( data.getvalue(), 'test.pdf', None) - self.assertEqual(result[1].strip(), 'Hello world') + self.assertEqual(result.strip(), 'Hello world') # check cron self.env['ir.config_parameter'].set_param( 'document_ocr.synchronous', 'False') @@ -39,12 +42,12 @@ class TestDocumentOcr(TransactionCase): self.assertEqual(attachment.index_content, _MARKER_PHRASE) attachment._ocr_cron() 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( 'document_ocr.synchronous', 'True') data = StringIO() test_image = Image.new('1', (200, 30)) - test_image.save(data, 'Palm') - result = self.env['ir.attachment']._index( + test_image.save(data, 'palm') + result = attachment._index( data.getvalue(), 'test.palm', None) - self.assertEqual(result[1], None) + self.assertEqual(result, '')