github.com/acoshift/pgsql@v0.15.3/pgstmt/result_test.go (about)

     1  package pgstmt_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/acoshift/pgsql"
    10  	"github.com/acoshift/pgsql/pgctx"
    11  	"github.com/acoshift/pgsql/pgstmt"
    12  )
    13  
    14  func TestResult_QueryAllWith(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	db := open(t)
    18  	defer db.Close()
    19  
    20  	ctx := context.Background()
    21  	ctx = pgctx.NewContext(ctx, db)
    22  
    23  	type v struct {
    24  		a int
    25  		b int
    26  	}
    27  
    28  	var vs []*v
    29  	err := pgstmt.Select(func(b pgstmt.SelectStatement) {
    30  		b.Columns("*")
    31  		b.FromValues(func(b pgstmt.Values) {
    32  			b.Value("1", "2")
    33  			b.Value("3", "4")
    34  			b.Value("5", "6")
    35  		}, "t")
    36  	}).IterWith(ctx, func(scan pgsql.Scanner) error {
    37  		var x v
    38  		err := scan(&x.a, &x.b)
    39  		if err != nil {
    40  			return err
    41  		}
    42  		vs = append(vs, &x)
    43  		return nil
    44  	})
    45  
    46  	assert.NoError(t, err)
    47  	if assert.Len(t, vs, 3) {
    48  		assert.EqualValues(t, []*v{
    49  			{1, 2},
    50  			{3, 4},
    51  			{5, 6},
    52  		}, vs)
    53  	}
    54  }