changeset cfe1940ebc6e in modules/authentication_sms:default
details: 
https://hg.tryton.org/modules/authentication_sms?cmd=changeset&node=cfe1940ebc6e
description:
        Replace test setuptools command by unittest discover

        issue9215
        review389851002
diffstat:

 setup.py                         |    6 +-
 tests/__init__.py                |    9 +--
 tests/test_authentication_sms.py |  124 ---------------------------------------
 tests/test_module.py             |  118 +++++++++++++++++++++++++++++++++++++
 tox.ini                          |    3 +-
 5 files changed, 125 insertions(+), 135 deletions(-)

diffs (300 lines):

diff -r 35f93cf0468a -r cfe1940ebc6e setup.py
--- a/setup.py  Sun Apr 10 19:11:38 2022 +0200
+++ b/setup.py  Sat Apr 16 18:30:17 2022 +0200
@@ -137,13 +137,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]
     authentication_sms = trytond.modules.authentication_sms
     """,
-    test_suite='tests',
-    test_loader='trytond.test_loader:Loader',
-    tests_require=tests_require,
     )
diff -r 35f93cf0468a -r cfe1940ebc6e tests/__init__.py
--- a/tests/__init__.py Sun Apr 10 19:11:38 2022 +0200
+++ b/tests/__init__.py Sat Apr 16 18:30:17 2022 +0200
@@ -1,10 +1,5 @@
 # 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 .test_module import send_sms
 
-try:
-    from trytond.modules.authentication_sms.tests.test_authentication_sms 
import (  # noqa: E501
-        send_sms, suite)
-except ImportError:
-    from .test_authentication_sms import send_sms, suite
-
-__all__ = ['suite', 'send_sms']
+__all__ = ['send_sms']
diff -r 35f93cf0468a -r cfe1940ebc6e tests/test_authentication_sms.py
--- a/tests/test_authentication_sms.py  Sun Apr 10 19:11:38 2022 +0200
+++ /dev/null   Thu Jan 01 00:00:00 1970 +0000
@@ -1,124 +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 datetime
-import unittest
-
-from trytond.config import config
-from trytond.exceptions import LoginException
-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
-
-
-def send_sms(text, to, from_):
-    sms_queue.append({
-            'text': text,
-            'to': to,
-            'from': from_,
-            })
-
-
-sms_queue = []
-
-
-class AuthenticationSMSTestCase(ModuleTestCase):
-    'Test Authentication SMS module'
-    module = 'authentication_sms'
-
-    def setUp(self):
-        super(AuthenticationSMSTestCase, self).setUp()
-        methods = config.get('session', 'authentications', default='')
-        config.set('session', 'authentications', 'sms')
-        self.addCleanup(config.set, 'session', 'authentications', methods)
-        config.add_section('authentication_sms')
-        config.set(
-            'authentication_sms', 'function',
-            'trytond.modules.authentication_sms.tests.send_sms')
-        self.addCleanup(config.remove_section, 'authentication_sms')
-        del sms_queue[:]
-
-    @with_transaction()
-    def test_sms_code_default_code(self):
-        pool = Pool()
-        SMSCode = pool.get('res.user.login.sms_code')
-        code = SMSCode.default_code()
-        self.assertEqual(len(code), 6)
-
-    @with_transaction()
-    def test_sms_code_get(self):
-        pool = Pool()
-        SMSCode = pool.get('res.user.login.sms_code')
-
-        record, = SMSCode.create([{'user_id': 1}])
-
-        records = list(SMSCode.get(1))
-        self.assertEqual(records, [record])
-
-        future = datetime.datetime.now() + datetime.timedelta(10 * 60)
-        records = list(SMSCode.get(1, _now=future))
-        self.assertFalse(records)
-        self.assertFalse(SMSCode.search([]))
-
-    @with_transaction()
-    def test_sms_code_send(self):
-        pool = Pool()
-        User = pool.get('res.user')
-        SMSCode = pool.get('res.user.login.sms_code')
-
-        user = User(name='sms', login='sms', mobile='+123456789')
-        user.save()
-
-        SMSCode.send(user.id)
-        record, = SMSCode.search([])
-        self.assertEqual(len(sms_queue), 1)
-        sms, = sms_queue
-        self.assertEqual(record.user_id, user.id)
-        self.assertIn(record.code, sms['text'])
-        self.assertEqual(user.mobile, sms['to'])
-
-        # Don't send a second SMS as long as the first is valid
-        SMSCode.send(user.id)
-        self.assertEqual(len(sms_queue), 1)
-
-    @with_transaction()
-    def test_sms_code_check(self):
-        pool = Pool()
-        SMSCode = pool.get('res.user.login.sms_code')
-
-        record, = SMSCode.create([{'user_id': 1}])
-        sms_code = record.code
-
-        self.assertFalse(SMSCode.check(1, 'foo'))
-        self.assertTrue(SMSCode.check(1, sms_code))
-        # Second check should fail
-        self.assertFalse(SMSCode.check(1, sms_code))
-
-    @with_transaction()
-    def test_user_get_login(self):
-        pool = Pool()
-        User = pool.get('res.user')
-        SMSCode = pool.get('res.user.login.sms_code')
-
-        user = User(name='sms', login='sms', mobile='+123456789')
-        user.save()
-
-        with self.assertRaises(LoginException) as cm:
-            User.get_login('sms', {})
-        self.assertEqual(cm.exception.name, 'sms_code')
-        self.assertEqual(cm.exception.type, 'char')
-
-        record, = SMSCode.search([])
-        sms_code = record.code
-
-        user_id = User.get_login('sms', {
-                'sms_code': sms_code,
-                })
-        self.assertEqual(user_id, user.id)
-
-
-def suite():
-    suite = test_suite()
-    suite.addTests(unittest.TestLoader().loadTestsFromTestCase(
-            AuthenticationSMSTestCase))
-    return suite
diff -r 35f93cf0468a -r cfe1940ebc6e 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,118 @@
+# 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 datetime
+
+from trytond.config import config
+from trytond.exceptions import LoginException
+from trytond.pool import Pool
+from trytond.tests.test_tryton import ModuleTestCase
+from trytond.tests.test_tryton import with_transaction
+
+
+def send_sms(text, to, from_):
+    sms_queue.append({
+            'text': text,
+            'to': to,
+            'from': from_,
+            })
+
+
+sms_queue = []
+
+
+class AuthenticationSMSTestCase(ModuleTestCase):
+    'Test Authentication SMS module'
+    module = 'authentication_sms'
+
+    def setUp(self):
+        super(AuthenticationSMSTestCase, self).setUp()
+        methods = config.get('session', 'authentications', default='')
+        config.set('session', 'authentications', 'sms')
+        self.addCleanup(config.set, 'session', 'authentications', methods)
+        config.add_section('authentication_sms')
+        config.set(
+            'authentication_sms', 'function',
+            'trytond.modules.authentication_sms.tests.send_sms')
+        self.addCleanup(config.remove_section, 'authentication_sms')
+        del sms_queue[:]
+
+    @with_transaction()
+    def test_sms_code_default_code(self):
+        pool = Pool()
+        SMSCode = pool.get('res.user.login.sms_code')
+        code = SMSCode.default_code()
+        self.assertEqual(len(code), 6)
+
+    @with_transaction()
+    def test_sms_code_get(self):
+        pool = Pool()
+        SMSCode = pool.get('res.user.login.sms_code')
+
+        record, = SMSCode.create([{'user_id': 1}])
+
+        records = list(SMSCode.get(1))
+        self.assertEqual(records, [record])
+
+        future = datetime.datetime.now() + datetime.timedelta(10 * 60)
+        records = list(SMSCode.get(1, _now=future))
+        self.assertFalse(records)
+        self.assertFalse(SMSCode.search([]))
+
+    @with_transaction()
+    def test_sms_code_send(self):
+        pool = Pool()
+        User = pool.get('res.user')
+        SMSCode = pool.get('res.user.login.sms_code')
+
+        user = User(name='sms', login='sms', mobile='+123456789')
+        user.save()
+
+        SMSCode.send(user.id)
+        record, = SMSCode.search([])
+        self.assertEqual(len(sms_queue), 1)
+        sms, = sms_queue
+        self.assertEqual(record.user_id, user.id)
+        self.assertIn(record.code, sms['text'])
+        self.assertEqual(user.mobile, sms['to'])
+
+        # Don't send a second SMS as long as the first is valid
+        SMSCode.send(user.id)
+        self.assertEqual(len(sms_queue), 1)
+
+    @with_transaction()
+    def test_sms_code_check(self):
+        pool = Pool()
+        SMSCode = pool.get('res.user.login.sms_code')
+
+        record, = SMSCode.create([{'user_id': 1}])
+        sms_code = record.code
+
+        self.assertFalse(SMSCode.check(1, 'foo'))
+        self.assertTrue(SMSCode.check(1, sms_code))
+        # Second check should fail
+        self.assertFalse(SMSCode.check(1, sms_code))
+
+    @with_transaction()
+    def test_user_get_login(self):
+        pool = Pool()
+        User = pool.get('res.user')
+        SMSCode = pool.get('res.user.login.sms_code')
+
+        user = User(name='sms', login='sms', mobile='+123456789')
+        user.save()
+
+        with self.assertRaises(LoginException) as cm:
+            User.get_login('sms', {})
+        self.assertEqual(cm.exception.name, 'sms_code')
+        self.assertEqual(cm.exception.type, 'char')
+
+        record, = SMSCode.search([])
+        sms_code = record.code
+
+        user_id = User.get_login('sms', {
+                'sms_code': sms_code,
+                })
+        self.assertEqual(user_id, user.id)
+
+
+del ModuleTestCase
diff -r 35f93cf0468a -r cfe1940ebc6e tox.ini
--- a/tox.ini   Sun Apr 10 19:11:38 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=.*/authentication_sms/* setup.py test
+    coverage run --include=.*/authentication_sms/* -m unittest discover -s 
tests
     coverage report --include=.*/authentication_sms/* --omit=*/tests/*
 deps =
     coverage

Reply via email to