...

Source file src/github.com/jackc/pgtype/int8_test.go

Documentation: github.com/jackc/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"math"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/jackc/pgtype"
     9  	"github.com/jackc/pgtype/testutil"
    10  )
    11  
    12  func TestInt8Transcode(t *testing.T) {
    13  	testutil.TestSuccessfulTranscode(t, "int8", []interface{}{
    14  		&pgtype.Int8{Int: math.MinInt64, Status: pgtype.Present},
    15  		&pgtype.Int8{Int: -1, Status: pgtype.Present},
    16  		&pgtype.Int8{Int: 0, Status: pgtype.Present},
    17  		&pgtype.Int8{Int: 1, Status: pgtype.Present},
    18  		&pgtype.Int8{Int: math.MaxInt64, Status: pgtype.Present},
    19  		&pgtype.Int8{Int: 0, Status: pgtype.Null},
    20  	})
    21  }
    22  
    23  func TestInt8Set(t *testing.T) {
    24  	successfulTests := []struct {
    25  		source interface{}
    26  		result pgtype.Int8
    27  	}{
    28  		{source: int8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    29  		{source: int16(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    30  		{source: int32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    31  		{source: int64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    32  		{source: int8(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}},
    33  		{source: int16(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}},
    34  		{source: int32(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}},
    35  		{source: int64(-1), result: pgtype.Int8{Int: -1, Status: pgtype.Present}},
    36  		{source: uint8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    37  		{source: uint16(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    38  		{source: uint32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    39  		{source: uint64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    40  		{source: float32(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    41  		{source: float64(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    42  		{source: "1", result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    43  		{source: _int8(1), result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
    44  	}
    45  
    46  	for i, tt := range successfulTests {
    47  		var r pgtype.Int8
    48  		err := r.Set(tt.source)
    49  		if err != nil {
    50  			t.Errorf("%d: %v", i, err)
    51  		}
    52  
    53  		if r != tt.result {
    54  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    55  		}
    56  	}
    57  }
    58  
    59  func TestInt8AssignTo(t *testing.T) {
    60  	var i8 int8
    61  	var i16 int16
    62  	var i32 int32
    63  	var i64 int64
    64  	var i int
    65  	var ui8 uint8
    66  	var ui16 uint16
    67  	var ui32 uint32
    68  	var ui64 uint64
    69  	var ui uint
    70  	var pi8 *int8
    71  	var _i8 _int8
    72  	var _pi8 *_int8
    73  
    74  	simpleTests := []struct {
    75  		src      pgtype.Int8
    76  		dst      interface{}
    77  		expected interface{}
    78  	}{
    79  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)},
    80  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)},
    81  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)},
    82  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)},
    83  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)},
    84  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)},
    85  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)},
    86  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
    87  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)},
    88  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)},
    89  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)},
    90  		{src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))},
    91  		{src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &_pi8, expected: ((*_int8)(nil))},
    92  	}
    93  
    94  	for i, tt := range simpleTests {
    95  		err := tt.src.AssignTo(tt.dst)
    96  		if err != nil {
    97  			t.Errorf("%d: %v", i, err)
    98  		}
    99  
   100  		if dst := reflect.ValueOf(tt.dst).Elem().Interface(); dst != tt.expected {
   101  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
   102  		}
   103  	}
   104  
   105  	pointerAllocTests := []struct {
   106  		src      pgtype.Int8
   107  		dst      interface{}
   108  		expected interface{}
   109  	}{
   110  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)},
   111  		{src: pgtype.Int8{Int: 42, Status: pgtype.Present}, dst: &_pi8, expected: _int8(42)},
   112  	}
   113  
   114  	for i, tt := range pointerAllocTests {
   115  		err := tt.src.AssignTo(tt.dst)
   116  		if err != nil {
   117  			t.Errorf("%d: %v", i, err)
   118  		}
   119  
   120  		if dst := reflect.ValueOf(tt.dst).Elem().Elem().Interface(); dst != tt.expected {
   121  			t.Errorf("%d: expected %v to assign %v, but result was %v", i, tt.src, tt.expected, dst)
   122  		}
   123  	}
   124  
   125  	errorTests := []struct {
   126  		src pgtype.Int8
   127  		dst interface{}
   128  	}{
   129  		{src: pgtype.Int8{Int: 150, Status: pgtype.Present}, dst: &i8},
   130  		{src: pgtype.Int8{Int: 40000, Status: pgtype.Present}, dst: &i16},
   131  		{src: pgtype.Int8{Int: 5000000000, Status: pgtype.Present}, dst: &i32},
   132  		{src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui8},
   133  		{src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui16},
   134  		{src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui32},
   135  		{src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui64},
   136  		{src: pgtype.Int8{Int: -1, Status: pgtype.Present}, dst: &ui},
   137  		{src: pgtype.Int8{Int: 0, Status: pgtype.Null}, dst: &i64},
   138  	}
   139  
   140  	for i, tt := range errorTests {
   141  		err := tt.src.AssignTo(tt.dst)
   142  		if err == nil {
   143  			t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
   144  		}
   145  	}
   146  }
   147  
   148  func TestInt8MarshalJSON(t *testing.T) {
   149  	successfulTests := []struct {
   150  		source pgtype.Int8
   151  		result string
   152  	}{
   153  		{source: pgtype.Int8{Int: 0, Status: pgtype.Null}, result: "null"},
   154  		{source: pgtype.Int8{Int: 1, Status: pgtype.Present}, result: "1"},
   155  	}
   156  	for i, tt := range successfulTests {
   157  		r, err := tt.source.MarshalJSON()
   158  		if err != nil {
   159  			t.Errorf("%d: %v", i, err)
   160  		}
   161  
   162  		if string(r) != tt.result {
   163  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
   164  		}
   165  	}
   166  }
   167  
   168  func TestInt8UnmarshalJSON(t *testing.T) {
   169  	successfulTests := []struct {
   170  		source string
   171  		result pgtype.Int8
   172  	}{
   173  		{source: "null", result: pgtype.Int8{Int: 0, Status: pgtype.Null}},
   174  		{source: "1", result: pgtype.Int8{Int: 1, Status: pgtype.Present}},
   175  	}
   176  	for i, tt := range successfulTests {
   177  		var r pgtype.Int8
   178  		err := r.UnmarshalJSON([]byte(tt.source))
   179  		if err != nil {
   180  			t.Errorf("%d: %v", i, err)
   181  		}
   182  
   183  		if r != tt.result {
   184  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
   185  		}
   186  	}
   187  }
   188  

View as plain text