From cb7d7801b2884121794c39c63524f45439446489 Mon Sep 17 00:00:00 2001 From: Leonardo Daher Date: Mon, 26 Aug 2019 13:20:32 -0300 Subject: [PATCH 1/3] Add notify method This patch adds a method for sending a PostgreSQL notify with a payload to a specific channel. --- pgnotify/__init__.py | 18 ++++++++++++++++++ pgnotify/notify.py | 2 +- tests/test_pgnotify.py | 14 +++++++++++++- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/pgnotify/__init__.py b/pgnotify/__init__.py index 60b6728..f6c7c7c 100644 --- a/pgnotify/__init__.py +++ b/pgnotify/__init__.py @@ -7,3 +7,21 @@ get_dbapi_connection, start_listening, ) + +log.set_null_handler() + + +def notify(connection, channel, payload): + """Send a PostgreSQL notify with a payload + + :param connection: dburi, sqlengine or dbapiconnection + :param channel: channel that the notify is sent to + :param payload: payload to be sent together with notify + """ + connection = get_dbapi_connection(connection) + + with connection: + with connection.cursor() as cursor: + cursor.execute("SELECT pg_notify('%s', '%s');", (channel, payload)) + + connection.close() diff --git a/pgnotify/notify.py b/pgnotify/notify.py index eca89a3..7af3b6f 100644 --- a/pgnotify/notify.py +++ b/pgnotify/notify.py @@ -162,5 +162,5 @@ def await_pg_notifications( for s in signals_to_handle: if s in original_handlers: signal_name = signal.Signals(s).name - log.debug(f"restoring original handler for: {signal_name}") + log.debug("restoring original handler for: {}".format(signal_name)) signal.signal(s, original_handlers[s]) diff --git a/tests/test_pgnotify.py b/tests/test_pgnotify.py index ca0fbeb..2e57ab2 100644 --- a/tests/test_pgnotify.py +++ b/tests/test_pgnotify.py @@ -2,13 +2,14 @@ import os import signal +from unittest import mock import psycopg2 from pytest import raises from sqlalchemy import create_engine from sqlbag import S -from pgnotify import await_pg_notifications +from pgnotify import await_pg_notifications, notify from pgnotify.notify import get_dbapi_connection SIGNALS_TO_HANDLE = [signal.SIGINT] @@ -92,3 +93,14 @@ def get_timeout(): db, "hello", timeout=0.1, yield_on_timeout=True ): os.kill(os.getpid(), signal.SIGINT) + + +@mock.patch("pgnotify.get_dbapi_connection") +def test_send_notify(get_dbapi_connection_mock): + mock_db = mock.Mock() + assert notify(mock_db, "hello", "test") is None + get_dbapi_connection_mock.assert_called_once_with(mock_db) + connection_mock = get_dbapi_connection_mock.return_value + connection_mock.cursor.assert_called_once_with() + cursor = connection_mock.cursor.return_value + cursor.execute.assert_called_once_with("SELECT pg_notify('hello', 'test');") From e983ebc38723839f2b2f3c3132a4b34e24f290c2 Mon Sep 17 00:00:00 2001 From: Leonardo Daher Date: Tue, 27 Aug 2019 14:56:42 -0300 Subject: [PATCH 2/3] Format code --- pgnotify/__init__.py | 6 +++--- pgnotify/notify.py | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pgnotify/__init__.py b/pgnotify/__init__.py index f6c7c7c..5662d70 100644 --- a/pgnotify/__init__.py +++ b/pgnotify/__init__.py @@ -1,10 +1,10 @@ from __future__ import absolute_import, division, print_function, unicode_literals + from logx import log -log.set_null_handler() -from .notify import ( # noqa +from .notify import ( await_pg_notifications, - get_dbapi_connection, + get_dbapi_connection, # noqa start_listening, ) diff --git a/pgnotify/notify.py b/pgnotify/notify.py index 7af3b6f..2f6c19b 100644 --- a/pgnotify/notify.py +++ b/pgnotify/notify.py @@ -131,7 +131,7 @@ def await_pg_notifications( sig = signal.Signals(signal_int) signal_name = signal.Signals(sig).name - log.info(f"woken from slumber by signal: {signal_name}") + log.info("woken from slumber by signal: {}".format(signal_name)) yield signal_int if cc in r: From bc7c6be3b4e11e98aa0f1ee16b9063cb69508aca Mon Sep 17 00:00:00 2001 From: Leonardo Daher Date: Mon, 2 Sep 2019 16:48:43 -0300 Subject: [PATCH 3/3] Add support for installing via pip directly from github --- pyproject.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/pyproject.toml b/pyproject.toml index cc9b09a..a49bfdc 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -25,3 +25,7 @@ twine = "*" wheel = "*" isort = "*" black = {python=">3.6", version=">=18.9b0", allow_prereleases=true} + +[build-system] +requires = ["poetry"] +build-backend = "poetry.masonry.api"