github.com/tufanbarisyildirim/pop@v4.13.1+incompatible/nulls/int_test.go (about)

     1  package nulls_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/gobuffalo/pop/nulls"
     7  	"github.com/stretchr/testify/require"
     8  )
     9  
    10  func Test_IntUnmarshalJSON(t *testing.T) {
    11  	r := require.New(t)
    12  	cases := []struct {
    13  		Input []byte
    14  		Value int
    15  		Valid bool
    16  	}{
    17  		{
    18  			Input: []byte{'0'},
    19  			Value: 0,
    20  			Valid: true,
    21  		},
    22  		{
    23  			Input: []byte{'4', '2'},
    24  			Value: 42,
    25  			Valid: true,
    26  		},
    27  		{
    28  			Input: []byte{'n', 'u', 'l', 'l'},
    29  			Value: 0,
    30  			Valid: false,
    31  		},
    32  	}
    33  
    34  	for _, c := range cases {
    35  		i := nulls.Int{}
    36  		r.NoError(i.UnmarshalJSON(c.Input))
    37  		r.Equal(c.Value, i.Int)
    38  		r.Equal(c.Valid, i.Valid)
    39  	}
    40  }
    41  
    42  func Test_IntUnmarshalJSON_Errors(t *testing.T) {
    43  	r := require.New(t)
    44  
    45  	cases := []struct {
    46  		Input []byte
    47  	}{
    48  		{
    49  			Input: []byte{'a'},
    50  		},
    51  		{
    52  			Input: []byte{},
    53  		},
    54  	}
    55  
    56  	for _, c := range cases {
    57  		i := nulls.Int{}
    58  		r.Error(i.UnmarshalJSON(c.Input))
    59  		r.Equal(0, i.Int)
    60  		r.False(i.Valid)
    61  	}
    62  }