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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# -*- encoding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2013 Smile (<http://www.smile.fr>). All Rights Reserved
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from contextlib import contextmanager
from openerp.tests.common import TransactionCase
from openerp.tools.config import config
from openerp.addons.base.ir.ir_mail_server import MailDeliveryException
@contextmanager
def config_to_enable_email_sending(enable_email_sending):
init_config = {
'enable_email_sending': config.get('enable_email_sending'),
}
try:
config['enable_email_sending'] = enable_email_sending
yield config
finally:
for key, value in init_config.iteritems():
config[key] = value
class BaseTest(TransactionCase):
def setUp(self):
super(BaseTest, self).setUp()
self.model = self.env['res.partner.category']
def test_bulk_create(self):
"""
I create three categories with the same method call
I check that three categories were created
"""
names = ['t1', 't2', 't3']
vals_list = [{'name': name} for name in names]
categories = self.model.bulk_create(vals_list)
self.assertEquals(3, len(categories), 'Three categories shoudl have been created!')
self.assertListEqual(names, sorted(categories.mapped('name')),
'Names of the created categories are wrong!')
def test_unlink_cascade(self):
parent = self.model.create({'name': 'Parent'})
child = self.model.create({'name': 'Child', 'parent_id': parent.id})
self.assertTrue(parent.unlink())
self.assertFalse(child.exists())
def _get_email(self):
vals = {
'subject': 'The subject',
'body': 'and the body',
'email_from': 'admin@example.org',
'email_to': 'demo@example.org',
}
return self.env['mail.mail'].create(vals)
def test_disable_email_sending(self):
"""
I disable email sending in config file
I send an email
I check that no email was sent
"""
email = self._get_email()
with config_to_enable_email_sending(False):
email.send()
self.assertEquals('exception', email.state, 'Email should be in exception!')
def test_enable_email_sending(self):
"""
I enable email sending in config file
I send an email
I check that email was sent
"""
# I remove all outgoing mail server to be sure the exception will be raised
self.env['ir.mail_server'].search([]).unlink()
email = self._get_email()
with config_to_enable_email_sending(True):
with self.assertRaises(MailDeliveryException):
email.send(raise_exception=True)