Skip to content

Commit 28b8264

Browse files
committed
Merge branch 'main' into noref-add-platform-api-client
2 parents a207ccf + 2d103b2 commit 28b8264

5 files changed

Lines changed: 21 additions & 18 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
# Changelog
22

3-
## v0.0.7 - 2/28/23
3+
## v0.0.8 - 3/3/23
4+
- Pass in all kwargs from PostgreSQLClient to ConnectionPool so that all ConnectionPool settings
5+
can be set from the wrapper
6+
7+
## v0.0.7 - 3/1/23
48
- Added Oauth2ApiClient for oauth2 authenticated calls to our Platform API and Sierra
9+
- Set PostgreSQL connection pool to have a default pool size minimum of 0
510

611
## v0.0.5 - 2/22/23
712
- Support write queries to PostgreSQL and MySQL databases
@@ -12,17 +17,14 @@
1217
- Updated README with deployment information
1318

1419
## v0.0.3 - 2/10/23
15-
1620
- Added GitHub Actions workflow for deploying to production
1721
- Switched PostgreSQLClient to use connection pooling
1822

1923
## v0.0.2 - 2/6/23
20-
2124
- Added CODEOWNERS
2225
- Added GitHub Actions workflows for running tests and deploying to QA
2326
- Added tests for helper functions
2427
- Updated Avro encoder to avoid dependency on pandas
2528

2629
## v0.0.1 - 1/26/23
27-
2830
Initial version. Includes the `avro_encoder`, `kinesis_client`, `mysql_client`, `postgresql_client`, `redshift_client`, and `s3_client` classes as well as the `config_helper`, `kms_helper`, `log_helper`, and `obfuscation_helper` functions.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This package contains common Python utility classes and functions.
1010
* Connecting to and querying a MySQL database
1111
* Connecting to and querying a PostgreSQL database using a connection pool
1212
* Connecting to and querying Redshift
13-
* Making requests to the Oauth2 authenticated APIS such as NYPL Platform API and Sierra
13+
* Making requests to the Oauth2 authenticated APIs such as NYPL Platform API and Sierra
1414

1515
## Functions
1616
* Reading a YAML config file and putting the contents in os.environ

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.7"
7+
version = "0.0.8"
88
authors = [
99
{ name="Aaron Friedman", email="aaronfriedman@nypl.org" },
1010
]

src/nypl_py_utils/classes/postgresql_client.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@ 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)
24-
self.pool = ConnectionPool(
25-
self.conn_info, open=False,
26-
min_size=kwargs.get('min_size', 1),
27-
max_size=kwargs.get('max_size', None))
22+
self.kwargs = kwargs
23+
self.kwargs['min_size'] = kwargs.get('min_size', 0)
24+
self.kwargs['max_size'] = kwargs.get('max_size', 1)
25+
self.pool = ConnectionPool(self.conn_info, open=False, **self.kwargs)
2826

2927
def connect(self):
3028
"""
@@ -35,8 +33,7 @@ def connect(self):
3533
try:
3634
if self.pool is None:
3735
self.pool = ConnectionPool(
38-
self.conn_info, open=False, min_size=self.min_size,
39-
max_size=self.max_size)
36+
self.conn_info, open=False, **self.kwargs)
4037
self.pool.open(wait=True, timeout=self.timeout)
4138
except psycopg.Error as e:
4239
self.logger.error(
@@ -73,7 +70,6 @@ def execute_query(self, query, is_write_query=False, query_params=None,
7370
"""
7471
self.logger.info('Querying {} database'.format(self.db_name))
7572
self.logger.debug('Executing query {}'.format(query))
76-
self.pool.check()
7773
with self.pool.connection() as conn:
7874
try:
7975
conn.row_factory = row_factory

tests/test_postgresql_client.py

Lines changed: 8 additions & 3 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,25 +18,29 @@ 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):
2425
test_instance = PostgreSQLClient(
2526
'test_host', 'test_port', 'test_db_name', 'test_user',
26-
'test_password', min_size=5, max_size=10)
27+
'test_password', min_size=5, max_size=10, max_idle=15.0)
2728
assert test_instance.pool.conninfo == (
2829
'postgresql://test_user:test_password@test_host:test_port/' +
2930
'test_db_name')
3031
assert test_instance.pool._opened is False
3132
assert test_instance.pool.min_size == 5
3233
assert test_instance.pool.max_size == 10
34+
assert test_instance.pool.max_idle == 15.0
3335

3436
def test_connect(self, test_instance):
3537
test_instance.connect()
3638
test_instance.pool.open.assert_called_once_with(wait=True, timeout=300)
3739

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

0 commit comments

Comments
 (0)