(conn, caplog)
| 562 | |
| 563 | @pytest.mark.crdb_skip("do") |
| 564 | def 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 | |
| 605 | def test_execute(conn): |
nothing calls this directly
no test coverage detected