mirror of https://github.com/jackc/pgx.git
Added ability to connect to databases by name
Add documentation to readme on how to set up testspgx-vs-pq
parent
bf0891db7d
commit
996aed65ee
|
@ -2,3 +2,8 @@ pgx
|
||||||
===
|
===
|
||||||
|
|
||||||
Experimental PostgreSQL client library for Go
|
Experimental PostgreSQL client library for Go
|
||||||
|
|
||||||
|
Testing
|
||||||
|
-------
|
||||||
|
|
||||||
|
To setup the test environment create a database named pgx_test.
|
||||||
|
|
8
conn.go
8
conn.go
|
@ -17,6 +17,9 @@ type conn struct {
|
||||||
txStatus byte
|
txStatus byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// options:
|
||||||
|
// socket: path to unix domain socket
|
||||||
|
// database: name of database
|
||||||
func Connect(options map[string]string) (c *conn, err error) {
|
func Connect(options map[string]string) (c *conn, err error) {
|
||||||
c = new(conn)
|
c = new(conn)
|
||||||
|
|
||||||
|
@ -35,7 +38,12 @@ func Connect(options map[string]string) (c *conn, err error) {
|
||||||
|
|
||||||
// conn, err := net.Dial("tcp", "localhost:5432")
|
// conn, err := net.Dial("tcp", "localhost:5432")
|
||||||
|
|
||||||
|
var database string
|
||||||
|
|
||||||
msg := newStartupMessage()
|
msg := newStartupMessage()
|
||||||
|
if database, present = options["database"]; present {
|
||||||
|
msg.options["database"] = database
|
||||||
|
}
|
||||||
msg.options["user"] = "jack"
|
msg.options["user"] = "jack"
|
||||||
c.txStartupMessage(msg)
|
c.txStartupMessage(msg)
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestConnect(t *testing.T) {
|
func TestConnect(t *testing.T) {
|
||||||
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432"})
|
conn, err := Connect(map[string]string{"socket": "/private/tmp/.s.PGSQL.5432", "database": "pgx_test"})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to establish connection")
|
t.Fatal("Unable to establish connection")
|
||||||
}
|
}
|
||||||
|
@ -22,6 +22,12 @@ func TestConnect(t *testing.T) {
|
||||||
t.Error("Backend secret key not stored")
|
t.Error("Backend secret key not stored")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var rows []map[string]string
|
||||||
|
rows, err = conn.Query("select current_database()")
|
||||||
|
if err != nil || rows[0]["current_database"] != "pgx_test" {
|
||||||
|
t.Error("Did not connect to specified database (pgx_text)")
|
||||||
|
}
|
||||||
|
|
||||||
err = conn.Close()
|
err = conn.Close()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("Unable to close connection")
|
t.Fatal("Unable to close connection")
|
||||||
|
|
Loading…
Reference in New Issue