summaryrefslogtreecommitdiff
path: root/tests/backends/sqlite
diff options
context:
space:
mode:
authorMariusz Felisiak <felisiak.mariusz@gmail.com>2023-08-23 09:09:23 +0200
committerGitHub <noreply@github.com>2023-08-23 09:09:23 +0200
commitdd45d5223b3c5640baefcb591782bbcff873b6bf (patch)
treecc5f61b2c82e6a1f0c605e91f788dc7b39f4806f /tests/backends/sqlite
parenta9e0f3d3014461c2199123721899162c0876959d (diff)
Fixed ResourceWarning from unclosed SQLite connection on Python 3.13+.
- backends.sqlite.tests.ThreadSharing.test_database_sharing_in_threads - backends.tests.ThreadTests.test_default_connection_thread_local: on SQLite, close() doesn't explicitly close in-memory connections. - servers.tests.LiveServerInMemoryDatabaseLockTest - test_runner.tests.SQLiteInMemoryTestDbs.test_transaction_support Check out https://github.com/python/cpython/pull/108015.
Diffstat (limited to 'tests/backends/sqlite')
-rw-r--r--tests/backends/sqlite/tests.py27
1 files changed, 21 insertions, 6 deletions
diff --git a/tests/backends/sqlite/tests.py b/tests/backends/sqlite/tests.py
index 2b728f8409..330ed50488 100644
--- a/tests/backends/sqlite/tests.py
+++ b/tests/backends/sqlite/tests.py
@@ -6,7 +6,13 @@ import unittest
from pathlib import Path
from unittest import mock
-from django.db import NotSupportedError, connection, transaction
+from django.db import (
+ DEFAULT_DB_ALIAS,
+ NotSupportedError,
+ connection,
+ connections,
+ transaction,
+)
from django.db.models import Aggregate, Avg, StdDev, Sum, Variance
from django.db.utils import ConnectionHandler
from django.test import TestCase, TransactionTestCase, override_settings
@@ -222,11 +228,20 @@ class ThreadSharing(TransactionTestCase):
available_apps = ["backends"]
def test_database_sharing_in_threads(self):
+ thread_connections = []
+
def create_object():
Object.objects.create()
+ thread_connections.append(connections[DEFAULT_DB_ALIAS].connection)
- create_object()
- thread = threading.Thread(target=create_object)
- thread.start()
- thread.join()
- self.assertEqual(Object.objects.count(), 2)
+ main_connection = connections[DEFAULT_DB_ALIAS].connection
+ try:
+ create_object()
+ thread = threading.Thread(target=create_object)
+ thread.start()
+ thread.join()
+ self.assertEqual(Object.objects.count(), 2)
+ finally:
+ for conn in thread_connections:
+ if conn is not main_connection:
+ conn.close()