Skip to content

Commit 3a0869f

Browse files
Update pgsql client to have min size of 0
1 parent 0cb5835 commit 3a0869f

4 files changed

Lines changed: 13 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v0.0.7 - 3/1/23
4+
- Set PostgreSQL connection pool to have a default pool size minimum of 0
5+
36
## v0.0.5 - 2/22/23
47
- Support write queries to PostgreSQL and MySQL databases
58
- Support different return formats when querying PostgreSQL, MySQL, and Redshift databases

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "nypl_py_utils"
7-
version = "0.0.6"
7+
version = "0.0.7"
88
authors = [
99
{ name="Aaron Friedman", email="aaronfriedman@nypl.org" },
1010
]

src/nypl_py_utils/classes/postgresql_client.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ def __init__(self, host, port, db_name, user, password, **kwargs):
1919
'{db_name}').format(user=user, password=password,
2020
host=host, port=port,
2121
db_name=db_name)
22-
self.min_size = kwargs.get('min_size', 1)
23-
self.max_size = kwargs.get('max_size', None)
22+
self.min_size = kwargs.get('min_size', 0)
23+
self.max_size = kwargs.get('max_size', 1)
2424
self.pool = ConnectionPool(
2525
self.conn_info, open=False,
26-
min_size=kwargs.get('min_size', 1),
27-
max_size=kwargs.get('max_size', None))
26+
min_size=self.min_size, max_size=self.max_size)
2827

2928
def connect(self):
3029
"""
@@ -73,7 +72,6 @@ def execute_query(self, query, is_write_query=False, query_params=None,
7372
"""
7473
self.logger.info('Querying {} database'.format(self.db_name))
7574
self.logger.debug('Executing query {}'.format(query))
76-
self.pool.check()
7775
with self.pool.connection() as conn:
7876
try:
7977
conn.row_factory = row_factory

tests/test_postgresql_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import pytest
22

33
from nypl_py_utils import PostgreSQLClient, PostgreSQLClientError
4+
from psycopg import Error
45

56

67
class TestPostgreSQLClient:
@@ -17,7 +18,7 @@ def test_init(self, test_instance):
1718
'postgresql://test_user:test_password@test_host:test_port/' +
1819
'test_db_name')
1920
assert test_instance.pool._opened is False
20-
assert test_instance.pool.min_size == 1
21+
assert test_instance.pool.min_size == 0
2122
assert test_instance.pool.max_size == 1
2223

2324
def test_init_with_kwargs(self):
@@ -35,7 +36,10 @@ def test_connect(self, test_instance):
3536
test_instance.connect()
3637
test_instance.pool.open.assert_called_once_with(wait=True, timeout=300)
3738

38-
def test_connect_with_exception(self):
39+
def test_connect_with_exception(self, mocker):
40+
mocker.patch('psycopg_pool.ConnectionPool.open',
41+
side_effect=Error())
42+
3943
test_instance = PostgreSQLClient(
4044
'test_host', 'test_port', 'test_db_name', 'test_user',
4145
'test_password', timeout=1.0)

0 commit comments

Comments
 (0)