11import os
22import platform
3+ import re
34from unittest import mock
45
56import pytest
1314 obfuscate_process_password ,
1415 duration_in_words ,
1516 format_output ,
17+ notify_callback ,
1618 PGCli ,
1719 OutputSettings ,
1820 COLOR_CODE_REGEX ,
@@ -432,6 +434,7 @@ def test_pg_service_file(tmpdir):
432434 "b_host" ,
433435 "5435" ,
434436 "" ,
437+ notify_callback ,
435438 application_name = "pgcli" ,
436439 )
437440 del os .environ ["PGPASSWORD" ]
@@ -487,7 +490,7 @@ def test_application_name_db_uri(tmpdir):
487490 cli = PGCli (pgclirc_file = str (tmpdir .join ("rcfile" )))
488491 cli .
connect_uri (
"postgres://[email protected] /?application_name=cow" )
489492 mock_pgexecute .assert_called_with (
490- "bar" , "bar" , "" , "baz.com" , "" , "" , application_name = "cow"
493+ "bar" , "bar" , "" , "baz.com" , "" , "" , notify_callback , application_name = "cow"
491494 )
492495
493496
@@ -514,3 +517,23 @@ def test_application_name_db_uri(tmpdir):
514517)
515518def test_duration_in_words (duration_in_seconds , words ):
516519 assert duration_in_words (duration_in_seconds ) == words
520+
521+
522+ @dbtest
523+ def test_notifications (executor ):
524+ run (executor , "listen chan1" )
525+
526+ with mock .patch ("pgcli.main.click.secho" ) as mock_secho :
527+ run (executor , "notify chan1, 'testing1'" )
528+ mock_secho .assert_called ()
529+ arg = mock_secho .call_args_list [0 ].args [0 ]
530+ assert re .match (
531+ r'Notification received on channel "chan1" \(PID \d+\):\ntesting1' ,
532+ arg ,
533+ )
534+
535+ run (executor , "unlisten chan1" )
536+
537+ with mock .patch ("pgcli.main.click.secho" ) as mock_secho :
538+ run (executor , "notify chan1, 'testing2'" )
539+ mock_secho .assert_not_called ()
0 commit comments