Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
robfordww authored and jackc committed Nov 11, 2023
1 parent b4d72d4 commit 9b6d380
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions copy_from_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package pgx_test

import (
"context"
"errors"
"fmt"
"os"
"reflect"
Expand Down Expand Up @@ -802,3 +803,47 @@ func TestConnCopyFromAutomaticStringConversion(t *testing.T) {

ensureConnValid(t, conn)
}

func TestCopyFromFunc(t *testing.T) {
t.Parallel()

conn := mustConnectString(t, os.Getenv("PGX_TEST_DATABASE"))
defer closeConn(t, conn)

mustExec(t, conn, `create temporary table foo(
a int
)`)

dataCh := make(chan int, 1)
closeChanErr := errors.New("closed channel")

const channelItems = 10
go func() {
for i := 0; i < channelItems; i++ {
dataCh <- i
}
close(dataCh)
}()

copyCount, err := conn.CopyFrom(context.Background(), pgx.Identifier{"foo"}, []string{"a"},
pgx.CopyFromFunc(func() ([]any, error) {
v, ok := <-dataCh
if !ok {
return nil, closeChanErr
}
return []any{v}, nil
}))

fmt.Print(copyCount, err, "\n")

require.ErrorIs(t, err, closeChanErr)
require.EqualValues(t, channelItems, copyCount)

rows, err := conn.Query(context.Background(), "select * from foo order by a")
require.NoError(t, err)
nums, err := pgx.CollectRows(rows, pgx.RowTo[int64])
require.NoError(t, err)
require.Equal(t, []int64{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}, nums)

ensureConnValid(t, conn)
}

0 comments on commit 9b6d380

Please sign in to comment.