From d636174c859300df0b54b698576f337c397ed3ed Mon Sep 17 00:00:00 2001 From: Roberto Rosario Date: Thu, 27 Jun 2019 11:35:58 -0400 Subject: [PATCH] Add test case database connection check Signed-off-by: Roberto Rosario --- mayan/apps/common/tests/base.py | 14 ++++++++------ mayan/apps/common/tests/mixins.py | 25 ++++++++++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/mayan/apps/common/tests/base.py b/mayan/apps/common/tests/base.py index 3c63cef475..5c08bffd57 100644 --- a/mayan/apps/common/tests/base.py +++ b/mayan/apps/common/tests/base.py @@ -9,16 +9,18 @@ from mayan.apps.permissions.classes import Permission from mayan.apps.smart_settings.classes import Namespace from .mixins import ( - ClientMethodsTestCaseMixin, ContentTypeCheckTestCaseMixin, - ModelTestCaseMixin, OpenFileCheckTestCaseMixin, - RandomPrimaryKeyModelMonkeyPatchMixin, SilenceLoggerTestCaseMixin, - TempfileCheckTestCasekMixin, TestViewTestCaseMixin + ClientMethodsTestCaseMixin, ConnectionsCheckTestCaseMixin, + ContentTypeCheckTestCaseMixin, ModelTestCaseMixin, + OpenFileCheckTestCaseMixin, RandomPrimaryKeyModelMonkeyPatchMixin, + SilenceLoggerTestCaseMixin, TempfileCheckTestCasekMixin, + TestViewTestCaseMixin ) class BaseTestCase( - SilenceLoggerTestCaseMixin, RandomPrimaryKeyModelMonkeyPatchMixin, - ACLTestCaseMixin, ModelTestCaseMixin, OpenFileCheckTestCaseMixin, + SilenceLoggerTestCaseMixin, ConnectionsCheckTestCaseMixin, + RandomPrimaryKeyModelMonkeyPatchMixin, ACLTestCaseMixin, + ModelTestCaseMixin, OpenFileCheckTestCaseMixin, TempfileCheckTestCasekMixin, TestCase ): """ diff --git a/mayan/apps/common/tests/mixins.py b/mayan/apps/common/tests/mixins.py index 1060f9c4ea..afb9501fde 100644 --- a/mayan/apps/common/tests/mixins.py +++ b/mayan/apps/common/tests/mixins.py @@ -12,7 +12,7 @@ from django.apps import apps from django.conf import settings from django.conf.urls import url from django.contrib.contenttypes.models import ContentType -from django.db import connection, models +from django.db import connection, connections, models from django.db.models.signals import post_save, pre_save from django.http import HttpResponse from django.template import Context, Template @@ -80,6 +80,29 @@ class ClientMethodsTestCaseMixin(object): ) +class ConnectionsCheckTestCaseMixin(object): + _open_connections_check_enable = True + + def _get_open_connections_count(self): + return len(connections.all()) + + def setUp(self): + super(ConnectionsCheckTestCaseMixin, self).setUp() + self._connections_count = self._get_open_connections_count() + + def tearDown(self): + if self._open_connections_check_enable: + + self.assertEqual( + self._connections_count, self._get_open_connections_count(), + msg='Database connection leak. The number of database ' + 'connections at the start and at the end of the test are not ' + 'the same.' + ) + + super(ConnectionsCheckTestCaseMixin, self).tearDown() + + class ContentTypeCheckTestCaseMixin(object): expected_content_type = 'text/html; charset=utf-8'