Skip to content

Commit 2925510

Browse files
jshanelsam
authored andcommitted
Check error if rows.Next returns false. (#365)
* Check error if rows.Next returns false. Per https://golang.org/pkg/database/sql/#Rows.Next, > returns true on success, or false if there is no next result row or an error > happened while preparing it. Err should be consulted to distinguish between > the two cases. * Fix up vet errors and ensure lowercasefields is reset in postgres tests
1 parent c5fd513 commit 2925510

4 files changed

Lines changed: 18 additions & 10 deletions

File tree

dialect_postgres_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ var _ = Describe("PostgresDialect", func() {
3030
dialect gorp.PostgresDialect
3131
)
3232

33+
BeforeEach(func() {
34+
lowercasefields = false
35+
})
36+
3337
JustBeforeEach(func() {
3438
dialect = gorp.PostgresDialect{
3539
LowercaseFields: lowercasefields,

gorp_go18_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,10 @@ package gorp_test
1616

1717
import (
1818
"context"
19-
"github.com/stretchr/testify/assert"
2019
"testing"
2120
"time"
21+
22+
"github.com/stretchr/testify/assert"
2223
)
2324

2425
// Drivers that don't support cancellation.
@@ -47,12 +48,12 @@ func TestWithNotCanceledContext(t *testing.T) {
4748
func TestWithCanceledContext(t *testing.T) {
4849
dialect, driver := dialectAndDriver()
4950
if unsupportedDrivers[driver] {
50-
t.Skip("Cancellation is not yet supported by all drivers. Not known to be supported in %s.", driver)
51+
t.Skipf("Cancellation is not yet supported by all drivers. Not known to be supported in %s.", driver)
5152
}
5253

5354
sleepDialect, ok := dialect.(SleepDialect)
5455
if !ok {
55-
t.Skip("Sleep is not supported in all dialects. Not known to be supported in %s.", driver)
56+
t.Skipf("Sleep is not supported in all dialects. Not known to be supported in %s.", driver)
5657
}
5758

5859
dbmap := initDbMap()

gorp_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,15 +1766,15 @@ func TestSelectVal(t *testing.T) {
17661766
// SelectFloat
17671767
f64 := selectFloat(dbmap, "select "+columnName(dbmap, TableWithNull{}, "Float64")+" from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"='abc'")
17681768
if f64 != 32.2 {
1769-
t.Errorf("float64 %d != 32.2", f64)
1769+
t.Errorf("float64 %f != 32.2", f64)
17701770
}
17711771
f64 = selectFloat(dbmap, "select min("+columnName(dbmap, TableWithNull{}, "Float64")+") from "+tableName(dbmap, TableWithNull{}))
17721772
if f64 != 32.2 {
1773-
t.Errorf("float64 min %d != 32.2", f64)
1773+
t.Errorf("float64 min %f != 32.2", f64)
17741774
}
17751775
f64 = selectFloat(dbmap, "select count(*) from "+tableName(dbmap, TableWithNull{})+" where "+columnName(dbmap, TableWithNull{}, "Str")+"="+bindVar, "asdfasdf")
17761776
if f64 != 0 {
1777-
t.Errorf("float64 no rows %d != 0", f64)
1777+
t.Errorf("float64 no rows %f != 0", f64)
17781778
}
17791779

17801780
// SelectNullFloat
@@ -1885,13 +1885,13 @@ func TestNullTime(t *testing.T) {
18851885
}}
18861886
err := dbmap.Insert(ent)
18871887
if err != nil {
1888-
t.Error("failed insert on %s", err.Error())
1888+
t.Errorf("failed insert on %s", err.Error())
18891889
}
18901890
err = dbmap.SelectOne(ent, `select * from nulltime_test where `+columnName(dbmap, WithNullTime{}, "Id")+`=:Id`, map[string]interface{}{
18911891
"Id": ent.Id,
18921892
})
18931893
if err != nil {
1894-
t.Error("failed select on %s", err.Error())
1894+
t.Errorf("failed select on %s", err.Error())
18951895
}
18961896
if ent.Time.Valid {
18971897
t.Error("gorp.NullTime returns valid but expected null.")
@@ -1907,13 +1907,13 @@ func TestNullTime(t *testing.T) {
19071907
}}
19081908
err = dbmap.Insert(ent)
19091909
if err != nil {
1910-
t.Error("failed insert on %s", err.Error())
1910+
t.Errorf("failed insert on %s", err.Error())
19111911
}
19121912
err = dbmap.SelectOne(ent, `select * from nulltime_test where `+columnName(dbmap, WithNullTime{}, "Id")+`=:Id`, map[string]interface{}{
19131913
"Id": ent.Id,
19141914
})
19151915
if err != nil {
1916-
t.Error("failed select on %s", err.Error())
1916+
t.Errorf("failed select on %s", err.Error())
19171917
}
19181918
if !ent.Time.Valid {
19191919
t.Error("gorp.NullTime returns invalid but expected valid.")

select.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ func selectVal(e SqlExecutor, holder interface{}, query string, args ...interfac
169169
defer rows.Close()
170170

171171
if !rows.Next() {
172+
if err := rows.Err(); err != nil {
173+
return err
174+
}
172175
return sql.ErrNoRows
173176
}
174177

0 commit comments

Comments
 (0)