changeset ada7c2d53eb3 in modules/marketing_email:default
details:
https://hg.tryton.org/modules/marketing_email?cmd=changeset&node=ada7c2d53eb3
description:
Replace test setuptools command by unittest discover
issue9215
review389851002
diffstat:
setup.py | 6 +-
tests/__init__.py | 8 --
tests/test_marketing_email.py | 165 ------------------------------------------
tests/test_module.py | 158 ++++++++++++++++++++++++++++++++++++++++
tox.ini | 3 +-
5 files changed, 163 insertions(+), 177 deletions(-)
diffs (379 lines):
diff -r f92a089239bd -r ada7c2d53eb3 setup.py
--- a/setup.py Mon Apr 11 23:24:21 2022 +0200
+++ b/setup.py Sat Apr 16 18:30:17 2022 +0200
@@ -136,13 +136,13 @@
license='GPL-3',
python_requires='>=3.7',
install_requires=requires,
+ extras_require={
+ 'test': tests_require,
+ },
dependency_links=dependency_links,
zip_safe=False,
entry_points="""
[trytond.modules]
marketing_email = trytond.modules.marketing_email
""",
- test_suite='tests',
- test_loader='trytond.test_loader:Loader',
- tests_require=tests_require,
)
diff -r f92a089239bd -r ada7c2d53eb3 tests/__init__.py
--- a/tests/__init__.py Mon Apr 11 23:24:21 2022 +0200
+++ b/tests/__init__.py Sat Apr 16 18:30:17 2022 +0200
@@ -1,10 +1,2 @@
# This file is part of Tryton. The COPYRIGHT file at the top level of
# this repository contains the full copyright notices and license terms.
-
-try:
- from trytond.modules.marketing_email.tests.test_marketing_email import \
- suite # noqa: E501
-except ImportError:
- from .test_marketing_email import suite
-
-__all__ = ['suite']
diff -r f92a089239bd -r ada7c2d53eb3 tests/test_marketing_email.py
--- a/tests/test_marketing_email.py Mon Apr 11 23:24:21 2022 +0200
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,165 +0,0 @@
-# This file is part of Tryton. The COPYRIGHT file at the top level of
-# this repository contains the full copyright notices and license terms.
-
-import unittest
-from unittest.mock import ANY, Mock, patch
-
-from trytond.config import config
-from trytond.modules.marketing_email import marketing as marketing_module
-from trytond.pool import Pool
-from trytond.tests.test_tryton import ModuleTestCase
-from trytond.tests.test_tryton import suite as test_suite
-from trytond.tests.test_tryton import with_transaction
-from trytond.transaction import Transaction
-
-SUBSCRIBE_URL = 'http://www.example.com/subscribe'
-UNSUBSCRIBE_URL = 'http://www.example.com/unsubscribe'
-FROM = '[email protected]'
-
-
-class MarketingEmailTestCase(ModuleTestCase):
- 'Test Marketing Email module'
- module = 'marketing_email'
-
- def setUp(self):
- super().setUp()
- if not config.has_section('marketing'):
- config.add_section('marketing')
- subscribe_url = config.get(
- 'marketing', 'email_subscribe_url', default='')
- config.set('marketing', 'email_subscribe_url', SUBSCRIBE_URL)
- self.addCleanup(
- lambda: config.set(
- 'marketing', 'email_subscribe_url', subscribe_url))
- unsubscribe_url = config.get(
- 'marketing', 'email_unsubscribe_url', default='')
- config.set('marketing', 'email_unsubscribe_url', UNSUBSCRIBE_URL)
- self.addCleanup(
- lambda: config.set(
- 'marketing', 'email_unsubscribe_url', unsubscribe_url))
- spy_pixel = config.get('marketing', 'email_spy_pixel', default='')
- config.set('marketing', 'email_spy_pixel', 'true')
- self.addCleanup(
- lambda: config.set('marketing', 'email_spy_pixel', spy_pixel))
- from_ = config.get('email', 'from', default='')
- config.set('email', 'from', FROM)
- self.addCleanup(lambda: config.set('email', 'from', from_))
-
- @with_transaction()
- def test_subscribe(self):
- "Test subscribe"
- pool = Pool()
- Email = pool.get('marketing.email')
- EmailList = pool.get('marketing.email.list')
-
- email_list = EmailList(name="Test")
- email_list.save()
-
- with patch.object(
- marketing_module, 'sendmail_transactional') as sendmail:
- email_list.request_subscribe('[email protected]')
- sendmail.assert_called_once_with(FROM, ['[email protected]'], ANY)
-
- with Transaction().set_context(active_test=False):
- email, = Email.search([
- ('list_', '=', email_list.id),
- ])
- token = email.email_token
- self.assertTrue(token)
- self.assertFalse(email.active)
-
- self.assertEqual(
- email.get_email_subscribe_url(),
- '%s?token=%s' % (SUBSCRIBE_URL, token))
-
- Email.subscribe_url(SUBSCRIBE_URL)
- self.assertFalse(email.active)
-
- Email.subscribe_url('%s?token=12345' % SUBSCRIBE_URL)
- self.assertFalse(email.active)
-
- Email.subscribe_url(email.get_email_subscribe_url())
- self.assertTrue(email.active)
-
- @with_transaction()
- def test_unsubscribe(self):
- "Test unsubscribe"
- pool = Pool()
- Email = pool.get('marketing.email')
- EmailList = pool.get('marketing.email.list')
-
- email_list = EmailList(name="Test")
- email_list.save()
-
- email = Email(email='[email protected]', list_=email_list.id)
- email.save()
-
- token = email.email_token
- self.assertTrue(token)
- self.assertTrue(email.active)
-
- with patch.object(
- marketing_module, 'sendmail_transactional') as sendmail:
- email_list.request_unsubscribe('[email protected]')
- sendmail.assert_called_once_with(FROM, ['[email protected]'], ANY)
-
- self.assertEqual(
- email.get_email_unsubscribe_url(),
- '%s?token=%s' % (UNSUBSCRIBE_URL, token))
-
- Email.unsubscribe_url(UNSUBSCRIBE_URL)
- self.assertTrue(email.active)
-
- Email.unsubscribe_url('%s?token=12345' % UNSUBSCRIBE_URL)
- self.assertTrue(email.active)
-
- Email.unsubscribe_url(email.get_email_unsubscribe_url())
- self.assertFalse(email.active)
-
- @with_transaction()
- def test_send_messages(self):
- "Test messages are sent to the list"
- pool = Pool()
- Email = pool.get('marketing.email')
- EmailList = pool.get('marketing.email.list')
- Message = pool.get('marketing.email.message')
- ShortenedURL = pool.get('web.shortened_url')
-
- email_list = EmailList(name="Test")
- email_list.save()
-
- email = Email(email='[email protected]', list_=email_list)
- email.save()
- message = Message(list_=email_list)
- message.title = 'Test'
- message.content = (
- '<html>'
- '<body>'
- '<a href="http://www.example.com/">Content</a>'
- '</body>'
- '</html>')
- message.save()
- Message.send([message])
-
- with patch.object(
- marketing_module,
- 'sendmail_transactional') as sendmail:
- smtpd_datamanager = Mock()
- Message.process(smtpd_datamanager=smtpd_datamanager)
-
- sendmail.assert_called_once_with(
- FROM, [('', '[email protected]')], ANY,
- datamanager=smtpd_datamanager)
- urls = ShortenedURL.search([
- ('record', '=', str(message)),
- ])
-
- self.assertEqual(message.state, 'sent')
- self.assertEqual(len(urls), 2)
-
-
-def suite():
- suite = test_suite()
- suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
- MarketingEmailTestCase))
- return suite
diff -r f92a089239bd -r ada7c2d53eb3 tests/test_module.py
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/tests/test_module.py Sat Apr 16 18:30:17 2022 +0200
@@ -0,0 +1,158 @@
+# This file is part of Tryton. The COPYRIGHT file at the top level of
+# this repository contains the full copyright notices and license terms.
+
+from unittest.mock import ANY, Mock, patch
+
+from trytond.config import config
+from trytond.modules.marketing_email import marketing as marketing_module
+from trytond.pool import Pool
+from trytond.tests.test_tryton import ModuleTestCase, with_transaction
+from trytond.transaction import Transaction
+
+SUBSCRIBE_URL = 'http://www.example.com/subscribe'
+UNSUBSCRIBE_URL = 'http://www.example.com/unsubscribe'
+FROM = '[email protected]'
+
+
+class MarketingEmailTestCase(ModuleTestCase):
+ 'Test Marketing Email module'
+ module = 'marketing_email'
+
+ def setUp(self):
+ super().setUp()
+ if not config.has_section('marketing'):
+ config.add_section('marketing')
+ subscribe_url = config.get(
+ 'marketing', 'email_subscribe_url', default='')
+ config.set('marketing', 'email_subscribe_url', SUBSCRIBE_URL)
+ self.addCleanup(
+ lambda: config.set(
+ 'marketing', 'email_subscribe_url', subscribe_url))
+ unsubscribe_url = config.get(
+ 'marketing', 'email_unsubscribe_url', default='')
+ config.set('marketing', 'email_unsubscribe_url', UNSUBSCRIBE_URL)
+ self.addCleanup(
+ lambda: config.set(
+ 'marketing', 'email_unsubscribe_url', unsubscribe_url))
+ spy_pixel = config.get('marketing', 'email_spy_pixel', default='')
+ config.set('marketing', 'email_spy_pixel', 'true')
+ self.addCleanup(
+ lambda: config.set('marketing', 'email_spy_pixel', spy_pixel))
+ from_ = config.get('email', 'from', default='')
+ config.set('email', 'from', FROM)
+ self.addCleanup(lambda: config.set('email', 'from', from_))
+
+ @with_transaction()
+ def test_subscribe(self):
+ "Test subscribe"
+ pool = Pool()
+ Email = pool.get('marketing.email')
+ EmailList = pool.get('marketing.email.list')
+
+ email_list = EmailList(name="Test")
+ email_list.save()
+
+ with patch.object(
+ marketing_module, 'sendmail_transactional') as sendmail:
+ email_list.request_subscribe('[email protected]')
+ sendmail.assert_called_once_with(FROM, ['[email protected]'], ANY)
+
+ with Transaction().set_context(active_test=False):
+ email, = Email.search([
+ ('list_', '=', email_list.id),
+ ])
+ token = email.email_token
+ self.assertTrue(token)
+ self.assertFalse(email.active)
+
+ self.assertEqual(
+ email.get_email_subscribe_url(),
+ '%s?token=%s' % (SUBSCRIBE_URL, token))
+
+ Email.subscribe_url(SUBSCRIBE_URL)
+ self.assertFalse(email.active)
+
+ Email.subscribe_url('%s?token=12345' % SUBSCRIBE_URL)
+ self.assertFalse(email.active)
+
+ Email.subscribe_url(email.get_email_subscribe_url())
+ self.assertTrue(email.active)
+
+ @with_transaction()
+ def test_unsubscribe(self):
+ "Test unsubscribe"
+ pool = Pool()
+ Email = pool.get('marketing.email')
+ EmailList = pool.get('marketing.email.list')
+
+ email_list = EmailList(name="Test")
+ email_list.save()
+
+ email = Email(email='[email protected]', list_=email_list.id)
+ email.save()
+
+ token = email.email_token
+ self.assertTrue(token)
+ self.assertTrue(email.active)
+
+ with patch.object(
+ marketing_module, 'sendmail_transactional') as sendmail:
+ email_list.request_unsubscribe('[email protected]')
+ sendmail.assert_called_once_with(FROM, ['[email protected]'], ANY)
+
+ self.assertEqual(
+ email.get_email_unsubscribe_url(),
+ '%s?token=%s' % (UNSUBSCRIBE_URL, token))
+
+ Email.unsubscribe_url(UNSUBSCRIBE_URL)
+ self.assertTrue(email.active)
+
+ Email.unsubscribe_url('%s?token=12345' % UNSUBSCRIBE_URL)
+ self.assertTrue(email.active)
+
+ Email.unsubscribe_url(email.get_email_unsubscribe_url())
+ self.assertFalse(email.active)
+
+ @with_transaction()
+ def test_send_messages(self):
+ "Test messages are sent to the list"
+ pool = Pool()
+ Email = pool.get('marketing.email')
+ EmailList = pool.get('marketing.email.list')
+ Message = pool.get('marketing.email.message')
+ ShortenedURL = pool.get('web.shortened_url')
+
+ email_list = EmailList(name="Test")
+ email_list.save()
+
+ email = Email(email='[email protected]', list_=email_list)
+ email.save()
+ message = Message(list_=email_list)
+ message.title = 'Test'
+ message.content = (
+ '<html>'
+ '<body>'
+ '<a href="http://www.example.com/">Content</a>'
+ '</body>'
+ '</html>')
+ message.save()
+ Message.send([message])
+
+ with patch.object(
+ marketing_module,
+ 'sendmail_transactional') as sendmail:
+ smtpd_datamanager = Mock()
+ Message.process(smtpd_datamanager=smtpd_datamanager)
+
+ sendmail.assert_called_once_with(
+ FROM, [('', '[email protected]')], ANY,
+ datamanager=smtpd_datamanager)
+ urls = ShortenedURL.search([
+ ('record', '=', str(message)),
+ ])
+
+ self.assertEqual(message.state, 'sent')
+ self.assertEqual(len(urls), 2)
+
+
+del ModuleTestCase
diff -r f92a089239bd -r ada7c2d53eb3 tox.ini
--- a/tox.ini Mon Apr 11 23:24:21 2022 +0200
+++ b/tox.ini Sat Apr 16 18:30:17 2022 +0200
@@ -2,8 +2,9 @@
envlist = {py37,py38,py39,py310}-{sqlite,postgresql}
[testenv]
+extras = test
commands =
- coverage run --include=.*/marketing_email/* setup.py test
+ coverage run --include=.*/marketing_email/* -m unittest discover -s tests
coverage report --include=.*/marketing_email/* --omit=*/tests/*
deps =
coverage