diff --git a/pgnotify/__init__.py b/pgnotify/__init__.py index 60b6728..5662d70 100644 --- a/pgnotify/__init__.py +++ b/pgnotify/__init__.py @@ -1,9 +1,27 @@ 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, ) + +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..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: @@ -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/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" 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');")