From b501ed285aa8a5bd17ebb66d1b2fcc8ce4d1ee40 Mon Sep 17 00:00:00 2001 From: labile <1a6i1e@gmail.com> Date: Wed, 1 Nov 2023 05:12:31 +0300 Subject: [PATCH 1/2] add Kind() method to convert *Rows to pgx.Rows interface --- rows.go | 8 ++++++++ 1 file changed, 8 insertions(+) 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{ From 013790f99ac3404b279089bce4273970e8550e6e Mon Sep 17 00:00:00 2001 From: labile <1a6i1e@gmail.com> Date: Thu, 2 Nov 2023 19:00:21 +0300 Subject: [PATCH 2/2] test for Rows.Kind() method --- rows_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) 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) + } + } +}