From f18de44b351f56e0de727dcd39240bd0b476fc16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=92=D0=BB=D0=B0=D0=B4=D0=B8=D0=BC=D0=B8=D1=80=20=D0=90?= =?UTF-8?q?=D1=82=D0=B0=D0=BC=D0=B0=D0=BD=D0=BE=D0=B2?= Date: Sat, 19 Oct 2024 22:08:00 +0300 Subject: [PATCH] fix tests --- examples/booktest/mysql/db_test.go | 5 ++++- examples/booktest/postgresql/db_test.go | 5 ++++- examples/booktest/sqlite/db_test.go | 5 ++++- 3 files changed, 12 insertions(+), 3 deletions(-) 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 {