Skip to content
This repository was archived by the owner on Aug 19, 2025. It is now read-only.

Commit 66ec568

Browse files
committed
Checks for failing unittests
1 parent 4651671 commit 66ec568

2 files changed

Lines changed: 28 additions & 29 deletions

File tree

databases/backends/aiopg.py

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _get_connection_kwargs(self) -> dict: # TODO move to `core.py`
6363

6464
return kwargs
6565

66-
async def connect(self) -> None: # TODO as MySQL one?
66+
async def connect(self) -> None:
6767
assert self._pool is None, "DatabaseBackend is already running"
6868
kwargs = self._get_connection_kwargs()
6969
self._pool = await aiopg.create_pool(
@@ -72,7 +72,6 @@ async def connect(self) -> None: # TODO as MySQL one?
7272
user=self._database_url.username or getpass.getuser(),
7373
password=self._database_url.password,
7474
database=self._database_url.database,
75-
# autocommit=True,
7675
**kwargs,
7776
)
7877

@@ -112,8 +111,6 @@ async def fetch_all(self, query: ClauseElement) -> typing.List[typing.Mapping]:
112111
assert self._connection is not None, "Connection is not acquired"
113112
query, args, context = self._compile(query)
114113
cursor = await self._connection.cursor()
115-
# TODO
116-
import pdb; pdb.set_trace()
117114
try:
118115
await cursor.execute(query, args)
119116
rows = await cursor.fetchall()
@@ -129,8 +126,6 @@ async def fetch_one(self, query: ClauseElement) -> typing.Optional[typing.Mappin
129126
assert self._connection is not None, "Connection is not acquired"
130127
query, args, context = self._compile(query)
131128
cursor = await self._connection.cursor()
132-
# TODO
133-
import pdb; pdb.set_trace()
134129
try:
135130
await cursor.execute(query, args)
136131
row = await cursor.fetchone()
@@ -145,8 +140,6 @@ async def execute(self, query: ClauseElement) -> typing.Any:
145140
assert self._connection is not None, "Connection is not acquired"
146141
query, args, context = self._compile(query)
147142
cursor = await self._connection.cursor()
148-
# TODO
149-
import pdb; pdb.set_trace()
150143
try:
151144
await cursor.execute(query, args)
152145
return cursor.lastrowid
@@ -156,8 +149,6 @@ async def execute(self, query: ClauseElement) -> typing.Any:
156149
async def execute_many(self, queries: typing.List[ClauseElement]) -> None:
157150
assert self._connection is not None, "Connection is not acquired"
158151
cursor = await self._connection.cursor()
159-
# TODO
160-
import pdb; pdb.set_trace()
161152
try:
162153
for single_query in queries:
163154
single_query, args, context = self._compile(single_query)
@@ -171,8 +162,6 @@ async def iterate(
171162
assert self._connection is not None, "Connection is not acquired"
172163
query, args, context = self._compile(query)
173164
cursor = await self._connection.cursor()
174-
# TODO
175-
import pdb; pdb.set_trace()
176165
try:
177166
await cursor.execute(query, args)
178167
metadata = ResultMetaData(context, cursor.description)
@@ -217,17 +206,14 @@ def __init__(self, connection: AiopgConnection):
217206
self._savepoint_name = ""
218207

219208
async def start(self, is_root: bool) -> None:
220-
import pdb; pdb.set_trace()
221209
assert self._connection._connection is not None, "Connection is not acquired"
222210
self._is_root = is_root
223211
cursor = await self._connection._connection.cursor()
224212
if self._is_root:
225-
# await self._connection._connection.begin()
226213
await cursor.execute("BEGIN")
227214
else:
228215
id = str(uuid.uuid4()).replace("-", "_")
229216
self._savepoint_name = f"STARLETTE_SAVEPOINT_{id}"
230-
# cursor = await self._connection._connection.cursor()
231217
try:
232218
await cursor.execute(f"SAVEPOINT {self._savepoint_name}")
233219
finally:
@@ -237,10 +223,8 @@ async def commit(self) -> None:
237223
assert self._connection._connection is not None, "Connection is not acquired"
238224
cursor = await self._connection._connection.cursor()
239225
if self._is_root:
240-
# await self._connection._connection.commit()
241226
await cursor.execute("COMMIT")
242227
else:
243-
# cursor = await self._connection._connection.cursor()
244228
try:
245229
await cursor.execute(f"RELEASE SAVEPOINT {self._savepoint_name}")
246230
finally:
@@ -250,10 +234,8 @@ async def rollback(self) -> None:
250234
assert self._connection._connection is not None, "Connection is not acquired"
251235
cursor = await self._connection._connection.cursor()
252236
if self._is_root:
253-
# await self._connection._connection.rollback()
254237
await cursor.execute("ROLLBACK")
255238
else:
256-
# cursor = await self._connection._connection.cursor()
257239
try:
258240
await cursor.execute(f"ROLLBACK TO SAVEPOINT {self._savepoint_name}")
259241
finally:

tests/test_databases.py

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import datetime
33
import decimal
44
import functools
5+
import json
56
import os
67

78
import pytest
@@ -300,6 +301,9 @@ async def test_execute_return_val(database_url):
300301
query = notes.insert()
301302
values = {"text": "example1", "completed": True}
302303
pk = await database.execute(query, values)
304+
# Apparently for `aiopg` it's OID that will always 0 in this case
305+
# As it's only one action within this cursor life cycle
306+
# Something to triple check
303307
assert isinstance(pk, int)
304308

305309
query = notes.select().where(notes.c.id == pk)
@@ -505,7 +509,15 @@ async def test_json_field(database_url):
505509
# execute()
506510
query = session.insert()
507511
values = {"data": {"text": "hello", "boolean": True, "int": 1}}
508-
await database.execute(query, values)
512+
if str(database_url).startswith("postgresql+psycopg2"):
513+
await database.execute(
514+
query.values(
515+
# or wrapped with `psycopg2.extras.Json`
516+
data=json.dumps({"text": "hello", "boolean": True, "int": 1})
517+
)
518+
)
519+
else:
520+
await database.execute(query, values)
509521

510522
# fetch_all()
511523
query = session.select()
@@ -667,19 +679,19 @@ async def test_queries_with_expose_backend_connection(database_url):
667679
raw_connection = connection.raw_connection
668680

669681
# Insert query
670-
if str(database_url).startswith("mysql"):
682+
if str(database_url).startswith("mysql") or str(database_url).startswith("postgresql+psycopg2"):
671683
insert_query = "INSERT INTO notes (text, completed) VALUES (%s, %s)"
672684
else:
673685
insert_query = "INSERT INTO notes (text, completed) VALUES ($1, $2)"
674686

675687
# execute()
676688
values = ("example1", True)
677689

678-
if str(database_url).startswith("postgresql"):
679-
await raw_connection.execute(insert_query, *values)
680-
elif str(database_url).startswith("mysql"):
690+
if str(database_url).startswith("mysql") or str(database_url).startswith("postgresql+psycopg2"):
681691
cursor = await raw_connection.cursor()
682692
await cursor.execute(insert_query, values)
693+
elif str(database_url).startswith("postgresql"):
694+
await raw_connection.execute(insert_query, *values)
683695
elif str(database_url).startswith("sqlite"):
684696
await raw_connection.execute(insert_query, values)
685697

@@ -689,19 +701,24 @@ async def test_queries_with_expose_backend_connection(database_url):
689701
if str(database_url).startswith("mysql"):
690702
cursor = await raw_connection.cursor()
691703
await cursor.executemany(insert_query, values)
704+
elif str(database_url).startswith("postgresql+psycopg2"):
705+
cursor = await raw_connection.cursor()
706+
# No async support for `executemany`
707+
for value in values:
708+
await cursor.execute(insert_query, value)
692709
else:
693710
await raw_connection.executemany(insert_query, values)
694711

695712
# Select query
696713
select_query = "SELECT notes.id, notes.text, notes.completed FROM notes"
697714

698715
# fetch_all()
699-
if str(database_url).startswith("postgresql"):
700-
results = await raw_connection.fetch(select_query)
701-
elif str(database_url).startswith("mysql"):
716+
if str(database_url).startswith("mysql") or str(database_url).startswith("postgresql+psycopg2"):
702717
cursor = await raw_connection.cursor()
703718
await cursor.execute(select_query)
704719
results = await cursor.fetchall()
720+
elif str(database_url).startswith("postgresql"):
721+
results = await raw_connection.fetch(select_query)
705722
elif str(database_url).startswith("sqlite"):
706723
results = await raw_connection.execute_fetchall(select_query)
707724

@@ -713,9 +730,9 @@ async def test_queries_with_expose_backend_connection(database_url):
713730
assert results[1][2] == False
714731
assert results[2][1] == "example3"
715732
assert results[2][2] == True
716-
733+
717734
# fetch_one()
718-
if str(database_url).startswith("postgresql"):
735+
if str(database_url).startswith("postgresql") and not str(database_url).startswith("postgresql+psycopg2"):
719736
result = await raw_connection.fetchrow(select_query)
720737
else:
721738
cursor = await raw_connection.cursor()

0 commit comments

Comments
 (0)