aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/database/sql/sql_test.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/database/sql/sql_test.go')
-rw-r--r--libgo/go/database/sql/sql_test.go90
1 files changed, 83 insertions, 7 deletions
diff --git a/libgo/go/database/sql/sql_test.go b/libgo/go/database/sql/sql_test.go
index 64b9dfe..f68cefe 100644
--- a/libgo/go/database/sql/sql_test.go
+++ b/libgo/go/database/sql/sql_test.go
@@ -131,6 +131,7 @@ func TestDriverPanic(t *testing.T) {
}
func exec(t testing.TB, db *DB, query string, args ...interface{}) {
+ t.Helper()
_, err := db.Exec(query, args...)
if err != nil {
t.Fatalf("Exec of %q: %v", query, err)
@@ -1338,6 +1339,54 @@ func TestConnQuery(t *testing.T) {
}
}
+func TestConnRaw(t *testing.T) {
+ db := newTestDB(t, "people")
+ defer closeDB(t, db)
+
+ ctx, cancel := context.WithCancel(context.Background())
+ defer cancel()
+ conn, err := db.Conn(ctx)
+ if err != nil {
+ t.Fatal(err)
+ }
+ conn.dc.ci.(*fakeConn).skipDirtySession = true
+ defer conn.Close()
+
+ sawFunc := false
+ err = conn.Raw(func(dc interface{}) error {
+ sawFunc = true
+ if _, ok := dc.(*fakeConn); !ok {
+ return fmt.Errorf("got %T want *fakeConn", dc)
+ }
+ return nil
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !sawFunc {
+ t.Fatal("Raw func not called")
+ }
+
+ func() {
+ defer func() {
+ x := recover()
+ if x == nil {
+ t.Fatal("expected panic")
+ }
+ conn.closemu.Lock()
+ closed := conn.dc == nil
+ conn.closemu.Unlock()
+ if !closed {
+ t.Fatal("expected connection to be closed after panic")
+ }
+ }()
+ err = conn.Raw(func(dc interface{}) error {
+ panic("Conn.Raw panic should return an error")
+ })
+ t.Fatal("expected panic from Raw func")
+ }()
+}
+
func TestCursorFake(t *testing.T) {
db := newTestDB(t, "people")
defer closeDB(t, db)
@@ -1401,7 +1450,7 @@ func TestInvalidNilValues(t *testing.T) {
{
name: "int",
input: &date2,
- expectedError: `sql: Scan error on column index 0, name "bdate": converting driver.Value type <nil> ("<nil>") to a int: invalid syntax`,
+ expectedError: `sql: Scan error on column index 0, name "bdate": converting NULL to int is unsupported`,
},
}
@@ -1671,6 +1720,18 @@ func TestNullInt64Param(t *testing.T) {
nullTestRun(t, spec)
}
+func TestNullInt32Param(t *testing.T) {
+ spec := nullTestSpec{"nullint32", "int32", [6]nullTestRow{
+ {NullInt32{31, true}, 1, NullInt32{31, true}},
+ {NullInt32{-22, false}, 1, NullInt32{0, false}},
+ {22, 1, NullInt32{22, true}},
+ {NullInt32{33, true}, 1, NullInt32{33, true}},
+ {NullInt32{222, false}, 1, NullInt32{0, false}},
+ {0, NullInt32{31, false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
func TestNullFloat64Param(t *testing.T) {
spec := nullTestSpec{"nullfloat64", "float64", [6]nullTestRow{
{NullFloat64{31.2, true}, 1, NullFloat64{31.2, true}},
@@ -1695,6 +1756,21 @@ func TestNullBoolParam(t *testing.T) {
nullTestRun(t, spec)
}
+func TestNullTimeParam(t *testing.T) {
+ t0 := time.Time{}
+ t1 := time.Date(2000, 1, 1, 8, 9, 10, 11, time.UTC)
+ t2 := time.Date(2010, 1, 1, 8, 9, 10, 11, time.UTC)
+ spec := nullTestSpec{"nulldatetime", "datetime", [6]nullTestRow{
+ {NullTime{t1, true}, t2, NullTime{t1, true}},
+ {NullTime{t1, false}, t2, NullTime{t0, false}},
+ {t1, t2, NullTime{t1, true}},
+ {NullTime{t1, true}, t2, NullTime{t1, true}},
+ {NullTime{t1, false}, t2, NullTime{t0, false}},
+ {t2, NullTime{t1, false}, nil},
+ }}
+ nullTestRun(t, spec)
+}
+
func nullTestRun(t *testing.T, spec nullTestSpec) {
db := newTestDB(t, "")
defer closeDB(t, db)
@@ -3530,7 +3606,7 @@ type nvcConn struct {
skipNamedValueCheck bool
}
-type decimal struct {
+type decimalInt struct {
value int
}
@@ -3554,7 +3630,7 @@ func (c *nvcConn) CheckNamedValue(nv *driver.NamedValue) error {
nv.Value = "OUT:*string"
}
return nil
- case decimal, []int64:
+ case decimalInt, []int64:
return nil
case doNotInclude:
return driver.ErrRemoveArgument
@@ -3583,13 +3659,13 @@ func TestNamedValueChecker(t *testing.T) {
}
o1 := ""
- _, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A,str1=?,out1=?O1,array1=?", Named("A", decimal{123}), "hello", Named("O1", Out{Dest: &o1}), []int64{42, 128, 707}, doNotInclude{})
+ _, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A,str1=?,out1=?O1,array1=?", Named("A", decimalInt{123}), "hello", Named("O1", Out{Dest: &o1}), []int64{42, 128, 707}, doNotInclude{})
if err != nil {
t.Fatal("exec insert", err)
}
var (
str1 string
- dec1 decimal
+ dec1 decimalInt
arr1 []int64
)
err = db.QueryRowContext(ctx, "SELECT|keys|dec1,str1,array1|").Scan(&dec1, &str1, &arr1)
@@ -3599,7 +3675,7 @@ func TestNamedValueChecker(t *testing.T) {
list := []struct{ got, want interface{} }{
{o1, "from-server"},
- {dec1, decimal{123}},
+ {dec1, decimalInt{123}},
{str1, "hello"},
{arr1, []int64{42, 128, 707}},
}
@@ -3632,7 +3708,7 @@ func TestNamedValueCheckerSkip(t *testing.T) {
t.Fatal("exec create", err)
}
- _, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A", Named("A", decimal{123}))
+ _, err = db.ExecContext(ctx, "INSERT|keys|dec1=?A", Named("A", decimalInt{123}))
if err == nil {
t.Fatalf("expected error with bad argument, got %v", err)
}