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

     1  package pgsql_test
     2  
     3  import (
     4  	"database/sql/driver"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"github.com/acoshift/pgsql"
    10  )
    11  
    12  func TestNull_Value(t *testing.T) {
    13  	t.Parallel()
    14  
    15  	t.Run("Int64 valid", func(t *testing.T) {
    16  		x := 1
    17  		v, err := pgsql.Null(&x).Value()
    18  		assert.NoError(t, err)
    19  		assert.Equal(t, x, v)
    20  	})
    21  
    22  	t.Run("Int64 zero", func(t *testing.T) {
    23  		x := 0
    24  		v, err := pgsql.Null(&x).Value()
    25  		assert.NoError(t, err)
    26  		assert.Nil(t, v)
    27  	})
    28  
    29  	t.Run("Valuer valid", func(t *testing.T) {
    30  		x := testValuer{1}
    31  		v, err := pgsql.Null(&x).Value()
    32  		assert.NoError(t, err)
    33  		assert.Equal(t, x.x, v)
    34  	})
    35  
    36  	t.Run("Valuer zero", func(t *testing.T) {
    37  		x := testValuer{0}
    38  		v, err := pgsql.Null(&x).Value()
    39  		assert.NoError(t, err)
    40  		assert.Nil(t, v)
    41  	})
    42  
    43  	t.Run("Valuer nil", func(t *testing.T) {
    44  		var x *testValuer
    45  		v, err := pgsql.Null(x).Value()
    46  		assert.NoError(t, err)
    47  		assert.Nil(t, v)
    48  	})
    49  }
    50  
    51  type testValuer struct {
    52  	x int64
    53  }
    54  
    55  func (v testValuer) Value() (driver.Value, error) {
    56  	return v.x, nil
    57  }
    58  
    59  func (v testValuer) IsZero() bool {
    60  	return v.x == 0
    61  }