From 52f44287216b47149da9eb3f038408447f0e5981 Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Fri, 17 Apr 2026 14:53:52 +0200 Subject: improve test coverage, error reporting for Select() --- internal/assert/assert.go | 8 ++++++++ internal/mock/mock.go | 12 +++++++++--- 2 files changed, 17 insertions(+), 3 deletions(-) (limited to 'internal') diff --git a/internal/assert/assert.go b/internal/assert/assert.go index 26f91ff..6e641ca 100644 --- a/internal/assert/assert.go +++ b/internal/assert/assert.go @@ -4,6 +4,8 @@ package assert import ( + "cmp" + "errors" "reflect" "testing" ) @@ -26,6 +28,12 @@ func DeepEqual[V any](t testing.TB, actual, expected V) { } } +// ErrEqual is a test assertion. +func ErrEqual(t testing.TB, actual error, expected string) { + t.Helper() + Equal(t, cmp.Or(actual, errors.New("")).Error(), expected) +} + // SliceEqual is a test assertion. func SliceEqual[V comparable](t testing.TB, actual []V, expected ...V) { t.Helper() diff --git a/internal/mock/mock.go b/internal/mock/mock.go index d3358c4..ecbb03e 100644 --- a/internal/mock/mock.go +++ b/internal/mock/mock.go @@ -245,8 +245,9 @@ func (r result) RowsAffected() (int64, error) { // Rows is a mock response for a Query() or QueryRow() call. // It is constructed by [ResponseSet.ExpectQuery]. type Rows struct { - columns []string - results [][]any + columns []string + results [][]any + closeError error } // AndReturnColumns configures the set of column names that will be returend by this query. @@ -272,6 +273,11 @@ func (r *Rows) WithRow(values ...any) *Rows { return r } +// AndCloseFailsWith sets up Close() for this Rows to fail with the provided error message. +func (r *Rows) AndCloseFailsWith(err error) { + r.closeError = err +} + type rows struct { r Rows closed bool @@ -285,7 +291,7 @@ func (r *rows) Columns() []string { // Close implements the [driver.Rows] interface. func (r *rows) Close() error { r.closed = true - return nil + return r.r.closeError } // Next implements the [driver.Rows] interface. -- cgit v1.2.3