1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
import base64
from odoo.addons.mail.tests.common import MailCommon
from odoo.tests import tagged, users
@tagged("ir_attachment")
class TestAttachment(MailCommon):
@users("employee")
def test_register_as_main_attachment(self):
""" Test 'register_as_main_attachment', especially the multi support """
records_model1 = self.env["mail.test.simple.main.attachment"].create([
{
"name": f"First model {idx}",
}
for idx in range(5)
])
records_model2 = self.env["mail.test.gateway.main.attachment"].create([
{
"name": f"Second model {idx}",
}
for idx in range(5)
])
record_nomain = self.env["mail.test.simple"].create({"name": "No Main Attachment"})
attachments = self.env["ir.attachment"].create([
{
"datas": base64.b64encode(b'AttContent'),
"name": f"AttachName_{record.name}.pdf",
"mimetype": "application/pdf",
"res_id": record.id,
"res_model": record._name,
}
for record in records_model1
] + [
{
"datas": base64.b64encode(b'AttContent'),
"name": f"AttachName_{record.name}.pdf",
"mimetype": "application/pdf",
"res_id": record.id,
"res_model": record._name,
}
for record in records_model2
] + [
{
"datas": base64.b64encode(b'AttContent'),
"name": "AttachName_free.pdf",
"mimetype": "application/pdf",
}, {
"datas": base64.b64encode(b'AttContent'),
"name": f"AttachName_{record_nomain.name}.pdf",
"mimetype": "application/pdf",
"res_id": record_nomain.id,
"res_model": record_nomain._name,
}
])
attachments.register_as_main_attachment()
for record, attachment in zip(records_model1, attachments[:5]):
self.assertEqual(record.message_main_attachment_id, attachment)
for record, attachment in zip(records_model2, attachments[5:10]):
self.assertEqual(record.message_main_attachment_id, attachment)
|