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

     1  package pgsql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  
     8  	"github.com/acoshift/pgsql"
     9  )
    10  
    11  func TestScan(t *testing.T) {
    12  	t.Parallel()
    13  
    14  	db := open(t)
    15  	defer db.Close()
    16  
    17  	_, err := db.Exec(`
    18  		drop table if exists test_pgsql_scan;
    19  		create table test_pgsql_scan (
    20  			id int primary key,
    21  			json_value json,
    22  			array_value bigint[]
    23  		);
    24  		insert into test_pgsql_scan (id, json_value, array_value) values (1, '{"a": "test", "b": 7}', '{1, 2, 3}');
    25  	`)
    26  	if !assert.NoError(t, err) {
    27  		return
    28  	}
    29  	defer db.Exec(`drop table test_pgsql_scan`)
    30  
    31  	var obj struct {
    32  		A string
    33  		B int
    34  	}
    35  	var arr []int64
    36  
    37  	err = pgsql.Scan(db.QueryRow(`
    38  		select json_value, array_value
    39  		from test_pgsql_scan
    40  		where id = 1
    41  	`).Scan)(&obj, &arr)
    42  	if !assert.NoError(t, err) {
    43  		return
    44  	}
    45  
    46  	assert.Equal(t, "test", obj.A)
    47  	assert.Equal(t, 7, obj.B)
    48  	assert.Equal(t, []int64{1, 2, 3}, arr)
    49  }