Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add notify #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions pgnotify/__init__.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 2 additions & 2 deletions pgnotify/notify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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])
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
14 changes: 13 additions & 1 deletion tests/test_pgnotify.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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');")