Added ability to connect to databases by name
Add documentation to readme on how to set up tests
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
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.
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
+7
-1
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user