From 56cec604e6313c49582bbd9801686328696b2675 Mon Sep 17 00:00:00 2001 From: Yannick Tivisse Date: Fri, 20 Nov 2015 15:46:58 +0100 Subject: [PATCH] [ADD] mail: Test on email template on scheduled dates --- addons/mail/models/mail_mail.py | 9 ++++++-- addons/mail/tests/test_mail_template.py | 28 +++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) diff --git a/addons/mail/models/mail_mail.py b/addons/mail/models/mail_mail.py index 98f43005279..0e43d934e3e 100644 --- a/addons/mail/models/mail_mail.py +++ b/addons/mail/models/mail_mail.py @@ -3,6 +3,7 @@ import base64 import datetime import logging +import threading from email.utils import formataddr from urlparse import urljoin @@ -114,8 +115,12 @@ class MailMail(models.Model): try: # Force auto-commit - this is meant to be called by # the scheduler, and we can't allow rolling back the status - # of previously sent emails! - res = self.browse(ids).send(auto_commit=True) + # of previously sent emails! Does not auto_commit if in + # testing mode + if (not self.pool._init or getattr(threading.currentThread(), 'testing', False)): + res = self.browse(ids).send(auto_commit=False) + else: + res = self.browse(ids).send(auto_commit=True) except Exception: _logger.exception("Failed processing mail queue") return res diff --git a/addons/mail/tests/test_mail_template.py b/addons/mail/tests/test_mail_template.py index 66d3e2053b8..afd93018cf1 100644 --- a/addons/mail/tests/test_mail_template.py +++ b/addons/mail/tests/test_mail_template.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import base64 +import datetime from openerp.addons.mail.tests.common import TestMail from openerp.tools import mute_logger @@ -136,3 +137,30 @@ class TestMailTemplate(TestMail): self.assertEqual(action.name, 'Send Mail (%s)' % self.email_template.name) value = self.email_template.ref_ir_value self.assertEqual(value.name, 'Send Mail (%s)' % self.email_template.name) + + def test_set_scheduled_date_on_a_template(self): + self.email_template_in_2_days = self.email_template.copy() + self.email_template_in_2_days.write({'scheduled_date': "${(datetime.datetime.now() + relativedelta(days=2)).strftime('%Y-%m-%d %H:%M')}"}) + + mail_now_id = self.email_template.send_mail(self.group_pigs.id) + mail_in_2_days_id = self.email_template_in_2_days.send_mail(self.group_pigs.id) + + mail_now = self.env['mail.mail'].browse(mail_now_id) + mail_in_2_days = self.env['mail.mail'].browse(mail_in_2_days_id) + + # assert scheduled date are correct + self.assertEqual(bool(mail_now.scheduled_date), False) + scheduled_date = datetime.datetime.strptime(mail_in_2_days.scheduled_date, '%Y-%m-%d %H:%M') + date_in_2_days = datetime.datetime.today() + datetime.timedelta(days = 2) + self.assertEqual(scheduled_date.day, date_in_2_days.day) + self.assertEqual(scheduled_date.month, date_in_2_days.month) + self.assertEqual(scheduled_date.year, date_in_2_days.year) + + # Launch the scheduler on the first mail, it should be reported in self.mails + # and the mail_mail is now deleted + self.env['mail.mail'].process_email_queue(ids=[mail_now.id]) + self.assertTrue(len(self._mails) > 0) + + # Launch the scheduler on the first mail, it's still in 'outgoing' state + self.env['mail.mail'].process_email_queue(ids=[mail_in_2_days.id]) + self.assertEqual(mail_in_2_days.state, 'outgoing')