From d3a37e501013c6d1290224d4cfcedd6234df86d4 Mon Sep 17 00:00:00 2001 From: Holger Brunn Date: Tue, 9 May 2017 10:19:07 +0200 Subject: [PATCH] [FIX] CI [ADD] tests for attachments_to_filesystem --- document_ocr/models/ir_attachment.py | 2 +- document_ocr/tests/test_document_ocr.py | 40 +++++++++++++++---------- 2 files changed, 26 insertions(+), 16 deletions(-) diff --git a/document_ocr/models/ir_attachment.py b/document_ocr/models/ir_attachment.py index ec161712..da9fff31 100644 --- a/document_ocr/models/ir_attachment.py +++ b/document_ocr/models/ir_attachment.py @@ -53,7 +53,7 @@ class IrAttachment(models.Model): stderr=subprocess.PIPE, ) stdout, stderr = process.communicate(image_data.getvalue()) - if stderr: + if process.returncode: _logger.error('Error during OCR: %s', stderr) return stdout diff --git a/document_ocr/tests/test_document_ocr.py b/document_ocr/tests/test_document_ocr.py index 7dccb672..d4545ac2 100644 --- a/document_ocr/tests/test_document_ocr.py +++ b/document_ocr/tests/test_document_ocr.py @@ -4,7 +4,8 @@ from PIL import Image, ImageDraw, ImageFont from StringIO import StringIO from openerp.tests.common import TransactionCase -from openerp.addons.document_ocr.models.ir_attachment import _MARKER_PHRASE +from ..models.ir_attachment import _MARKER_PHRASE +from openerp.tools.misc import mute_logger class TestDocumentOcr(TransactionCase): @@ -21,12 +22,15 @@ class TestDocumentOcr(TransactionCase): result = self.env['ir.attachment']._index( data.getvalue(), 'test.png', None) self.assertEqual(result[1].strip(), 'Hello world') - # should also work for pdfs - data = StringIO() - test_image.save(data, 'pdf', resolution=300) - result = self.env['ir.attachment']._index( - data.getvalue(), 'test.pdf', None) - self.assertEqual(result[1].strip(), 'Hello world') + # should also work for pdfs if supported, protect against + # ancient pillows + if hasattr(Image, 'registered_extensions') and\ + 'PDF' in Image.registered_extensions().values(): + data = StringIO() + test_image.save(data, 'pdf', resolution=300) + result = self.env['ir.attachment']._index( + data.getvalue(), 'test.pdf', None) + self.assertEqual(result[1].strip(), 'Hello world') # check cron self.env['ir.config_parameter'].set_param( 'document_ocr.synchronous', 'False') @@ -38,11 +42,17 @@ class TestDocumentOcr(TransactionCase): attachment._ocr_cron() self.assertEqual(attachment.index_content.strip(), 'Hello world') # and for an unreadable image, we expect an error - 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( - data.getvalue(), 'test.palm', None) - self.assertEqual(result[1], None) + if hasattr(Image, 'registered_extensions') and\ + 'PALM' in Image.registered_extensions().values(): + 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') + with mute_logger( + 'openerp.addons.document_ocr.models.ir_attachment' + ): + result = self.env['ir.attachment']._index( + data.getvalue(), 'test.palm', None + ) + self.assertEqual(result[1], None)