diff --git a/rows.go b/rows.go index 6483bcb..fdf000a 100644 --- a/rows.go +++ b/rows.go @@ -275,6 +275,14 @@ func (r *Rows) FromCSVString(s string) *Rows { return r } +// Kind returns rows corresponding to the interface pgx.Rows +// useful for testing entities that implement an interface pgx.RowScanner +func (r *Rows) Kind() pgx.Rows { + return &rowSets{ + sets: []*Rows{r}, + } +} + // NewRowsWithColumnDefinition return rows with columns metadata func NewRowsWithColumnDefinition(columns ...pgconn.FieldDescription) *Rows { return &Rows{ diff --git a/rows_test.go b/rows_test.go index 0d8cb15..62f43f6 100644 --- a/rows_test.go +++ b/rows_test.go @@ -671,3 +671,32 @@ func TestMockQueryWithCollect(t *testing.T) { func TestRowsConn(t *testing.T) { assert.Nil(t, (&rowSets{}).Conn()) } + +func TestRowsKind(t *testing.T) { + var alphabet = []string{"a", "b", "c", "d", "e", "f"} + rows := NewRows([]string{"id", "alphabet"}) + + for id, b := range alphabet { + rows.AddRow(id, b) + } + + kindRows := rows.Kind() + + for i := 0; kindRows.Next(); i++ { + var ( + letter string + index int + ) + if err := kindRows.Scan(&index, &letter); err != nil { + t.Fatalf("unexpected error: %s", err) + } + + if index != i { + t.Fatalf("expected %d, but got %d", i, index) + } + + if letter != alphabet[i] { + t.Fatalf("expected %s, but got %s", alphabet[i], letter) + } + } +}