From 9191e018ff90deb99f3881966a5d356a05027e0f Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Tue, 14 Apr 2026 00:41:25 +0200 Subject: initial test coverage for Store.Select functions --- internal/assert/assert.go | 13 +++++++++++++ internal/mock/driver.go | 20 ++++++++++++-------- internal/must/must.go | 2 ++ internal/plan.go | 2 ++ 4 files changed, 29 insertions(+), 8 deletions(-) (limited to 'internal') diff --git a/internal/assert/assert.go b/internal/assert/assert.go index c82d35c..84b6ecf 100644 --- a/internal/assert/assert.go +++ b/internal/assert/assert.go @@ -23,3 +23,16 @@ func DeepEqual[V any](t testing.TB, actual, expected V) { t.Errorf("expected %#v, but got %#v", expected, actual) } } + +// SliceEqual is a test assertion. +func SliceEqual[V comparable](t testing.TB, actual []V, expected ...V) { + t.Helper() + if len(actual) != len(expected) { + t.Errorf("length mismatch: expected %#v, but got %#v", expected, actual) + } + for idx := range actual { + if actual[idx] != expected[idx] { + t.Errorf("element %d: expected %#v, but got %#v", idx, expected[idx], actual[idx]) + } + } +} diff --git a/internal/mock/driver.go b/internal/mock/driver.go index 4183097..d3358c4 100644 --- a/internal/mock/driver.go +++ b/internal/mock/driver.go @@ -75,22 +75,26 @@ func newExpectation[T any](args []any) expectation[T] { output: new(T), } for idx, arg := range args { - e.args[idx] = arg + var err error + e.args[idx], err = driver.DefaultParameterConverter.ConvertValue(arg) + if err != nil { + panic(fmt.Sprintf("could not convert value %#v into driver.Value: %s", arg, err.Error())) + } } return e } -// ExpectExec plans a response to an Exec() call. -func (rs *ResponseSet) ExpectExec(args ...any) *Result { +// ExpectExecWithArgs plans a response to an Exec() call. +func (rs *ResponseSet) ExpectExecWithArgs(args ...any) *Result { e := newExpectation[Result](args) rs.expectedExecs = append(rs.expectedExecs, e) return e.output } -// ExpectQuery plans a response to a Query() or QueryRows() call. -func (rs *ResponseSet) ExpectQuery(args ...any) *Result { - e := newExpectation[Result](args) - rs.expectedExecs = append(rs.expectedExecs, e) +// ExpectQueryWithArgs plans a response to a Query() or QueryRows() call. +func (rs *ResponseSet) ExpectQueryWithArgs(args ...any) *Rows { + e := newExpectation[Rows](args) + rs.expectedQueries = append(rs.expectedQueries, e) return e.output } @@ -258,7 +262,7 @@ func (r *Rows) AndReturnColumns(columns ...string) *Rows { // WithRow adds a row to the result set that will be returned by this query. // This may only be called after AndReturnColumns(). func (r *Rows) WithRow(values ...any) *Rows { - if len(r.columns) != 0 { + if len(r.columns) == 0 { panic("AndReturnColumns() has not been called for this Rows object yet") } if len(r.columns) != len(values) { diff --git a/internal/must/must.go b/internal/must/must.go index e472579..7a137c6 100644 --- a/internal/must/must.go +++ b/internal/must/must.go @@ -7,6 +7,7 @@ import "testing" // Succeed fails the test if err is not nil. func Succeed(t testing.TB, err error) { + t.Helper() if err != nil { t.Fatal(err.Error()) } @@ -16,6 +17,7 @@ func Succeed(t testing.TB, err error) { // and either forwards the result value on success, or fails the test on error. func Return[V any](value V, err error) func(testing.TB) V { return func(t testing.TB) V { + t.Helper() if err != nil { t.Fatal(err.Error()) } diff --git a/internal/plan.go b/internal/plan.go index f619a5f..b57b8dd 100644 --- a/internal/plan.go +++ b/internal/plan.go @@ -14,6 +14,7 @@ import ( // Plan holds all information that we can derive from reflecting on a given type. // The queries held within are only valid within the context of a given SQL dialect. type Plan struct { + TypeName string // for use in error messages TableName string // from info.TableNameIs marker (if any) AllColumnNames []string // in order of struct fields PrimaryKeyColumnNames []string // from info.PrimaryKeyIs marker (if any) @@ -64,6 +65,7 @@ func buildPlan(t reflect.Type, dialect Dialect, opts PlanOpts) (Plan, error) { } var p = Plan{ + TypeName: t.Name(), TableName: opts.TableName, PrimaryKeyColumnNames: opts.PrimaryKeyColumnNames, IndexByColumnName: make(map[string][]int), -- cgit v1.2.3