File: test_ir_attachment.py

package info (click to toggle)
odoo 18.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 878,716 kB
  • sloc: javascript: 927,937; python: 685,670; xml: 388,524; sh: 1,033; sql: 415; makefile: 26
file content (61 lines) | stat: -rw-r--r-- 2,331 bytes parent folder | download
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)