-
Notifications
You must be signed in to change notification settings - Fork 4
/
pgsql_test.go
134 lines (120 loc) · 2.91 KB
/
pgsql_test.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2011 John E. Barham. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package pgsql
import (
"os"
"fmt"
"time"
"math"
"bytes"
"testing"
)
type rec struct {
tf bool
i32 int
i64 int64
s string
b []byte
}
var rows = []rec{
{false, math.MinInt32, math.MinInt64, "hello world", []byte{0xDE, 0xAD}},
{true, math.MaxInt32, math.MaxInt64, "Γεια σας κόσμο", []byte{0xBE, 0xEF}},
}
func chkerr(t *testing.T, err os.Error) {
if err != nil {
t.Fatal(err)
}
}
func TestPq(t *testing.T) {
var err os.Error
var res *Result
conn, err := Connect("dbname=testdb")
chkerr(t, err)
defer conn.Close()
// Create test table, and schedule its deletion.
err = conn.Exec("CREATE TABLE gopq_test (tf bool, i32 int, i64 bigint, s text, b bytea)")
chkerr(t, err)
defer conn.Exec("DROP TABLE gopq_test")
// Insert test rows.
stmt, err := conn.Prepare("INSERT INTO gopq_test VALUES ($1, $2, $3, $4, $5)")
chkerr(t, err)
defer stmt.Clear()
for _, row := range rows {
err = stmt.Exec(row.tf, row.i32, row.i64, row.s, row.b)
chkerr(t, err)
}
// Verify that all test rows were inserted.
res, err = conn.Query("SELECT COUNT(*) FROM gopq_test")
chkerr(t, err)
if !res.Next() {
t.Fatal("Result.Next failed")
}
var count int
err = res.Scan(&count)
chkerr(t, err)
if count != len(rows) {
t.Fatalf("invalid row count %d, expected %d", count, len(rows))
}
res.Clear()
// Retrieve inserted rows and verify inserted values.
res, err = conn.Query("SELECT * FROM gopq_test")
chkerr(t, err)
t.Log("cols:", res.Names())
for i := 0; res.Next(); i++ {
t.Log("row:", i)
var tf bool
var i32 int
var i64 int64
var s string
var b []byte
err := res.Scan(&tf, &i32, &i64, &s, &b)
if err != nil {
t.Fatal("scan error:", err)
}
if tf != rows[i].tf {
t.Fatal("bad bool")
}
if i32 != rows[i].i32 {
t.Fatal("bad int32")
}
if i64 != rows[i].i64 {
t.Fatal("bad int64")
}
if s != rows[i].s {
t.Fatal("bad string")
}
if !bytes.Equal(b, rows[i].b) {
t.Fatal("bad byte array")
}
}
res.Clear()
// Test timestamp parsing.
res, err = conn.Query("SELECT now()")
var now time.Time
res.Next()
if err = res.Scan(&now); err != nil {
t.Fatal("time.Time parse error:", err)
}
t.Log("now:", now)
res.Clear()
// Test parsing of float values.
var f32 float32
res, err = conn.Query(fmt.Sprintf("SELECT %v", float32(math.MaxFloat32)))
chkerr(t, err)
res.Next()
chkerr(t, res.Scan(&f32))
if f32 != math.MaxFloat32 {
t.Fatalf("float32 conversion error: expected %v, got %v", float32(math.MaxFloat32), f32)
}
res.Clear()
var f64 float64
res, err = conn.Query(fmt.Sprintf("SELECT %v", float64(math.MaxFloat64)))
chkerr(t, err)
res.Next()
chkerr(t, res.Scan(&f64))
if f64 != math.MaxFloat64 {
t.Fatalf("float64 conversion error: expected %v, got %v", float64(math.MaxFloat64), f64)
}
res.Clear()
}