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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In tests, leave SQLAlchemy in charge of establishing engine connection #46

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@
import sqlalchemy as sa
import pytest


@pytest.fixture
def engine(postgresql):
"""Create an SQLAlchemy engine with a disposable PostgreSQL database."""
return sa.create_engine('postgresql://', poolclass=sa.pool.StaticPool,
creator=lambda: postgresql)
from healpix_alchemy.tests.benchmarks.conftest import engine # noqa: F401


@pytest.fixture(autouse=True)
def add_mock_create_engine(monkeypatch, request):
"""Monkey patch sqlalchemy.create_engine for doctests in README.md."""
if request.node.name == 'README.md':
engine = request.getfixturevalue('engine')
monkeypatch.setattr(sa, 'create_engine', Mock(return_value=engine))
db_engine = request.getfixturevalue('engine')
monkeypatch.setattr(sa, 'create_engine', Mock(return_value=db_engine))
16 changes: 13 additions & 3 deletions healpix_alchemy/tests/benchmarks/conftest.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import numpy as np
import sqlalchemy as sa
import pytest
from pytest_postgresql.janitor import DatabaseJanitor

from . import data, models


@pytest.fixture
def engine(postgresql):
return sa.create_engine('postgresql://', poolclass=sa.pool.StaticPool,
creator=lambda: postgresql)
def engine(postgresql_proc):
"""Create an SQLAlchemy engine with a disposable PostgreSQL database."""
host = postgresql_proc.host
port = postgresql_proc.port
user = postgresql_proc.user
password = postgresql_proc.password
db = postgresql_proc.dbname
version = postgresql_proc.version

url = sa.engine.URL.create('postgresql', user, password, host, port, db)
with DatabaseJanitor(user, host, port, db, version, password):
yield sa.create_engine(url)


@pytest.fixture
Expand Down
122 changes: 118 additions & 4 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ sqlalchemy = ">=1.4"
astroquery = "*"
numpy = "*"
pandas = "*"
psycopg = {version="*", extras=["binary"]} # for pytest-postgresql
psycopg2-binary = "*"
pytest = "*"
pytest-benchmark = "*"
pytest-doctestplus = "*"
pytest-postgresql = "<4.0.0" # pytest-postgresql 4.0.0 uses psycopg3, but sqlalchemy does not yet support it
pytest-postgresql = "*"

[tool.pytest.ini_options]
addopts = "--doctest-glob *.md"
Expand Down