diff --git a/examples/booktest/mysql/db_test.go b/examples/booktest/mysql/db_test.go index 1e96d4cb8e..cffbc09fd2 100644 --- a/examples/booktest/mysql/db_test.go +++ b/examples/booktest/mysql/db_test.go @@ -5,6 +5,7 @@ package booktest import ( "context" "database/sql" + "slices" "testing" "time" @@ -155,7 +156,9 @@ func TestBooks(t *testing.T) { Title: "my book title", Yr: 2016, }) - for book := range rows.Iterate() { + + // We need to collect the iterator elements to avoid panic when we make other db queries in the loop. + for _, book := range slices.Collect(rows.Iterate()) { t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z)) author, err := dq.GetAuthor(ctx, book.AuthorID) if err != nil { diff --git a/examples/booktest/postgresql/db_test.go b/examples/booktest/postgresql/db_test.go index 7598702d8f..3c66a44c6c 100644 --- a/examples/booktest/postgresql/db_test.go +++ b/examples/booktest/postgresql/db_test.go @@ -4,6 +4,7 @@ package booktest import ( "context" + "slices" "testing" "time" @@ -144,7 +145,9 @@ func TestBooks(t *testing.T) { Title: "my book title", Year: 2016, }) - for book := range rows.Iterate() { + + // We need to collect the iterator elements to avoid panic when we make other db queries in the loop. + for _, book := range slices.Collect(rows.Iterate()) { t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Time.Format(time.RFC822Z)) author, err := dq.GetAuthor(ctx, book.AuthorID) if err != nil { diff --git a/examples/booktest/sqlite/db_test.go b/examples/booktest/sqlite/db_test.go index 7e9d7ca2de..3dccab70f0 100644 --- a/examples/booktest/sqlite/db_test.go +++ b/examples/booktest/sqlite/db_test.go @@ -4,6 +4,7 @@ package booktest import ( "context" + "slices" "testing" "time" @@ -143,7 +144,9 @@ func TestBooks(t *testing.T) { Title: "my book title", Yr: 2016, }) - for book := range rows.Iterate() { + + // We need to collect the iterator elements to avoid panic when we make other db queries in the loop. + for _, book := range slices.Collect(rows.Iterate()) { t.Logf("Book %d (%s): %s available: %s\n", book.BookID, book.BookType, book.Title, book.Available.Format(time.RFC822Z)) author, err := dq.GetAuthor(ctx, book.AuthorID) if err != nil {