...

Source file src/github.com/jackc/pgtype/int4_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 TestInt4Transcode(t *testing.T) {
    13  	testutil.TestSuccessfulTranscode(t, "int4", []interface{}{
    14  		&pgtype.Int4{Int: math.MinInt32, Status: pgtype.Present},
    15  		&pgtype.Int4{Int: -1, Status: pgtype.Present},
    16  		&pgtype.Int4{Int: 0, Status: pgtype.Present},
    17  		&pgtype.Int4{Int: 1, Status: pgtype.Present},
    18  		&pgtype.Int4{Int: math.MaxInt32, Status: pgtype.Present},
    19  		&pgtype.Int4{Int: 0, Status: pgtype.Null},
    20  	})
    21  }
    22  
    23  func TestInt4Set(t *testing.T) {
    24  	successfulTests := []struct {
    25  		source interface{}
    26  		result pgtype.Int4
    27  	}{
    28  		{source: int8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    29  		{source: int16(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    30  		{source: int32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    31  		{source: int64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    32  		{source: int8(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}},
    33  		{source: int16(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}},
    34  		{source: int32(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}},
    35  		{source: int64(-1), result: pgtype.Int4{Int: -1, Status: pgtype.Present}},
    36  		{source: uint8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    37  		{source: uint16(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    38  		{source: uint32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    39  		{source: uint64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    40  		{source: float32(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    41  		{source: float64(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    42  		{source: "1", result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    43  		{source: _int8(1), result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
    44  	}
    45  
    46  	for i, tt := range successfulTests {
    47  		var r pgtype.Int4
    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 TestInt4AssignTo(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.Int4
    76  		dst      interface{}
    77  		expected interface{}
    78  	}{
    79  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i8, expected: int8(42)},
    80  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i16, expected: int16(42)},
    81  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i32, expected: int32(42)},
    82  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i64, expected: int64(42)},
    83  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &i, expected: int(42)},
    84  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui8, expected: uint8(42)},
    85  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui16, expected: uint16(42)},
    86  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui32, expected: uint32(42)},
    87  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui64, expected: uint64(42)},
    88  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &ui, expected: uint(42)},
    89  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &_i8, expected: _int8(42)},
    90  		{src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &pi8, expected: ((*int8)(nil))},
    91  		{src: pgtype.Int4{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.Int4
   107  		dst      interface{}
   108  		expected interface{}
   109  	}{
   110  		{src: pgtype.Int4{Int: 42, Status: pgtype.Present}, dst: &pi8, expected: int8(42)},
   111  		{src: pgtype.Int4{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.Int4
   127  		dst interface{}
   128  	}{
   129  		{src: pgtype.Int4{Int: 150, Status: pgtype.Present}, dst: &i8},
   130  		{src: pgtype.Int4{Int: 40000, Status: pgtype.Present}, dst: &i16},
   131  		{src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui8},
   132  		{src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui16},
   133  		{src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui32},
   134  		{src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui64},
   135  		{src: pgtype.Int4{Int: -1, Status: pgtype.Present}, dst: &ui},
   136  		{src: pgtype.Int4{Int: 0, Status: pgtype.Null}, dst: &i32},
   137  	}
   138  
   139  	for i, tt := range errorTests {
   140  		err := tt.src.AssignTo(tt.dst)
   141  		if err == nil {
   142  			t.Errorf("%d: expected error but none was returned (%v -> %v)", i, tt.src, tt.dst)
   143  		}
   144  	}
   145  }
   146  
   147  func TestInt4MarshalJSON(t *testing.T) {
   148  	successfulTests := []struct {
   149  		source pgtype.Int4
   150  		result string
   151  	}{
   152  		{source: pgtype.Int4{Int: 0, Status: pgtype.Null}, result: "null"},
   153  		{source: pgtype.Int4{Int: 1, Status: pgtype.Present}, result: "1"},
   154  	}
   155  	for i, tt := range successfulTests {
   156  		r, err := tt.source.MarshalJSON()
   157  		if err != nil {
   158  			t.Errorf("%d: %v", i, err)
   159  		}
   160  
   161  		if string(r) != tt.result {
   162  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
   163  		}
   164  	}
   165  }
   166  
   167  func TestInt4UnmarshalJSON(t *testing.T) {
   168  	successfulTests := []struct {
   169  		source string
   170  		result pgtype.Int4
   171  	}{
   172  		{source: "null", result: pgtype.Int4{Int: 0, Status: pgtype.Null}},
   173  		{source: "1", result: pgtype.Int4{Int: 1, Status: pgtype.Present}},
   174  	}
   175  	for i, tt := range successfulTests {
   176  		var r pgtype.Int4
   177  		err := r.UnmarshalJSON([]byte(tt.source))
   178  		if err != nil {
   179  			t.Errorf("%d: %v", i, err)
   180  		}
   181  
   182  		if r != tt.result {
   183  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
   184  		}
   185  	}
   186  }
   187  

View as plain text