github.com/jackc/pgx/v5@v5.5.5/pgtype/int_test.go.erb (about)

     1  package pgtype_test
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/jackc/pgx/v5/pgtype"
     8  )
     9  
    10  <% [2, 4, 8].each do |pg_byte_size| %>
    11  <% pg_bit_size = pg_byte_size * 8 %>
    12  func TestInt<%= pg_byte_size %>Codec(t *testing.T) {
    13  	pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "int<%= pg_byte_size %>", []pgxtest.ValueRoundTripTest{
    14  		{int8(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    15  		{int16(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    16  		{int32(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    17  		{int64(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    18  		{uint8(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    19  		{uint16(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    20  		{uint32(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    21  		{uint64(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    22  		{int(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    23  		{uint(1), new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    24  		{pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 1, Valid: true}, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    25  		{int32(-1), new(pgtype.Int<%= pg_byte_size %>), isExpectedEq(pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: -1, Valid: true})},
    26  		{1, new(int8), isExpectedEq(int8(1))},
    27  		{1, new(int16), isExpectedEq(int16(1))},
    28  		{1, new(int32), isExpectedEq(int32(1))},
    29  		{1, new(int64), isExpectedEq(int64(1))},
    30  		{1, new(uint8), isExpectedEq(uint8(1))},
    31  		{1, new(uint16), isExpectedEq(uint16(1))},
    32  		{1, new(uint32), isExpectedEq(uint32(1))},
    33  		{1, new(uint64), isExpectedEq(uint64(1))},
    34  		{1, new(int), isExpectedEq(int(1))},
    35  		{1, new(uint), isExpectedEq(uint(1))},
    36  		{-1, new(int8), isExpectedEq(int8(-1))},
    37  		{-1, new(int16), isExpectedEq(int16(-1))},
    38  		{-1, new(int32), isExpectedEq(int32(-1))},
    39  		{-1, new(int64), isExpectedEq(int64(-1))},
    40  		{-1, new(int), isExpectedEq(int(-1))},
    41  		{math.MinInt<%= pg_bit_size %>, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(math.MinInt<%= pg_bit_size %>))},
    42  		{-1, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(-1))},
    43  		{0, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(0))},
    44  		{1, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(1))},
    45  		{math.MaxInt<%= pg_bit_size %>, new(int<%= pg_bit_size %>), isExpectedEq(int<%= pg_bit_size %>(math.MaxInt<%= pg_bit_size %>))},
    46  		{1, new(pgtype.Int<%= pg_byte_size %>), isExpectedEq(pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 1, Valid: true})},
    47  		{"1", new(string), isExpectedEq("1")},
    48  		{pgtype.Int<%= pg_byte_size %>{}, new(pgtype.Int<%= pg_byte_size %>), isExpectedEq(pgtype.Int<%= pg_byte_size %>{})},
    49  		{nil, new(*int<%= pg_bit_size %>), isExpectedEq((*int<%= pg_bit_size %>)(nil))},
    50  	})
    51  }
    52  
    53  func TestInt<%= pg_byte_size %>MarshalJSON(t *testing.T) {
    54  	successfulTests := []struct {
    55  		source pgtype.Int<%= pg_byte_size %>
    56  		result string
    57  	}{
    58  		{source: pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 0}, result: "null"},
    59  		{source: pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 1, Valid: true}, result: "1"},
    60  	}
    61  	for i, tt := range successfulTests {
    62  		r, err := tt.source.MarshalJSON()
    63  		if err != nil {
    64  			t.Errorf("%d: %v", i, err)
    65  		}
    66  
    67  		if string(r) != tt.result {
    68  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
    69  		}
    70  	}
    71  }
    72  
    73  func TestInt<%= pg_byte_size %>UnmarshalJSON(t *testing.T) {
    74  	successfulTests := []struct {
    75  		source string
    76  		result pgtype.Int<%= pg_byte_size %>
    77  	}{
    78  		{source: "null", result: pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 0}},
    79  		{source: "1", result: pgtype.Int<%= pg_byte_size %>{Int<%= pg_bit_size %>: 1, Valid: true}},
    80  	}
    81  	for i, tt := range successfulTests {
    82  		var r pgtype.Int<%= pg_byte_size %>
    83  		err := r.UnmarshalJSON([]byte(tt.source))
    84  		if err != nil {
    85  			t.Errorf("%d: %v", i, err)
    86  		}
    87  
    88  		if r != tt.result {
    89  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
    90  		}
    91  	}
    92  }
    93  <% end %>