diff --git a/django/test/utils.py b/django/test/utils.py
index 619f9cd..a79b9f3 100644
a
|
b
|
def captured_stdin():
|
541 | 541 | self.assertEqual(captured, "hello") |
542 | 542 | """ |
543 | 543 | return captured_output("stdin") |
| 544 | |
| 545 | |
| 546 | def reset_warning_registry(pattern=".*"): |
| 547 | """clear warning registry for all match modules. |
| 548 | This fixes unpexpected behavior from tests when running |
| 549 | with --reverse option""" |
| 550 | key = "__warningregistry__" |
| 551 | for mod in sys.modules.values(): |
| 552 | if hasattr(mod, key) and re.match(pattern, mod.__name__): |
| 553 | getattr(mod, key).clear() |
diff --git a/tests/cache/tests.py b/tests/cache/tests.py
index e3ad724..5140a73 100644
a
|
b
|
class GetCacheTests(IgnoreDeprecationWarningsMixin, TestCase):
|
1237 | 1237 | |
1238 | 1238 | def test_close(self): |
1239 | 1239 | from django.core import signals |
| 1240 | from django.core.cache import get_cache |
| 1241 | cache = get_cache('cache.closeable_cache.CacheClass') |
1240 | 1242 | self.assertFalse(cache.closed) |
1241 | 1243 | signals.request_finished.send(self.__class__) |
1242 | 1244 | self.assertTrue(cache.closed) |
… |
… |
class CacheUtils(TestCase):
|
1378 | 1380 | def test_get_cache_key(self): |
1379 | 1381 | request = self.factory.get(self.path) |
1380 | 1382 | response = HttpResponse() |
1381 | | key_prefix = 'localprefix' |
1382 | 1383 | # Expect None if no headers have been set yet. |
1383 | | self.assertIsNone(get_cache_key(request)) |
| 1384 | # it was missing the response parameter. |
| 1385 | self.assertIsNone(get_cache_key(request, response)) |
1384 | 1386 | # Set headers to an empty list. |
1385 | 1387 | learn_cache_key(request, response) |
1386 | 1388 | |
… |
… |
class CacheUtils(TestCase):
|
1390 | 1392 | '18a03f9c9649f7d684af5db3524f5c99.d41d8cd98f00b204e9800998ecf8427e' |
1391 | 1393 | ) |
1392 | 1394 | # Verify that a specified key_prefix is taken into account. |
| 1395 | key_prefix = 'localprefix' |
1393 | 1396 | learn_cache_key(request, response, key_prefix=key_prefix) |
1394 | 1397 | self.assertEqual( |
1395 | 1398 | get_cache_key(request, key_prefix=key_prefix), |
diff --git a/tests/deprecation/tests.py b/tests/deprecation/tests.py
index 79a6284..e0fbe12 100644
a
|
b
|
import unittest
|
5 | 5 | import warnings |
6 | 6 | |
7 | 7 | from django.test import SimpleTestCase, RequestFactory, override_settings |
| 8 | from django.test.utils import reset_warning_registry |
8 | 9 | from django.utils import six, translation |
9 | 10 | from django.utils.deprecation import RenameMethodsBase |
10 | 11 | from django.utils.encoding import force_text |
… |
… |
class RenameMethodsTests(SimpleTestCase):
|
23 | 24 | to `get_queryset` across the code base following #15363. |
24 | 25 | """ |
25 | 26 | |
| 27 | def setUp(self): |
| 28 | super(RenameMethodsTests, self).setUp() |
| 29 | reset_warning_registry() |
| 30 | |
26 | 31 | def test_class_definition_warnings(self): |
27 | 32 | """ |
28 | 33 | Ensure a warning is raised upon class definition to suggest renaming |
diff --git a/tests/mail/tests.py b/tests/mail/tests.py
index e4b5689..6173ea5 100644
a
|
b
|
class SMTPBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
966 | 966 | backend.close() |
967 | 967 | self.assertTrue(opened) |
968 | 968 | |
969 | | def test_server_stopped(self): |
970 | | """ |
971 | | Test that closing the backend while the SMTP server is stopped doesn't |
972 | | raise an exception. |
973 | | """ |
974 | | backend = smtp.EmailBackend(username='', password='') |
975 | | backend.open() |
976 | | self.server.stop() |
977 | | try: |
978 | | backend.close() |
979 | | except Exception as e: |
980 | | self.fail("close() unexpectedly raised an exception: %s" % e) |
981 | | |
982 | 969 | @override_settings(EMAIL_USE_TLS=True) |
983 | 970 | def test_email_tls_use_settings(self): |
984 | 971 | backend = smtp.EmailBackend() |
… |
… |
class SMTPBackendTests(BaseEmailBackendTests, SimpleTestCase):
|
1110 | 1097 | |
1111 | 1098 | finally: |
1112 | 1099 | SMTP.send = send |
| 1100 | |
| 1101 | |
| 1102 | class SMTPBackendStoppedServerTest(SimpleTestCase): |
| 1103 | email_backend = 'django.core.mail.backends.smtp.EmailBackend' |
| 1104 | |
| 1105 | @classmethod |
| 1106 | def setUpClass(cls): |
| 1107 | super(SMTPBackendStoppedTest, cls).setUpClass() |
| 1108 | cls.server = FakeSMTPServer(('127.0.0.1', 0), None) |
| 1109 | cls._settings_override = override_settings( |
| 1110 | EMAIL_HOST="127.0.0.1", |
| 1111 | EMAIL_PORT=cls.server.socket.getsockname()[1]) |
| 1112 | cls._settings_override.enable() |
| 1113 | cls.server.start() |
| 1114 | |
| 1115 | @classmethod |
| 1116 | def tearDownClass(cls): |
| 1117 | cls._settings_override.disable() |
| 1118 | cls.server.stop() |
| 1119 | super(SMTPBackendStoppedTest, cls).tearDownClass() |
| 1120 | |
| 1121 | def test_server_stopped(self): |
| 1122 | """ |
| 1123 | Test that closing the backend while the SMTP server is stopped doesn't |
| 1124 | raise an exception. |
| 1125 | """ |
| 1126 | backend = smtp.EmailBackend(username='', password='') |
| 1127 | backend.open() |
| 1128 | self.server.stop() |
| 1129 | try: |
| 1130 | backend.close() |
| 1131 | except Exception as e: |
| 1132 | self.fail("close() unexpectedly raised an exception: %s" % e) |
diff --git a/tests/modeladmin/tests.py b/tests/modeladmin/tests.py
index 93cb58a..9085f68 100644
a
|
b
|
class ListDisplayEditableTests(CheckTestCase):
|
1546 | 1546 | |
1547 | 1547 | class ModelAdminPermissionTests(TestCase): |
1548 | 1548 | |
| 1549 | def setUp(self): |
| 1550 | """ |
| 1551 | When running tests with --reverse, the last test changes the app label, |
| 1552 | what interferes with the following results |
| 1553 | """ |
| 1554 | self.model_admin = ModelAdmin(Band, AdminSite()) |
| 1555 | self.model_admin.opts.app_label = 'modeladmin' |
| 1556 | |
1549 | 1557 | class MockUser(object): |
1550 | 1558 | def has_module_perms(self, app_label): |
1551 | 1559 | if app_label == "modeladmin": |
… |
… |
class ModelAdminPermissionTests(TestCase):
|
1575 | 1583 | Ensure that has_add_permission returns True for users who can add |
1576 | 1584 | objects and False for users who can't. |
1577 | 1585 | """ |
1578 | | ma = ModelAdmin(Band, AdminSite()) |
1579 | 1586 | request = MockRequest() |
1580 | 1587 | request.user = self.MockAddUser() |
1581 | | self.assertTrue(ma.has_add_permission(request)) |
| 1588 | self.assertTrue(self.model_admin.has_add_permission(request)) |
1582 | 1589 | request.user = self.MockChangeUser() |
1583 | | self.assertFalse(ma.has_add_permission(request)) |
| 1590 | self.assertFalse(self.model_admin.has_add_permission(request)) |
1584 | 1591 | request.user = self.MockDeleteUser() |
1585 | | self.assertFalse(ma.has_add_permission(request)) |
| 1592 | self.assertFalse(self.model_admin.has_add_permission(request)) |
1586 | 1593 | |
1587 | 1594 | def test_has_change_permission(self): |
1588 | 1595 | """ |
1589 | 1596 | Ensure that has_change_permission returns True for users who can edit |
1590 | 1597 | objects and False for users who can't. |
1591 | 1598 | """ |
1592 | | ma = ModelAdmin(Band, AdminSite()) |
1593 | 1599 | request = MockRequest() |
1594 | 1600 | request.user = self.MockAddUser() |
1595 | | self.assertFalse(ma.has_change_permission(request)) |
| 1601 | self.assertFalse(self.model_admin.has_change_permission(request)) |
1596 | 1602 | request.user = self.MockChangeUser() |
1597 | | self.assertTrue(ma.has_change_permission(request)) |
| 1603 | self.assertTrue(self.model_admin.has_change_permission(request)) |
1598 | 1604 | request.user = self.MockDeleteUser() |
1599 | | self.assertFalse(ma.has_change_permission(request)) |
| 1605 | self.assertFalse(self.model_admin.has_change_permission(request)) |
1600 | 1606 | |
1601 | 1607 | def test_has_delete_permission(self): |
1602 | 1608 | """ |
1603 | 1609 | Ensure that has_delete_permission returns True for users who can delete |
1604 | 1610 | objects and False for users who can't. |
1605 | 1611 | """ |
1606 | | ma = ModelAdmin(Band, AdminSite()) |
1607 | 1612 | request = MockRequest() |
1608 | 1613 | request.user = self.MockAddUser() |
1609 | | self.assertFalse(ma.has_delete_permission(request)) |
| 1614 | self.assertFalse(self.model_admin.has_delete_permission(request)) |
1610 | 1615 | request.user = self.MockChangeUser() |
1611 | | self.assertFalse(ma.has_delete_permission(request)) |
| 1616 | self.assertFalse(self.model_admin.has_delete_permission(request)) |
1612 | 1617 | request.user = self.MockDeleteUser() |
1613 | | self.assertTrue(ma.has_delete_permission(request)) |
| 1618 | self.assertTrue(self.model_admin.has_delete_permission(request)) |
1614 | 1619 | |
1615 | 1620 | def test_has_module_permission(self): |
1616 | 1621 | """ |
1617 | 1622 | Ensure that has_module_permission returns True for users who have any |
1618 | 1623 | permission for the module and False for users who don't. |
1619 | 1624 | """ |
1620 | | ma = ModelAdmin(Band, AdminSite()) |
1621 | 1625 | request = MockRequest() |
1622 | 1626 | request.user = self.MockAddUser() |
1623 | | self.assertTrue(ma.has_module_permission(request)) |
| 1627 | self.assertTrue(self.model_admin.has_module_permission(request)) |
1624 | 1628 | request.user = self.MockChangeUser() |
1625 | | self.assertTrue(ma.has_module_permission(request)) |
| 1629 | self.assertTrue(self.model_admin.has_module_permission(request)) |
1626 | 1630 | request.user = self.MockDeleteUser() |
1627 | | self.assertTrue(ma.has_module_permission(request)) |
1628 | | ma.opts.app_label = "anotherapp" |
| 1631 | self.assertTrue(self.model_admin.has_module_permission(request)) |
| 1632 | self.model_admin.opts.app_label = 'anotherapp' |
1629 | 1633 | request.user = self.MockAddUser() |
1630 | | self.assertFalse(ma.has_module_permission(request)) |
| 1634 | self.assertFalse(self.model_admin.has_module_permission(request)) |
1631 | 1635 | request.user = self.MockChangeUser() |
1632 | | self.assertFalse(ma.has_module_permission(request)) |
| 1636 | self.assertFalse(self.model_admin.has_module_permission(request)) |
1633 | 1637 | request.user = self.MockDeleteUser() |
1634 | | self.assertFalse(ma.has_module_permission(request)) |
| 1638 | self.assertFalse(self.model_admin.has_module_permission(request)) |
diff --git a/tests/utils_tests/test_text.py b/tests/utils_tests/test_text.py
index 4ef3031..a43870f 100644
a
|
b
|
from unittest import skipUnless
|
5 | 5 | import warnings |
6 | 6 | |
7 | 7 | from django.test import SimpleTestCase |
| 8 | from django.test.utils import reset_warning_registry |
8 | 9 | from django.utils import six, text |
9 | 10 | from django.utils.deprecation import RemovedInDjango19Warning |
10 | 11 | from django.utils.encoding import force_text |
… |
… |
IS_WIDE_BUILD = (len('\U0001F4A9') == 1)
|
18 | 19 | |
19 | 20 | class TestUtilsText(SimpleTestCase): |
20 | 21 | |
| 22 | def setUp(self): |
| 23 | super(TestUtilsText, self).setUp() |
| 24 | reset_warning_registry() |
| 25 | |
21 | 26 | def test_get_text_list(self): |
22 | 27 | self.assertEqual(text.get_text_list(['a', 'b', 'c', 'd']), 'a, b, c or d') |
23 | 28 | self.assertEqual(text.get_text_list(['a', 'b', 'c'], 'and'), 'a, b and c') |