From 30f2d1af03b1f1e981e8e15151079795914f8f2c Mon Sep 17 00:00:00 2001 From: Mariusz Felisiak Date: Tue, 8 Dec 2020 07:05:49 +0100 Subject: [PATCH] Refs #32233 -- Added tests for nonexistent cache and databases aliases. --- tests/cache/tests.py | 5 +++++ tests/db_utils/tests.py | 28 +++++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/tests/cache/tests.py b/tests/cache/tests.py index 367d2d7119c..dd4d441ad13 100644 --- a/tests/cache/tests.py +++ b/tests/cache/tests.py @@ -2499,3 +2499,8 @@ class CacheHandlerTest(SimpleTestCase): t.join() self.assertIsNot(c[0], c[1]) + + def test_nonexistent_alias(self): + msg = "Could not find config for 'nonexistent' in settings.CACHES" + with self.assertRaisesMessage(InvalidCacheBackendError, msg): + caches['nonexistent'] diff --git a/tests/db_utils/tests.py b/tests/db_utils/tests.py index 3ce8e82cd30..ca40d4d1e62 100644 --- a/tests/db_utils/tests.py +++ b/tests/db_utils/tests.py @@ -3,7 +3,9 @@ import unittest from django.core.exceptions import ImproperlyConfigured from django.db import DEFAULT_DB_ALIAS, ProgrammingError, connection -from django.db.utils import ConnectionHandler, load_backend +from django.db.utils import ( + ConnectionDoesNotExist, ConnectionHandler, load_backend, +) from django.test import SimpleTestCase, TestCase @@ -38,6 +40,30 @@ class ConnectionHandlerTests(SimpleTestCase): with self.assertRaisesMessage(ImproperlyConfigured, msg): conns['other'].ensure_connection() + def test_nonexistent_alias(self): + msg = "The connection nonexistent doesn't exist" + conns = ConnectionHandler({ + DEFAULT_DB_ALIAS: {'ENGINE': 'django.db.backends.dummy'}, + }) + with self.assertRaisesMessage(ConnectionDoesNotExist, msg): + conns['nonexistent'] + + def test_ensure_defaults_nonexistent_alias(self): + msg = "The connection nonexistent doesn't exist" + conns = ConnectionHandler({ + DEFAULT_DB_ALIAS: {'ENGINE': 'django.db.backends.dummy'}, + }) + with self.assertRaisesMessage(ConnectionDoesNotExist, msg): + conns.ensure_defaults('nonexistent') + + def test_prepare_test_settings_nonexistent_alias(self): + msg = "The connection nonexistent doesn't exist" + conns = ConnectionHandler({ + DEFAULT_DB_ALIAS: {'ENGINE': 'django.db.backends.dummy'}, + }) + with self.assertRaisesMessage(ConnectionDoesNotExist, msg): + conns.prepare_test_settings('nonexistent') + class DatabaseErrorWrapperTests(TestCase):