Best Python code snippet using Kiwi_python
test_mail_template.py
Source:test_mail_template.py  
1# -*- coding: utf-8 -*-2# Part of Odoo. See LICENSE file for full copyright and licensing details.3import base644from odoo.addons.test_mail.tests.common import TestMailCommon, TestRecipients5from odoo.tests import tagged6from odoo.tools import mute_logger7@tagged('mail_template')8class TestMailTemplate(TestMailCommon, TestRecipients):9    @classmethod10    def setUpClass(cls):11        super(TestMailTemplate, cls).setUpClass()12        cls.test_record = cls.env['mail.test.lang'].with_context(cls._test_context).create({13            'email_from': 'ignasse@example.com',14            'name': 'Test',15        })16        cls.user_employee.write({17            'groups_id': [(4, cls.env.ref('base.group_partner_manager').id)],18        })19        cls._attachments = [{20            'name': 'first.txt',21            'datas': base64.b64encode(b'My first attachment'),22            'res_model': 'res.partner',23            'res_id': cls.user_admin.partner_id.id24        }, {25            'name': 'second.txt',26            'datas': base64.b64encode(b'My second attachment'),27            'res_model': 'res.partner',28            'res_id': cls.user_admin.partner_id.id29        }]30        cls.email_1 = 'test1@example.com'31        cls.email_2 = 'test2@example.com'32        cls.email_3 = cls.partner_1.email33        # activate translations34        cls.env['res.lang']._activate_lang('es_ES')35        cls.env.ref('base.module_base')._update_translations(['es_ES'])36        # create a complete test template37        cls.test_template = cls._create_template('mail.test.lang', {38            'attachment_ids': [(0, 0, cls._attachments[0]), (0, 0, cls._attachments[1])],39            'body_html': '<p>English Body for <t t-out="object.name"/></p>',40            'lang': '{{ object.customer_id.lang or object.lang }}',41            'email_to': '%s, %s' % (cls.email_1, cls.email_2),42            'email_cc': '%s' % cls.email_3,43            'partner_to': '%s,%s' % (cls.partner_2.id, cls.user_admin.partner_id.id),44            'subject': 'English for {{ object.name }}',45        })46        # Make sure Spanish translations have not been altered47        description_translations = cls.env['ir.translation'].search([48            ('module', '=', 'test_mail'),49            ('src', '=', cls.test_record._description),50            ('lang', '=', 'es_ES')51        ])52        if description_translations:53            description_translations.update({'value': 'Spanish description'})54        else:55            description_translations.create({56                'type': 'model',57                'name': 'ir.model,name',58                'module': 'test_mail',59                'lang': 'es_ES',60                'res_id': cls.env['ir.model']._get_id('mail.test.lang'),61                'src': cls.test_record._description,62                'value': 'Spanish description',63                'state': 'translated',64            })65        cls.env['ir.translation'].create({66            'type': 'model',67            'name': 'mail.template,subject',68            'module': 'mail',69            'lang': 'es_ES',70            'res_id': cls.test_template.id,71            'value': 'Spanish for {{ object.name }}',72            'state': 'translated',73        })74        cls.env['ir.translation'].create({75            'type': 'model',76            'name': 'mail.template,body_html',77            'module': 'mail',78            'lang': 'es_ES',79            'res_id': cls.test_template.id,80            'value': '<p>Spanish Body for <t t-out="object.name" /></p>',81            'state': 'translated',82        })83        view = cls.env['ir.ui.view'].create({84            'name': 'test_layout',85            'key': 'test_layout',86            'type': 'qweb',87            'arch_db': '<body><t t-out="message.body"/> English Layout <t t-esc="model_description"/></body>'88        })89        cls.env['ir.model.data'].create({90            'name': 'test_layout',91            'module': 'test_mail',92            'model': 'ir.ui.view',93            'res_id': view.id94        })95        cls.env['ir.translation'].create({96            'type': 'model_terms',97            'name': 'ir.ui.view,arch_db',98            'module': 'test_mail',99            'lang': 'es_ES',100            'res_id': view.id,101            'src': 'English Layout',102            'value': 'Spanish Layout',103            'state': 'translated',104        })105        # admin should receive emails106        cls.user_admin.write({'notification_type': 'email'})107        # Force the attachments of the template to be in the natural order.108        cls.test_template.invalidate_cache(['attachment_ids'], ids=cls.test_template.ids)109    @mute_logger('odoo.addons.mail.models.mail_mail')110    def test_template_send_email(self):111        mail_id = self.test_template.send_mail(self.test_record.id)112        mail = self.env['mail.mail'].sudo().browse(mail_id)113        self.assertEqual(mail.email_cc, self.test_template.email_cc)114        self.assertEqual(mail.email_to, self.test_template.email_to)115        self.assertEqual(mail.recipient_ids, self.partner_2 | self.user_admin.partner_id)116        self.assertEqual(mail.subject, 'English for %s' % self.test_record.name)117    @mute_logger('odoo.addons.mail.models.mail_mail')118    def test_template_translation_lang(self):119        test_record = self.env['mail.test.lang'].browse(self.test_record.ids)120        test_record.write({121            'lang': 'es_ES',122        })123        test_template = self.env['mail.template'].browse(self.test_template.ids)124        mail_id = test_template.send_mail(test_record.id, notif_layout='test_mail.test_layout')125        mail = self.env['mail.mail'].sudo().browse(mail_id)126        self.assertEqual(mail.body_html,127                         '<body><p>Spanish Body for %s</p> Spanish Layout Spanish description</body>' % self.test_record.name)128        self.assertEqual(mail.subject, 'Spanish for %s' % self.test_record.name)129    @mute_logger('odoo.addons.mail.models.mail_mail')130    def test_template_translation_partner_lang(self):131        test_record = self.env['mail.test.lang'].browse(self.test_record.ids)132        customer = self.env['res.partner'].create({133            'email': 'robert.carlos@test.example.com',134            'lang': 'es_ES',135            'name': 'Roberto Carlos',136            })137        test_record.write({138            'customer_id': customer.id,139        })140        test_template = self.env['mail.template'].browse(self.test_template.ids)141        mail_id = test_template.send_mail(test_record.id, notif_layout='test_mail.test_layout')142        mail = self.env['mail.mail'].sudo().browse(mail_id)143        self.assertEqual(mail.body_html,144                         '<body><p>Spanish Body for %s</p> Spanish Layout Spanish description</body>' % self.test_record.name)145        self.assertEqual(mail.subject, 'Spanish for %s' % self.test_record.name)146    def test_template_add_context_action(self):147        self.test_template.create_action()148        # check template act_window has been updated149        self.assertTrue(bool(self.test_template.ref_ir_act_window))150        # check those records151        action = self.test_template.ref_ir_act_window152        self.assertEqual(action.name, 'Send Mail (%s)' % self.test_template.name)153        self.assertEqual(action.binding_model_id.model, 'mail.test.lang')154    # def test_template_scheduled_date(self):155    #     from unittest.mock import patch156    #     self.email_template_in_2_days = self.email_template.copy()157    #     with patch('odoo.addons.mail.tests.test_mail_template.datetime', wraps=datetime) as mock_datetime:158    #         mock_datetime.now.return_value = datetime(2017, 11, 15, 11, 30, 28)159    #         mock_datetime.side_effect = lambda *args, **kw: datetime(*args, **kw)160    #         self.email_template_in_2_days.write({161    #             'scheduled_date': "{{ (datetime.datetime.now() + relativedelta(days=2)).strftime('%s') }}" % DEFAULT_SERVER_DATETIME_FORMAT,162    #         })163    #         mail_now_id = self.email_template.send_mail(self.test_record.id)164    #         mail_in_2_days_id = self.email_template_in_2_days.send_mail(self.test_record.id)165    #         mail_now = self.env['mail.mail'].browse(mail_now_id)166    #         mail_in_2_days = self.env['mail.mail'].browse(mail_in_2_days_id)167    #         # mail preparation168    #         self.assertEqual(mail_now.exists() | mail_in_2_days.exists(), mail_now | mail_in_2_days)169    #         self.assertEqual(bool(mail_now.scheduled_date), False)170    #         self.assertEqual(mail_now.state, 'outgoing')171    #         self.assertEqual(mail_in_2_days.state, 'outgoing')172    #         scheduled_date = datetime.strptime(mail_in_2_days.scheduled_date, DEFAULT_SERVER_DATETIME_FORMAT)173    #         date_in_2_days = datetime.now() + timedelta(days = 2)174    #         self.assertEqual(scheduled_date, date_in_2_days)175    #         # self.assertEqual(scheduled_date.month, date_in_2_days.month)176    #         # self.assertEqual(scheduled_date.year, date_in_2_days.year)177    #         # Launch the scheduler on the first mail, it should be reported in self.mails178    #         # and the mail_mail is now deleted179    #         self.env['mail.mail'].process_email_queue()180    #         self.assertEqual(mail_now.exists() | mail_in_2_days.exists(), mail_in_2_days)181    #         # Launch the scheduler on the first mail, it's still in 'outgoing' state182    #         self.env['mail.mail'].process_email_queue(ids=[mail_in_2_days.id])183    #         self.assertEqual(mail_in_2_days.state, 'outgoing')...test_variants.py
Source:test_variants.py  
1# -*- coding: utf-8 -*-2# Part of Odoo. See LICENSE file for full copyright and licensing details.3from . import common4class TestVariants(common.TestProductCommon):5    def setUp(self):6        res = super(TestVariants, self).setUp()7        self.size_attr = self.env['product.attribute'].create({'name': 'Size'})8        self.size_attr_value_s = self.env['product.attribute.value'].create({'name': 'S', 'attribute_id': self.size_attr.id})9        self.size_attr_value_m = self.env['product.attribute.value'].create({'name': 'M', 'attribute_id': self.size_attr.id})10        self.size_attr_value_l = self.env['product.attribute.value'].create({'name': 'L', 'attribute_id': self.size_attr.id})11        return res12    def test_variants_creation_mono(self):13        test_template = self.env['product.template'].create({14            'name': 'Sofa',15            'uom_id': self.uom_unit.id,16            'uom_po_id': self.uom_unit.id,17            'attribute_line_ids': [(0, 0, {18                'attribute_id': self.size_attr.id,19                'value_ids': [(4, self.size_attr_value_s.id)],20            })]21        })22        # produced variants: one variant, because mono value23        self.assertEqual(len(test_template.product_variant_ids), 1)24        self.assertEqual(test_template.product_variant_ids.attribute_value_ids, self.size_attr_value_s)25    def test_variants_creation_mono_double(self):26        test_template = self.env['product.template'].create({27            'name': 'Sofa',28            'uom_id': self.uom_unit.id,29            'uom_po_id': self.uom_unit.id,30            'attribute_line_ids': [(0, 0, {31                'attribute_id': self.prod_att_1.id,32                'value_ids': [(4, self.prod_attr1_v2.id)],33            }), (0, 0, {34                'attribute_id': self.size_attr.id,35                'value_ids': [(4, self.size_attr_value_s.id)],36            })]37        })38        # produced variants: one variant, because only 1 combination is possible39        self.assertEqual(len(test_template.product_variant_ids), 1)40        self.assertEqual(test_template.product_variant_ids.attribute_value_ids, self.size_attr_value_s + self.prod_attr1_v2)41    def test_variants_creation_mono_multi(self):42        test_template = self.env['product.template'].create({43            'name': 'Sofa',44            'uom_id': self.uom_unit.id,45            'uom_po_id': self.uom_unit.id,46            'attribute_line_ids': [(0, 0, {47                'attribute_id': self.prod_att_1.id,48                'value_ids': [(4, self.prod_attr1_v2.id)],49            }), (0, 0, {50                'attribute_id': self.size_attr.id,51                'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id)],52            })]53        })54        # produced variants: two variants, simple matrix55        self.assertEqual(len(test_template.product_variant_ids), 2)56        for value in self.size_attr_value_s + self.size_attr_value_m:57            products = self.env['product.product'].search([58                ('product_tmpl_id', '=', test_template.id),59                ('attribute_value_ids', 'in', value.id),60                ('attribute_value_ids', 'in', self.prod_attr1_v2.id)61            ])62            self.assertEqual(len(products), 1)63    def test_variants_creation_matrix(self):64        test_template = self.env['product.template'].create({65            'name': 'Sofa',66            'uom_id': self.uom_unit.id,67            'uom_po_id': self.uom_unit.id,68            'attribute_line_ids': [(0, 0, {69                'attribute_id': self.prod_att_1.id,70                'value_ids': [(4, self.prod_attr1_v1.id), (4, self.prod_attr1_v2.id)],71            }), (0, 0, {72                'attribute_id': self.size_attr.id,73                'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id), (4, self.size_attr_value_l.id)],74            })]75        })76        # produced variants: value matrix : 2x3 values77        self.assertEqual(len(test_template.product_variant_ids), 6)78        for value_1 in self.prod_attr1_v1 + self.prod_attr1_v2:79            for value_2 in self.size_attr_value_m + self.size_attr_value_m + self.size_attr_value_l:80                products = self.env['product.product'].search([81                    ('product_tmpl_id', '=', test_template.id),82                    ('attribute_value_ids', 'in', value_1.id),83                    ('attribute_value_ids', 'in', value_2.id)84                ])85                self.assertEqual(len(products), 1)86    def test_variants_creation_multi_update(self):87        test_template = self.env['product.template'].create({88            'name': 'Sofa',89            'uom_id': self.uom_unit.id,90            'uom_po_id': self.uom_unit.id,91            'attribute_line_ids': [(0, 0, {92                'attribute_id': self.prod_att_1.id,93                'value_ids': [(4, self.prod_attr1_v1.id), (4, self.prod_attr1_v2.id)],94            }), (0, 0, {95                'attribute_id': self.size_attr.id,96                'value_ids': [(4, self.size_attr_value_s.id), (4, self.size_attr_value_m.id)],97            })]98        })99        size_attribute_line = test_template.attribute_line_ids.filtered(lambda line: line.attribute_id == self.size_attr)100        test_template.write({101            'attribute_line_ids': [(1, size_attribute_line.id, {102                'value_ids': [(4, self.size_attr_value_l.id)],103            })]...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
