Django unittest without connecting to a database

I am trying to write unittest, which will check if the correct error message is returned if the database connection is thrown into an exception. I tried to use connection.creation.destroy_test_db(':memory:') , but this did not work as I expected. I suppose I need to either delete the tables or somehow cut off the db connection. Is it possible?

+4
source share
3 answers

I found my answer in the presentation Testing and Django by Carl Meyer . Here is how I did it:

 from django.db import DatabaseError from django.test import TestCase from django.test.client import Client import mock class NoDBTest(TestCase): cursor_wrapper = mock.Mock() cursor_wrapper.side_effect = DatabaseError @mock.patch("django.db.backends.util.CursorWrapper", cursor_wrapper) def test_no_database_connection(self): response = self.client.post('/signup/', form_data) self.assertEqual(message, 'An error occured with the DB') 
+4
source

This seems like a mockery job. For example, if you use MySQL, you can put the side_effect on connect method, for example:

 from django.test import TestCase from mock import patch import MySQLdb class DBTestCase(TestCase): def test_connection_error(self): with patch.object(MySQLdb, 'connect') as connect_method: connect_method.side_effect = Exception("Database Connection Error") # your assertions here 

Hope this helps.

+3
source

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) 

401 Unauthorized http cat

+2
source

Source: https://habr.com/ru/post/1480336/


All Articles