I was looking for the actual django response code in case of a database connection timeout when using pymysql . The following test confirmed that a 401 Unauthorized when pymysql raises an OperationalError .
from unittest.mock import patch import pymysql from django.test import TestCase, Client class TestDatabaseOutage(TestCase): client = None def setUp(self): self.client = Client() def test_database_connection_timeout_returns_401(self): with patch.object(pymysql, 'connect') as connect_method: message = "Can't connect to MySQL server on 'some_database.example.com' ([Errno 110] Connection timed out)" connect_method.side_effect = pymysql.OperationalError(2003, message) response = self.client.get('/') self.assertEqual(response.status_code, 401)

source share