[ADD] tests for attachments_to_filesystem
This commit is contained in:
Holger Brunn 2017-05-09 10:19:07 +02:00 committed by len
parent 8d69a35d61
commit d3a37e5010
2 changed files with 26 additions and 16 deletions

View File

@ -53,7 +53,7 @@ class IrAttachment(models.Model):
stderr=subprocess.PIPE, stderr=subprocess.PIPE,
) )
stdout, stderr = process.communicate(image_data.getvalue()) stdout, stderr = process.communicate(image_data.getvalue())
if stderr: if process.returncode:
_logger.error('Error during OCR: %s', stderr) _logger.error('Error during OCR: %s', stderr)
return stdout return stdout

View File

@ -4,7 +4,8 @@
from PIL import Image, ImageDraw, ImageFont from PIL import Image, ImageDraw, ImageFont
from StringIO import StringIO from StringIO import StringIO
from openerp.tests.common import TransactionCase 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): class TestDocumentOcr(TransactionCase):
@ -21,12 +22,15 @@ class TestDocumentOcr(TransactionCase):
result = self.env['ir.attachment']._index( result = self.env['ir.attachment']._index(
data.getvalue(), 'test.png', None) data.getvalue(), 'test.png', None)
self.assertEqual(result[1].strip(), 'Hello world') self.assertEqual(result[1].strip(), 'Hello world')
# should also work for pdfs # should also work for pdfs if supported, protect against
data = StringIO() # ancient pillows
test_image.save(data, 'pdf', resolution=300) if hasattr(Image, 'registered_extensions') and\
result = self.env['ir.attachment']._index( 'PDF' in Image.registered_extensions().values():
data.getvalue(), 'test.pdf', None) data = StringIO()
self.assertEqual(result[1].strip(), 'Hello world') 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 # check cron
self.env['ir.config_parameter'].set_param( self.env['ir.config_parameter'].set_param(
'document_ocr.synchronous', 'False') 'document_ocr.synchronous', 'False')
@ -38,11 +42,17 @@ class TestDocumentOcr(TransactionCase):
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 error
self.env['ir.config_parameter'].set_param( if hasattr(Image, 'registered_extensions') and\
'document_ocr.synchronous', 'True') 'PALM' in Image.registered_extensions().values():
data = StringIO() self.env['ir.config_parameter'].set_param(
test_image = Image.new('1', (200, 30)) 'document_ocr.synchronous', 'True')
test_image.save(data, 'Palm') data = StringIO()
result = self.env['ir.attachment']._index( test_image = Image.new('1', (200, 30))
data.getvalue(), 'test.palm', None) test_image.save(data, 'Palm')
self.assertEqual(result[1], None) 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)