mirror of
https://github.com/OCA/knowledge.git
synced 2025-07-13 15:34:49 -06:00
[FIX] CI
[ADD] tests for attachments_to_filesystem
This commit is contained in:
parent
38d824f426
commit
c4f104ae73
@ -28,6 +28,9 @@
|
||||
"depends": [
|
||||
'base',
|
||||
],
|
||||
"demo": [
|
||||
"demo/ir_attachment.xml",
|
||||
],
|
||||
"data": [
|
||||
"data/ir_cron.xml",
|
||||
"data/init.xml",
|
||||
|
14
attachments_to_filesystem/demo/ir_attachment.xml
Normal file
14
attachments_to_filesystem/demo/ir_attachment.xml
Normal file
@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<openerp>
|
||||
<data>
|
||||
<record id="test_attachment" model="ir.attachment">
|
||||
<field name="name">Test attachment</field>
|
||||
<field name="db_datas">aGVsbG8gd29ybGQ=</field>
|
||||
</record>
|
||||
<record id="test_attachment_unknown_model" model="ir.attachment">
|
||||
<field name="name">Test attachment</field>
|
||||
<field name="db_datas">aGVsbG8gd29ybGQ=</field>
|
||||
<field name="res_model">unknown.unknown</field>
|
||||
</record>
|
||||
</data>
|
||||
</openerp>
|
@ -61,7 +61,7 @@ class IrAttachment(models.Model):
|
||||
# otherwise, configure our cronjob to run next night
|
||||
user = self.env.user
|
||||
next_night = datetime.now() + relativedelta(
|
||||
hour=01, minute=42, second=0)
|
||||
hour=1, minute=42, second=0)
|
||||
user_tz = user.tz or 'UTC'
|
||||
next_night = pytz.timezone(user_tz).localize(next_night).astimezone(
|
||||
pytz.utc).replace(tzinfo=None)
|
||||
@ -79,28 +79,27 @@ class IrAttachment(models.Model):
|
||||
"""Do the actual moving"""
|
||||
limit = int(
|
||||
self.env['ir.config_parameter'].get_param(
|
||||
'attachments_to_filesystem.limit', '0')) or limit
|
||||
ir_attachment = self.env['ir.attachment']
|
||||
attachments = ir_attachment.search(
|
||||
'attachments_to_filesystem.limit', '0')
|
||||
) or limit
|
||||
attachments = self.env['ir.attachment'].search(
|
||||
[('db_datas', '!=', False)], limit=limit)
|
||||
logging.info('moving %d attachments to filestore', len(attachments))
|
||||
# attachments can be big, so we read every attachment on its own
|
||||
for counter, attachment_id in enumerate(attachments.ids, start=1):
|
||||
attachment_data = self.pool['ir.attachment'].read(
|
||||
self._cr, self._uid, [attachment_id], ['datas', 'res_model']
|
||||
)[0]
|
||||
for counter, attachment in enumerate(attachments, start=1):
|
||||
attachment_data = attachment.read(['datas', 'res_model'])[0]
|
||||
if attachment_data['res_model'] and not self.env.registry.get(
|
||||
attachment_data['res_model']):
|
||||
attachment_data['res_model']
|
||||
):
|
||||
logging.warning(
|
||||
'not moving attachment %d because it links to unknown '
|
||||
'model %s', attachment_id, attachment_data['res_model'])
|
||||
'model %s', attachment.id, attachment_data['res_model'])
|
||||
continue
|
||||
try:
|
||||
ir_attachment.browse(attachment_id).write({
|
||||
attachment.write({
|
||||
'datas': attachment_data['datas'],
|
||||
'db_datas': False,
|
||||
})
|
||||
except Exception:
|
||||
logging.exception('Error moving attachment #%d', attachment_id)
|
||||
logging.exception('Error moving attachment #%d', attachment.id)
|
||||
if not counter % (len(attachments) / 100 or limit):
|
||||
logging.info('moving attachments: %d done', counter)
|
||||
|
4
attachments_to_filesystem/tests/__init__.py
Normal file
4
attachments_to_filesystem/tests/__init__.py
Normal file
@ -0,0 +1,4 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2017 Therp BV <http://therp.nl>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
from . import test_attachments_to_filesystem
|
@ -0,0 +1,22 @@
|
||||
# -*- coding: utf-8 -*-
|
||||
# © 2017 Therp BV <http://therp.nl>
|
||||
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
|
||||
from openerp.tests.common import TransactionCase
|
||||
|
||||
|
||||
class TestAttachmentsToFilesystem(TransactionCase):
|
||||
def test_attachments_to_filesystem(self):
|
||||
# given the init function was run for the asynchronous behavior
|
||||
# already, we just need to test synchronous behavior
|
||||
self.env['ir.config_parameter'].set_param(
|
||||
'attachments_to_filesystem.move_during_init', 'yes'
|
||||
)
|
||||
self.env['ir.attachment']._attachments_to_filesystem_cron()
|
||||
self.assertFalse(
|
||||
self.env.ref('attachments_to_filesystem.test_attachment').db_datas
|
||||
)
|
||||
self.assertTrue(
|
||||
self.env.ref(
|
||||
'attachments_to_filesystem.test_attachment_unknown_model'
|
||||
).db_datas
|
||||
)
|
@ -56,4 +56,3 @@ Contributors
|
||||
'installable': False,
|
||||
'auto_install': False,
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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)
|
||||
|
@ -40,4 +40,3 @@ This module adds a company field to document page and the multi-company rule.
|
||||
'auto_install': False,
|
||||
'images': [],
|
||||
}
|
||||
|
||||
|
@ -44,4 +44,3 @@ class document_page(orm.Model):
|
||||
'company_id': lambda self, cr, uid, c: self.pool.get('res.company')
|
||||
._company_default_get(cr, uid, 'document_page', context=c)
|
||||
}
|
||||
|
||||
|
@ -37,6 +37,7 @@ def populate_tags(cr, column):
|
||||
env = Environment(cr, SUPERUSER_ID, {})
|
||||
document_page_tag = env['document.page.tag']
|
||||
document_page = env['document.page']
|
||||
# pylint: disable=E8103
|
||||
cr.execute(
|
||||
'SELECT %s, ARRAY_AGG(id) from %s WHERE %s IS NOT NULL GROUP BY %s' % (
|
||||
column, env['document.page']._table, column, column))
|
||||
|
@ -1,3 +1,3 @@
|
||||
"id","name","model_id:id","group_id:id","perm_read","perm_write","perm_create","perm_unlink"
|
||||
access_document_page_tag,access_document_page_tag,model_document_page_tag,,1,0,0,0
|
||||
access_document_page_tag_all,access_document_page_tag,model_document_page_tag,,1,0,0,0
|
||||
access_document_page_tag,access_document_page_tag,model_document_page_tag,base.group_user,1,1,1,1
|
||||
|
|
@ -20,6 +20,7 @@
|
||||
from psycopg2 import IntegrityError
|
||||
from openerp.tests.common import TransactionCase
|
||||
from ..hooks import post_init_hook
|
||||
from openerp.tools.misc import mute_logger
|
||||
|
||||
|
||||
class TestDocumentPageTags(TransactionCase):
|
||||
@ -28,5 +29,6 @@ class TestDocumentPageTags(TransactionCase):
|
||||
post_init_hook(self.cr, self.registry)
|
||||
# check we can't create nonunique tags
|
||||
with self.assertRaises(IntegrityError):
|
||||
self.env['document.page.tag'].name_create('test')
|
||||
self.env['document.page.tag'].name_create('test')
|
||||
with mute_logger('openerp.sql_db'):
|
||||
self.env['document.page.tag'].name_create('test')
|
||||
self.env['document.page.tag'].name_create('test')
|
||||
|
Loading…
Reference in New Issue
Block a user