MCPcopy
hub / github.com/psycopg/psycopg / test_notice_handlers

Function test_notice_handlers

tests/test_connection.py:564–602  ·  view source on GitHub ↗
(conn, caplog)

Source from the content-addressed store, hash-verified

562
563@pytest.mark.crdb_skip("do")
564def test_notice_handlers(conn, caplog):
565 caplog.set_level(logging.WARNING, logger="psycopg")
566 messages = []
567 severities = []
568
569 def cb1(diag):
570 messages.append(diag.message_primary)
571
572 def cb2(res):
573 raise Exception("hello from cb2")
574
575 conn.add_notice_handler(cb1)
576 conn.add_notice_handler(cb2)
577 conn.add_notice_handler("the wrong thing")
578 conn.add_notice_handler(lambda diag: severities.append(diag.severity_nonlocalized))
579
580 conn.pgconn.exec_(b"set client_min_messages to notice")
581 cur = conn.cursor()
582 cur.execute("do $$begin raise notice 'hello notice'; end$$ language plpgsql")
583 assert messages == ["hello notice"]
584 assert severities == ["NOTICE"]
585
586 assert len(caplog.records) == 2
587 rec = caplog.records[0]
588 assert rec.levelno == logging.ERROR
589 assert "hello from cb2" in rec.message
590 rec = caplog.records[1]
591 assert rec.levelno == logging.ERROR
592 assert "the wrong thing" in rec.message
593
594 conn.remove_notice_handler(cb1)
595 conn.remove_notice_handler("the wrong thing")
596 cur.execute("do $$begin raise warning 'hello warning'; end$$ language plpgsql")
597 assert len(caplog.records) == 3
598 assert messages == ["hello notice"]
599 assert severities == ["NOTICE", "WARNING"]
600
601 with pytest.raises(ValueError):
602 conn.remove_notice_handler(cb1)
603
604
605def test_execute(conn):

Callers

nothing calls this directly

Calls 5

add_notice_handlerMethod · 0.80
remove_notice_handlerMethod · 0.80
exec_Method · 0.45
cursorMethod · 0.45
executeMethod · 0.45

Tested by

no test coverage detected