Skip to content

Commit

Permalink
Merge pull request #168 from labi-le/master
Browse files Browse the repository at this point in the history
[+] add `Kind()` method to convert `*Rows` to `pgx.Rows` interface
  • Loading branch information
pashagolub authored Nov 2, 2023
2 parents d47eaa0 + 013790f commit 7f5256f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
8 changes: 8 additions & 0 deletions rows.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
29 changes: 29 additions & 0 deletions rows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}

0 comments on commit 7f5256f

Please sign in to comment.