def test_broken_pipe_read(con, db_kwargs): db1 = Connection(**db_kwargs) res = db1.run("select pg_backend_pid()") pid1 = res[0][0] con.run("select pg_terminate_backend(:v)", v=pid1) with pytest.raises(InterfaceError, match="network error on read"): db1.run("select 1")
def test_unix_socket_missing(): conn_params = { "unix_sock": "/file-does-not-exist", "user": "******" } with pytest.raises(InterfaceError): Connection(**conn_params)
def test_application_name_integer(db_kwargs): db_kwargs["application_name"] = 1 with pytest.raises( InterfaceError, match= "The parameter application_name can't be of type <class 'int'>.", ): Connection(**db_kwargs)
def test_internet_socket_connection_refused(): conn_params = {"port": 0, "user": "******"} with pytest.raises( InterfaceError, match="Can't create a connection to host localhost and port 0 " "\\(timeout is None and source_address is None\\).", ): Connection(**conn_params)
def test_application_name(db_kwargs): app_name = "my test application name" db_kwargs["application_name"] = app_name with Connection(**db_kwargs) as db: res = db.run("select application_name from pg_stat_activity " " where pid = pg_backend_pid()") application_name = res[0][0] assert application_name == app_name
def test_bytes_password(con, db_kwargs): # Create user username = "******" password = "******" con.run("create user " + username + " with password '" + password + "';") db_kwargs["user"] = username db_kwargs["password"] = password.encode("utf8") db_kwargs["database"] = "pg8000_md5" with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs) con.run("drop role " + username)
def test_broken_pipe_flush(con, db_kwargs): db1 = Connection(**db_kwargs) res = db1.run("select pg_backend_pid()") pid1 = res[0][0] con.run("select pg_terminate_backend(:v)", v=pid1) try: db1.run("select 1") except BaseException: pass # Sometimes raises and sometime doesn't try: db1.close() except InterfaceError as e: assert str(e) == "network error on flush"
def test_bytes_databaseName(db_kwargs): """Should only raise an exception saying db doesn't exist""" db_kwargs["database"] = bytes("pg8000_sn\uFF6Fw", "utf8") with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs)
def test_unicode_databaseName(db_kwargs): db_kwargs["database"] = "pg8000_sn\uFF6Fw" # Should only raise an exception saying db doesn't exist with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs)
def test_password(db_kwargs): db_kwargs["database"] = "pg8000_password" # Should only raise an exception saying db doesn't exist with pytest.raises(DatabaseError, match="3D000"): Connection(**db_kwargs)
def test_database_missing(db_kwargs): db_kwargs["database"] = "missing-db" with pytest.raises(DatabaseError): Connection(**db_kwargs)
def test_application_name_bytearray(db_kwargs): db_kwargs["application_name"] = bytearray(b"Philby") Connection(**db_kwargs)