Skip to content

Commit 756d6f7

Browse files
committed
add support for null poolclass
1 parent 058e5c9 commit 756d6f7

5 files changed

Lines changed: 81 additions & 6 deletions

File tree

open_bus_stride_db/db.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,32 @@
99
SQLALCHEMY_APPLICATION_NAME = os.getenv("SQLALCHEMY_APPLICATION_NAME", "db")
1010
SQLALCHEMY_APPLICATION_VERSION = os.getenv("SQLALCHEMY_APPLICATION_VERSION", "-")
1111

12-
13-
engine = create_engine(
14-
os.environ.get('SQLALCHEMY_URL', 'postgresql://postgres:123456@localhost'),
12+
_create_engine_kwargs = dict(
1513
future=True,
1614
connect_args={
1715
"options": "-c timezone=utc",
1816
"application_name": f'{SQLALCHEMY_APPLICATION_NAME} {SQLALCHEMY_APPLICATION_VERSION}'[:64],
1917
},
2018
echo=bool(os.environ.get('SQLALCHEMY_ECHO')),
21-
pool_size=int(os.environ.get('SQLALCHEMY_POOL_SIZE', 10)),
22-
max_overflow=int(os.environ.get('SQLALCHEMY_MAX_OVERFLOW', 20)),
23-
pool_timeout=int(os.environ.get('SQLALCHEMY_POOL_TIMEOUT', 30)),
19+
)
20+
if os.getenv("SQLALCHEMY_POOLCLASS_NULLPOOL") == "yes":
21+
from sqlalchemy.pool import NullPool
22+
_create_engine_kwargs.update(
23+
poolclass=NullPool,
24+
pool_pre_ping=True,
25+
)
26+
else:
27+
_create_engine_kwargs.update(
28+
dict(
29+
pool_size=int(os.environ.get('SQLALCHEMY_POOL_SIZE', 10)),
30+
max_overflow=int(os.environ.get('SQLALCHEMY_MAX_OVERFLOW', 20)),
31+
pool_timeout=int(os.environ.get('SQLALCHEMY_POOL_TIMEOUT', 30)),
32+
)
33+
)
34+
35+
engine = create_engine(
36+
os.environ.get('SQLALCHEMY_URL', 'postgresql://postgres:123456@localhost'),
37+
**_create_engine_kwargs,
2438
)
2539
_sessionmaker = sessionmaker(bind=engine, future=True, autoflush=False, autocommit=False)
2640

tests/__init__.py

Whitespace-only changes.

tests/conftest.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import os
2+
import time
3+
import importlib
4+
import subprocess
5+
6+
from open_bus_stride_db import db as db_module
7+
8+
import pytest
9+
10+
11+
@pytest.fixture(scope="session")
12+
def get_db():
13+
create_container = True
14+
if os.getenv("KEEP_TEST_POSTGRES_CONTAINER") == "yes":
15+
if subprocess.call([
16+
"docker", "container", "inspect", "open-bus-stride-db-test-postgres"
17+
], stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) == 0:
18+
create_container = False
19+
if create_container:
20+
subprocess.call([
21+
"docker", "rm", "-f", "open-bus-stride-db-test-postgres"
22+
])
23+
subprocess.check_call([
24+
"docker", "run", "--name", "open-bus-stride-db-test-postgres",
25+
"-e", "POSTGRES_USER=postgres",
26+
"-e", "POSTGRES_PASSWORD=123456",
27+
"-p", "5432:5432",
28+
"-d", "postgres:14"
29+
])
30+
time.sleep(3)
31+
32+
def get_db_(poolclass_nullpool=False):
33+
os.environ["SQLALCHEMY_URL"] = "postgresql://postgres:123456@localhost"
34+
if poolclass_nullpool:
35+
os.environ["SQLALCHEMY_POOLCLASS_NULLPOOL"] = "yes"
36+
elif "SQLALCHEMY_POOLCLASS_NULLPOOL" in os.environ:
37+
del os.environ["SQLALCHEMY_POOLCLASS_NULLPOOL"]
38+
importlib.reload(db_module)
39+
return db_module
40+
41+
try:
42+
yield get_db_
43+
finally:
44+
if os.getenv("KEEP_TEST_POSTGRES_CONTAINER") != "yes":
45+
subprocess.call([
46+
"docker", "rm", "-f", "open-bus-stride-db-test-postgres"
47+
])

tests/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pytest==9.0.2

tests/test_db.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
def test(get_db):
3+
db = get_db()
4+
with db.get_session() as session:
5+
assert session.execute("SELECT 1").scalar() == 1
6+
assert "poolclass" not in db._create_engine_kwargs
7+
8+
9+
def test_nullpool(get_db):
10+
db = get_db(poolclass_nullpool=True)
11+
with db.get_session() as session:
12+
assert session.execute("SELECT 1").scalar() == 1
13+
assert db._create_engine_kwargs["poolclass"].__name__ == "NullPool"

0 commit comments

Comments
 (0)