MCPcopy
hub / github.com/jackc/pgx / TestConnect

Function TestConnect

conn_test.go:49–84  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

47}
48
49func TestConnect(t *testing.T) {
50 t.Parallel()
51
52 connString := os.Getenv("PGX_TEST_DATABASE")
53 config := mustParseConfig(t, connString)
54
55 conn, err := pgx.ConnectConfig(context.Background(), config)
56 if err != nil {
57 t.Fatalf("Unable to establish connection: %v", err)
58 }
59
60 assertConfigsEqual(t, config, conn.Config(), "Conn.Config() returns original config")
61
62 var currentDB string
63 err = conn.QueryRow(context.Background(), "select current_database()").Scan(&currentDB)
64 if err != nil {
65 t.Fatalf("QueryRow Scan unexpectedly failed: %v", err)
66 }
67 if currentDB != config.Config.Database {
68 t.Errorf("Did not connect to specified database (%v)", config.Config.Database)
69 }
70
71 var user string
72 err = conn.QueryRow(context.Background(), "select current_user").Scan(&user)
73 if err != nil {
74 t.Fatalf("QueryRow Scan unexpectedly failed: %v", err)
75 }
76 if user != config.Config.User {
77 t.Errorf("Did not connect as specified user (%v)", config.Config.User)
78 }
79
80 err = conn.Close(context.Background())
81 if err != nil {
82 t.Fatal("Unable to close connection")
83 }
84}
85
86func TestConnectWithPreferSimpleProtocol(t *testing.T) {
87 t.Parallel()

Callers

nothing calls this directly

Calls 6

mustParseConfigFunction · 0.70
assertConfigsEqualFunction · 0.70
ScanMethod · 0.65
QueryRowMethod · 0.65
CloseMethod · 0.65
ConfigMethod · 0.45

Tested by

no test coverage detected