-
-
Notifications
You must be signed in to change notification settings - Fork 955
Expand file tree
/
Copy pathnotice_test.go
More file actions
52 lines (47 loc) · 1.46 KB
/
notice_test.go
File metadata and controls
52 lines (47 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package pq
import (
"database/sql"
"database/sql/driver"
"fmt"
"testing"
"github.com/lib/pq/internal/pqtest"
)
func TestConnectorWithNoticeHandler_Simple(t *testing.T) {
raise := func(c driver.Connector, t *testing.T, n string) {
db := sql.OpenDB(c)
defer db.Close()
pqtest.Exec(t, db, fmt.Sprintf(`
do language plpgsql $$ begin
raise notice '%s';
end $$
`, n))
}
b, err := NewConnector(pqtest.DSN(""))
if err != nil {
t.Fatal(err)
}
var notice *Error
// Make connector w/ handler to set the local var
c := ConnectorWithNoticeHandler(b, func(n *Error) { notice = n })
raise(c, t, "Test notice #1")
if notice == nil || notice.Message != "Test notice #1" {
t.Fatalf("wrong message:\nhave: %q\nwant: %q", notice.Message, "Test notice #1")
}
// Unset the handler on the same connector
prevC := c
if c = ConnectorWithNoticeHandler(c, nil); c != prevC {
t.Fatalf("Expected to not create new connector but did")
}
raise(c, t, "Test notice #2")
if notice == nil || notice.Message != "Test notice #1" {
t.Fatalf("wrong message:\nhave: %q\nwant: %q", notice.Message, "Test notice #1")
}
// Set it back on the same connector
if c = ConnectorWithNoticeHandler(c, func(n *Error) { notice = n }); c != prevC {
t.Fatal("Expected to not create new connector but did")
}
raise(c, t, "Test notice #3")
if notice == nil || notice.Message != "Test notice #3" {
t.Fatalf("wrong message:\nhave: %q\nwant: %q", notice.Message, "Test notice #3")
}
}