Skip to content

Commit f18de44

Browse files
committed
fix tests
1 parent 8e385b4 commit f18de44

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

examples/booktest/mysql/db_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package booktest
55
import (
66
"context"
77
"database/sql"
8+
"slices"
89
"testing"
910
"time"
1011

@@ -155,7 +156,9 @@ func TestBooks(t *testing.T) {
155156
Title: "my book title",
156157
Yr: 2016,
157158
})
158-
for book := range rows.Iterate() {
159+
160+
// We need to collect the iterator elements to avoid panic when we make other db queries in the loop.
161+
for _, book := range slices.Collect(rows.Iterate()) {
159162
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
160163
author, err := dq.GetAuthor(ctx, book.AuthorID)
161164
if err != nil {

examples/booktest/postgresql/db_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package booktest
44

55
import (
66
"context"
7+
"slices"
78
"testing"
89
"time"
910

@@ -144,7 +145,9 @@ func TestBooks(t *testing.T) {
144145
Title: "my book title",
145146
Year: 2016,
146147
})
147-
for book := range rows.Iterate() {
148+
149+
// We need to collect the iterator elements to avoid panic when we make other db queries in the loop.
150+
for _, book := range slices.Collect(rows.Iterate()) {
148151
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z))
149152
author, err := dq.GetAuthor(ctx, book.AuthorID)
150153
if err != nil {

examples/booktest/sqlite/db_test.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package booktest
44

55
import (
66
"context"
7+
"slices"
78
"testing"
89
"time"
910

@@ -143,7 +144,9 @@ func TestBooks(t *testing.T) {
143144
Title: "my book title",
144145
Yr: 2016,
145146
})
146-
for book := range rows.Iterate() {
147+
148+
// We need to collect the iterator elements to avoid panic when we make other db queries in the loop.
149+
for _, book := range slices.Collect(rows.Iterate()) {
147150
t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z))
148151
author, err := dq.GetAuthor(ctx, book.AuthorID)
149152
if err != nil {

0 commit comments

Comments
 (0)