示例#1
0
def test_connection_warn_close(dsn, recwarn):
    conn = Connection.connect(dsn)
    conn.close()
    del conn
    assert not recwarn

    conn = Connection.connect(dsn)
    del conn
    assert "IDLE" in str(recwarn.pop(ResourceWarning).message)

    conn = Connection.connect(dsn)
    conn.execute("select 1")
    del conn
    assert "INTRANS" in str(recwarn.pop(ResourceWarning).message)

    conn = Connection.connect(dsn)
    try:
        conn.execute("select wat")
    except Exception:
        pass
    del conn
    assert "INERROR" in str(recwarn.pop(ResourceWarning).message)

    with Connection.connect(dsn) as conn:
        pass
    del conn
    assert not recwarn
def test_connect_str_subclass(dsn):
    class MyString(str):
        pass

    conn = Connection.connect(MyString(dsn))
    assert not conn.closed
    assert conn.pgconn.status == conn.ConnStatus.OK
示例#3
0
def conn(dsn):
    """Return a `Connection` connected to the ``--test-dsn`` database."""
    from psycopg3 import Connection

    conn = Connection.connect(dsn)
    yield conn
    conn.close()
def test_connect_timeout():
    s = socket.socket(socket.AF_INET)
    s.bind(("", 0))
    port = s.getsockname()[1]
    s.listen(0)

    def closer():
        time.sleep(1.5)
        s.close()

    Thread(target=closer).start()

    t0 = time.time()
    with pytest.raises(psycopg3.DatabaseError):
        Connection.connect(host="localhost", port=port, connect_timeout=1)
    elapsed = time.time() - t0
    assert elapsed == pytest.approx(1.0, abs=0.05)
示例#5
0
def svcconn(dsn):
    """
    Return a session `Connection` connected to the ``--test-dsn`` database.
    """
    from psycopg3 import Connection

    conn = Connection.connect(dsn, autocommit=True)
    yield conn
    conn.close()
示例#6
0
def test_row_factory(dsn):
    conn = Connection.connect(dsn)
    assert conn.row_factory is tuple_row

    conn = Connection.connect(dsn, row_factory=my_row_factory)
    assert conn.row_factory is my_row_factory

    cur = conn.execute("select 'a' as ve")
    assert cur.fetchone() == ["Ave"]

    with conn.cursor(row_factory=lambda c: set) as cur:
        cur.execute("select 1, 1, 2")
        assert cur.fetchall() == [{1, 2}]

    with conn.cursor(row_factory=tuple_row) as cur:
        cur.execute("select 1, 1, 2")
        assert cur.fetchall() == [(1, 1, 2)]

    conn.row_factory = tuple_row
    cur = conn.execute("select 'vale'")
    assert cur.fetchone() == ("vale", )
def test_connect_bad():
    with pytest.raises(psycopg3.OperationalError):
        Connection.connect("dbname=nosuchdb")
def test_autocommit_connect(dsn):
    conn = Connection.connect(dsn, autocommit=True)
    assert conn.autocommit
def test_connect(dsn):
    conn = Connection.connect(dsn)
    assert not conn.closed
    assert conn.pgconn.status == conn.ConnStatus.OK