Added ability to connect to databases by name

Add documentation to readme on how to set up tests
pgx-vs-pq
Jack Christensen 2013-04-11 08:56:31 -05:00
parent bf0891db7d
commit 996aed65ee
3 changed files with 21 additions and 2 deletions

View File

@ -2,3 +2,8 @@ pgx
===
Experimental PostgreSQL client library for Go
Testing
-------
To setup the test environment create a database named pgx_test.

View File

@ -17,6 +17,9 @@ type conn struct {
txStatus byte
}
// options:
// socket: path to unix domain socket
// database: name of database
func Connect(options map[string]string) (c *conn, err error) {
c = new(conn)
@ -35,7 +38,12 @@ func Connect(options map[string]string) (c *conn, err error) {
// conn, err := net.Dial("tcp", "localhost:5432")
var database string
msg := newStartupMessage()
if database, present = options["database"]; present {
msg.options["database"] = database
}
msg.options["user"] = "jack"
c.txStartupMessage(msg)

View File

@ -5,7 +5,7 @@ import (
)
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 {
t.Fatal("Unable to establish connection")
}
@ -22,6 +22,12 @@ func TestConnect(t *testing.T) {
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()
if err != nil {
t.Fatal("Unable to close connection")