Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add postgresql+asyncpg scheme prefix (closes #395) #396

Merged
merged 22 commits into from
Sep 25, 2021
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test-suite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ jobs:
run: "scripts/install"
- name: "Run tests"
env:
TEST_DATABASE_URLS: "sqlite:///testsuite, mysql://username:password@localhost:3306/testsuite, postgresql://username:password@localhost:5432/testsuite, postgresql+aiopg://username:password@127.0.0.1:5432/testsuite"
TEST_DATABASE_URLS: "sqlite:///testsuite, mysql://username:password@localhost:3306/testsuite, postgresql://username:password@localhost:5432/testsuite, postgresql+aiopg://username:password@127.0.0.1:5432/testsuite, postgresql+asyncpg://username:password@localhost:5432/testsuite"
mhadam marked this conversation as resolved.
Show resolved Hide resolved
run: "scripts/test"
8 changes: 7 additions & 1 deletion databases/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(

self._force_rollback = force_rollback

backend_str = self.SUPPORTED_BACKENDS[self.url.scheme]
backend_str = self._get_backend()
backend_cls = import_from_string(backend_str)
assert issubclass(backend_cls, DatabaseBackend)
self._backend = backend_cls(self.url, **self.options)
Expand Down Expand Up @@ -220,6 +220,12 @@ def force_rollback(self) -> typing.Iterator[None]:
finally:
self._force_rollback = initial

def _get_backend(self) -> typing.Optional[str]:
try:
return self.SUPPORTED_BACKENDS[self.url.scheme]
except KeyError:
return self.SUPPORTED_BACKENDS[self.url.dialect]


class Connection:
def __init__(self, backend: DatabaseBackend) -> None:
Expand Down
20 changes: 16 additions & 4 deletions tests/test_databases.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ def create_test_database():
# Create test databases with tables creation
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.scheme == "mysql":
if database_url.scheme in ["mysql", "mysql+aiomysql"]:
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
url = str(database_url.replace(driver="pymysql"))
elif database_url.scheme == "postgresql+aiopg":
elif database_url.scheme in ["postgresql+aiopg", "postgresql+asyncpg"]:
url = str(database_url.replace(driver=None))
elif database_url.scheme in ["sqlite", "sqlite+aiosqlite"]:
url = str(database_url.replace(driver=None))
engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
Expand All @@ -96,9 +98,11 @@ def create_test_database():
# Drop test databases
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.scheme == "mysql":
if database_url.scheme in ["mysql", "mysql+aiomysql"]:
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
url = str(database_url.replace(driver="pymysql"))
elif database_url.scheme == "postgresql+aiopg":
elif database_url.scheme in ["postgresql+aiopg", "postgresql+asyncpg"]:
url = str(database_url.replace(driver=None))
elif database_url.scheme in ["sqlite", "sqlite+aiosqlite"]:
url = str(database_url.replace(driver=None))
engine = sqlalchemy.create_engine(url)
metadata.drop_all(engine)
Expand Down Expand Up @@ -1116,3 +1120,11 @@ async def test_postcompile_queries(database_url):
results = await database.fetch_all(query=query)

assert len(results) == 0


@pytest.mark.parametrize("database_url", DATABASE_URLS)
@async_adapter
async def test_supported_backends_schemes(database_url):
mhadam marked this conversation as resolved.
Show resolved Hide resolved
async with Database(database_url) as database:
backend_str = database._get_backend()
assert backend_str in database.SUPPORTED_BACKENDS.values()
12 changes: 8 additions & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ def create_test_database():
# Create test databases
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.scheme == "mysql":
if database_url.scheme in ["mysql", "mysql+aiomysql"]:
url = str(database_url.replace(driver="pymysql"))
elif database_url.scheme == "postgresql+aiopg":
elif database_url.scheme in ["postgresql+aiopg", "postgresql+asyncpg"]:
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
url = str(database_url.replace(driver=None))
elif database_url.scheme in ["sqlite", "sqlite+aiosqlite"]:
url = str(database_url.replace(driver=None))
engine = sqlalchemy.create_engine(url)
metadata.create_all(engine)
Expand All @@ -41,9 +43,11 @@ def create_test_database():
# Drop test databases
for url in DATABASE_URLS:
database_url = DatabaseURL(url)
if database_url.scheme == "mysql":
if database_url.scheme in ["mysql", "mysql+aiomysql"]:
url = str(database_url.replace(driver="pymysql"))
elif database_url.scheme == "postgresql+aiopg":
elif database_url.scheme in ["postgresql+aiopg", "postgresql+asyncpg"]:
aminalaee marked this conversation as resolved.
Show resolved Hide resolved
url = str(database_url.replace(driver=None))
elif database_url.scheme in ["sqlite", "sqlite+aiosqlite"]:
url = str(database_url.replace(driver=None))
engine = sqlalchemy.create_engine(url)
metadata.drop_all(engine)
Expand Down