mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-26 10:28:40 -06:00
Make tests to work, on 10.0
Fix small bugs
This commit is contained in:
parent
0c2740de22
commit
21526bd236
@ -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':
|
||||
|
@ -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, '')
|
||||
|
Loading…
Reference in New Issue
Block a user