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

     1  package pgsql_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  	"github.com/stretchr/testify/require"
     8  
     9  	"github.com/acoshift/pgsql"
    10  )
    11  
    12  func TestNullString(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	db := open(t)
    16  	defer db.Close()
    17  
    18  	_, err := db.Exec(`
    19  		drop table if exists test_pgsql_null_string;
    20  		create table test_pgsql_null_string (
    21  			id int primary key,
    22  			value varchar
    23  		);
    24  		insert into test_pgsql_null_string (
    25  			id, value
    26  		) values
    27  			(0, 'hello'),
    28  			(1, null);
    29  	`)
    30  	require.NoError(t, err)
    31  	defer db.Exec(`drop table test_pgsql_null_string`)
    32  
    33  	t.Run("Scan", func(t *testing.T) {
    34  		{
    35  			var p string
    36  			err = db.QueryRow(`select value from test_pgsql_null_string where id = 0`).Scan(pgsql.NullString(&p))
    37  			require.NoError(t, err)
    38  			assert.Equal(t, "hello", p)
    39  		}
    40  
    41  		{
    42  			var p string
    43  			err = db.QueryRow(`select value from test_pgsql_null_string where id = 1`).Scan(pgsql.NullString(&p))
    44  			require.NoError(t, err)
    45  			assert.Equal(t, "", p)
    46  		}
    47  	})
    48  }