added tests for cursor

This commit is contained in:
Arfey 2025-06-18 02:28:13 +03:00
parent 7635f5dbe0
commit 5235448ea2
2 changed files with 56 additions and 5 deletions

View file

@ -1,9 +1,11 @@
from unittest.mock import MagicMock
from django.db import DEFAULT_DB_ALIAS, async_connections
from django.test import AsyncTestCase, skipUnlessDBFeature
@skipUnlessDBFeature("supports_async")
class AsyncConnectionsTest(AsyncTestCase):
class AsyncCursorTest(AsyncTestCase):
@classmethod
async def asyncSetUpTestData(self):
@ -18,13 +20,54 @@ class AsyncConnectionsTest(AsyncTestCase):
);
""")
async def test_success(self):
async def test_execute(self):
connection = async_connections[DEFAULT_DB_ALIAS]
async with connection.cursor() as cursor:
await cursor.execute("""
INSERT INTO test_table_tmp (name) VALUES ('Test Name');
INSERT INTO test_table_tmp (name) VALUES ('1');
""")
res = await cursor.execute("""SELECT * FROM test_table_tmp;""")
data = await res.fetchone()
res = await cursor.execute("""SELECT name FROM test_table_tmp;""")
self.assertEqual(await res.fetchone(), ('1', ))
async def test_executemany(self):
connection = async_connections[DEFAULT_DB_ALIAS]
async with connection.cursor() as cursor:
await cursor.executemany(
"INSERT INTO test_table_tmp (name) VALUES (%s)",
[(1, ), (2, )]
)
res = await cursor.execute("""SELECT name FROM test_table_tmp;""")
self.assertEqual(await res.fetchall(), [('1', ), ('2', )])
async def test_iterator(self):
connection = async_connections[DEFAULT_DB_ALIAS]
async with connection.cursor() as cursor:
await cursor.execute("""
INSERT INTO test_table_tmp (name) VALUES ('1'), ('2'), ('3');
""")
await cursor.execute("""SELECT name FROM test_table_tmp;""")
self.assertEqual(
[i async for i in cursor],
[('1', ), ('2', ), ('3',)]
)
async def test_execution_wrapper(self):
connection = async_connections[DEFAULT_DB_ALIAS]
wrapper_spy = MagicMock()
def test_wrapper(execute, sql, params, many, context):
wrapper_spy(sql=sql, params=params, many=many)
return execute(sql, params, many, context)
with connection.execute_wrapper(test_wrapper):
async with connection.cursor() as cursor:
await cursor.execute("""select 1""")
wrapper_spy.assert_called_once_with(sql='select 1', params=None, many=False)

View file

@ -671,6 +671,14 @@ class AsyncDatabaseWrapperTests(AsyncTestCase):
2
)
async def test_is_usable(self):
async_connection = async_connections[DEFAULT_DB_ALIAS]
self.assertEqual(
await async_connection.is_usable(),
True
)
@skipUnlessDBFeature("supports_async")
@unittest.skipUnless(