Skip to content

Commit 6e68738

Browse files
author
Eugene Shershen
committed
make execute and executemany synchronous; update tests to reflect changes; add await_ adaptation
1 parent dab5dbb commit 6e68738

File tree

2 files changed

+22
-44
lines changed

2 files changed

+22
-44
lines changed

psqlpy_sqlalchemy/connection.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ def __init__(
4545
) -> None:
4646
self._adapt_connection = adapt_connection
4747
self._connection = adapt_connection._connection
48+
self.await_ = adapt_connection.await_
4849
self._rows: deque[t.Any] = deque()
4950
self._description: t.Optional[t.List[t.Tuple[t.Any, ...]]] = None
5051
self._arraysize = 1
@@ -350,19 +351,19 @@ async def _executemany(
350351
True,
351352
)
352353

353-
async def execute(
354+
def execute(
354355
self,
355356
operation: t.Any,
356357
parameters: t.Union[
357358
t.Sequence[t.Any], t.Mapping[str, Any], None
358359
] = None,
359360
) -> None:
360-
await self._prepare_execute(operation, parameters)
361+
self.await_(self._prepare_execute(operation, parameters))
361362

362-
async def executemany(
363+
def executemany(
363364
self, operation: t.Any, seq_of_parameters: t.Sequence[t.Any]
364365
) -> None:
365-
await self._executemany(operation, seq_of_parameters)
366+
self.await_(self._executemany(operation, seq_of_parameters))
366367

367368
def setinputsizes(self, *inputsizes: t.Any) -> None:
368369
raise NotImplementedError

tests/test_connection.py

Lines changed: 17 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ def setUp(self):
5151
"connection_errors": 0,
5252
}
5353
self.mock_adapt_connection._connection_valid = True
54+
self.mock_adapt_connection.await_ = Mock()
5455

5556
self.cursor = AsyncAdapt_psqlpy_cursor(self.mock_adapt_connection)
5657

@@ -216,10 +217,8 @@ def test_execute(self, mock_prepare_execute):
216217
operation = "SELECT * FROM table"
217218
parameters = {"id": 123}
218219

219-
# Since execute is now async, we need to run it in an async context
220-
import asyncio
221-
222-
asyncio.run(self.cursor.execute(operation, parameters))
220+
# Execute is now synchronous and uses await_ internally
221+
self.cursor.execute(operation, parameters)
223222

224223
mock_prepare_execute.assert_called_once_with(operation, parameters)
225224

@@ -231,10 +230,8 @@ def test_executemany(self, mock_executemany):
231230
operation = "INSERT INTO table VALUES ($1, $2)"
232231
seq_of_parameters = [[1, "a"], [2, "b"]]
233232

234-
# Since executemany is now async, we need to run it in an async context
235-
import asyncio
236-
237-
asyncio.run(self.cursor.executemany(operation, seq_of_parameters))
233+
# Executemany is now synchronous and uses await_ internally
234+
self.cursor.executemany(operation, seq_of_parameters)
238235

239236
mock_executemany.assert_called_once_with(operation, seq_of_parameters)
240237

@@ -306,10 +303,8 @@ def test_executemany_coverage(self, mock_executemany):
306303
operation = "INSERT INTO test VALUES ($1, $2)"
307304
seq_of_parameters = [[1, "a"], [2, "b"]]
308305

309-
# Since executemany is now async, we need to run it in an async context
310-
import asyncio
311-
312-
asyncio.run(self.cursor.executemany(operation, seq_of_parameters))
306+
# Executemany is now synchronous and uses await_ internally
307+
self.cursor.executemany(operation, seq_of_parameters)
313308

314309
mock_executemany.assert_called_once_with(operation, seq_of_parameters)
315310

@@ -321,9 +316,7 @@ def test_execute_update_basic(self, mock_prepare_execute):
321316
"""Test basic UPDATE operation with execute method"""
322317
operation = "UPDATE users SET name = 'John' WHERE id = 1"
323318

324-
import asyncio
325-
326-
asyncio.run(self.cursor.execute(operation))
319+
self.cursor.execute(operation)
327320

328321
mock_prepare_execute.assert_called_once_with(operation, None)
329322

@@ -337,9 +330,7 @@ def test_execute_update_with_named_parameters(self, mock_prepare_execute):
337330
)
338331
parameters = {"name": "John Doe", "email": "[email protected]", "id": 1}
339332

340-
import asyncio
341-
342-
asyncio.run(self.cursor.execute(operation, parameters))
333+
self.cursor.execute(operation, parameters)
343334

344335
mock_prepare_execute.assert_called_once_with(operation, parameters)
345336

@@ -353,9 +344,7 @@ def test_execute_update_with_positional_parameters(
353344
operation = "UPDATE users SET name = $1, email = $2 WHERE id = $3"
354345
parameters = ["John Doe", "[email protected]", 1]
355346

356-
import asyncio
357-
358-
asyncio.run(self.cursor.execute(operation, parameters))
347+
self.cursor.execute(operation, parameters)
359348

360349
mock_prepare_execute.assert_called_once_with(operation, parameters)
361350

@@ -372,9 +361,7 @@ def test_execute_update_multiple_columns(self, mock_prepare_execute):
372361
"id": 2,
373362
}
374363

375-
import asyncio
376-
377-
asyncio.run(self.cursor.execute(operation, parameters))
364+
self.cursor.execute(operation, parameters)
378365

379366
mock_prepare_execute.assert_called_once_with(operation, parameters)
380367

@@ -386,9 +373,7 @@ def test_execute_update_with_where_clause(self, mock_prepare_execute):
386373
operation = "UPDATE users SET status = :status WHERE age > :min_age AND created_at < :date"
387374
parameters = {"status": "active", "min_age": 18, "date": "2023-01-01"}
388375

389-
import asyncio
390-
391-
asyncio.run(self.cursor.execute(operation, parameters))
376+
self.cursor.execute(operation, parameters)
392377

393378
mock_prepare_execute.assert_called_once_with(operation, parameters)
394379

@@ -404,9 +389,7 @@ def test_executemany_update_operations(self, mock_executemany):
404389
["Bob Johnson", "[email protected]", 3],
405390
]
406391

407-
import asyncio
408-
409-
asyncio.run(self.cursor.executemany(operation, seq_of_parameters))
392+
self.cursor.executemany(operation, seq_of_parameters)
410393

411394
mock_executemany.assert_called_once_with(operation, seq_of_parameters)
412395

@@ -421,9 +404,7 @@ def test_executemany_update_with_dict_parameters(self, mock_executemany):
421404
{"name": "Jane Updated", "id": 2},
422405
]
423406

424-
import asyncio
425-
426-
asyncio.run(self.cursor.executemany(operation, seq_of_parameters))
407+
self.cursor.executemany(operation, seq_of_parameters)
427408

428409
mock_executemany.assert_called_once_with(operation, seq_of_parameters)
429410

@@ -436,9 +417,7 @@ def test_execute_update_with_uuid_parameter(self, mock_prepare_execute):
436417
operation = "UPDATE users SET profile_id = :profile_id WHERE id = :id"
437418
parameters = {"profile_id": test_uuid, "id": 1}
438419

439-
import asyncio
440-
441-
asyncio.run(self.cursor.execute(operation, parameters))
420+
self.cursor.execute(operation, parameters)
442421

443422
mock_prepare_execute.assert_called_once_with(operation, parameters)
444423

@@ -455,7 +434,7 @@ def test_execute_update_async_greenlet_fix(self, mock_prepare_execute):
455434
import asyncio
456435

457436
# This should not raise any greenlet-related errors
458-
asyncio.run(self.cursor.execute(operation, parameters))
437+
self.cursor.execute(operation, parameters)
459438

460439
mock_prepare_execute.assert_called_once_with(operation, parameters)
461440

@@ -472,9 +451,7 @@ def test_execute_update_with_null_values(self, mock_prepare_execute):
472451
)
473452
parameters = {"email": None, "phone": None, "id": 1}
474453

475-
import asyncio
476-
477-
asyncio.run(self.cursor.execute(operation, parameters))
454+
self.cursor.execute(operation, parameters)
478455

479456
mock_prepare_execute.assert_called_once_with(operation, parameters)
480457

0 commit comments

Comments
 (0)