...

Source file src/github.com/jackc/pgx/v5/pgtype/timestamptz_test.go

Documentation: github.com/jackc/pgx/v5/pgtype

     1  package pgtype_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	pgx "github.com/jackc/pgx/v5"
     9  	"github.com/jackc/pgx/v5/pgtype"
    10  	"github.com/jackc/pgx/v5/pgxtest"
    11  	"github.com/stretchr/testify/require"
    12  )
    13  
    14  func TestTimestamptzCodec(t *testing.T) {
    15  	skipCockroachDB(t, "Server does not support infinite timestamps (see https://github.com/cockroachdb/cockroach/issues/41564)")
    16  
    17  	pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "timestamptz", []pgxtest.ValueRoundTripTest{
    18  		{time.Date(-100, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(-100, 1, 1, 0, 0, 0, 0, time.Local))},
    19  		{time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(-1, 1, 1, 0, 0, 0, 0, time.Local))},
    20  		{time.Date(0, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(0, 1, 1, 0, 0, 0, 0, time.Local))},
    21  		{time.Date(1, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(1, 1, 1, 0, 0, 0, 0, time.Local))},
    22  
    23  		{time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local))},
    24  		{time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(1970, 1, 1, 0, 0, 0, 0, time.Local))},
    25  		{time.Date(1999, 12, 31, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(1999, 12, 31, 0, 0, 0, 0, time.Local))},
    26  		{time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(2000, 1, 1, 0, 0, 0, 0, time.Local))},
    27  		{time.Date(2000, 1, 2, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(2000, 1, 2, 0, 0, 0, 0, time.Local))},
    28  		{time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local), new(time.Time), isExpectedEqTime(time.Date(2200, 1, 1, 0, 0, 0, 0, time.Local))},
    29  
    30  		// Nanosecond truncation
    31  		{time.Date(2020, 1, 1, 0, 0, 0, 999999999, time.Local), new(time.Time), isExpectedEqTime(time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local))},
    32  		{time.Date(2020, 1, 1, 0, 0, 0, 999999001, time.Local), new(time.Time), isExpectedEqTime(time.Date(2020, 1, 1, 0, 0, 0, 999999000, time.Local))},
    33  
    34  		{pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Valid: true}, new(pgtype.Timestamptz), isExpectedEq(pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Valid: true})},
    35  		{pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, new(pgtype.Timestamptz), isExpectedEq(pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Valid: true})},
    36  		{pgtype.Timestamptz{}, new(pgtype.Timestamptz), isExpectedEq(pgtype.Timestamptz{})},
    37  		{nil, new(*time.Time), isExpectedEq((*time.Time)(nil))},
    38  	})
    39  }
    40  
    41  // https://github.com/jackc/pgx/v4/pgtype/pull/128
    42  func TestTimestamptzTranscodeBigTimeBinary(t *testing.T) {
    43  	defaultConnTestRunner.RunTest(context.Background(), t, func(ctx context.Context, t testing.TB, conn *pgx.Conn) {
    44  		in := &pgtype.Timestamptz{Time: time.Date(294276, 12, 31, 23, 59, 59, 999999000, time.UTC), Valid: true}
    45  		var out pgtype.Timestamptz
    46  
    47  		err := conn.QueryRow(ctx, "select $1::timestamptz", in).Scan(&out)
    48  		if err != nil {
    49  			t.Fatal(err)
    50  		}
    51  
    52  		require.Equal(t, in.Valid, out.Valid)
    53  		require.Truef(t, in.Time.Equal(out.Time), "expected %v got %v", in.Time, out.Time)
    54  	})
    55  }
    56  
    57  // https://github.com/jackc/pgtype/issues/74
    58  func TestTimestamptzDecodeTextInvalid(t *testing.T) {
    59  	c := &pgtype.TimestamptzCodec{}
    60  	var tstz pgtype.Timestamptz
    61  	plan := c.PlanScan(nil, pgtype.TimestamptzOID, pgtype.TextFormatCode, &tstz)
    62  	err := plan.Scan([]byte(`eeeee`), &tstz)
    63  	require.Error(t, err)
    64  }
    65  
    66  func TestTimestamptzMarshalJSON(t *testing.T) {
    67  	successfulTests := []struct {
    68  		source pgtype.Timestamptz
    69  		result string
    70  	}{
    71  		{source: pgtype.Timestamptz{}, result: "null"},
    72  		{source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Valid: true}, result: "\"2012-03-29T10:05:45-06:00\""},
    73  		{source: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Valid: true}, result: "\"2012-03-29T10:05:45.555-06:00\""},
    74  		{source: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Valid: true}, result: "\"infinity\""},
    75  		{source: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Valid: true}, result: "\"-infinity\""},
    76  	}
    77  	for i, tt := range successfulTests {
    78  		r, err := tt.source.MarshalJSON()
    79  		if err != nil {
    80  			t.Errorf("%d: %v", i, err)
    81  		}
    82  
    83  		if string(r) != tt.result {
    84  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, string(r))
    85  		}
    86  	}
    87  }
    88  
    89  func TestTimestamptzUnmarshalJSON(t *testing.T) {
    90  	successfulTests := []struct {
    91  		source string
    92  		result pgtype.Timestamptz
    93  	}{
    94  		{source: "null", result: pgtype.Timestamptz{}},
    95  		{source: "\"2012-03-29T10:05:45-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 0, time.FixedZone("", -6*60*60)), Valid: true}},
    96  		{source: "\"2012-03-29T10:05:45.555-06:00\"", result: pgtype.Timestamptz{Time: time.Date(2012, 3, 29, 10, 5, 45, 555*1000*1000, time.FixedZone("", -6*60*60)), Valid: true}},
    97  		{source: "\"infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.Infinity, Valid: true}},
    98  		{source: "\"-infinity\"", result: pgtype.Timestamptz{InfinityModifier: pgtype.NegativeInfinity, Valid: true}},
    99  	}
   100  	for i, tt := range successfulTests {
   101  		var r pgtype.Timestamptz
   102  		err := r.UnmarshalJSON([]byte(tt.source))
   103  		if err != nil {
   104  			t.Errorf("%d: %v", i, err)
   105  		}
   106  
   107  		if !r.Time.Equal(tt.result.Time) || r.Valid != tt.result.Valid || r.InfinityModifier != tt.result.InfinityModifier {
   108  			t.Errorf("%d: expected %v to convert to %v, but it was %v", i, tt.source, tt.result, r)
   109  		}
   110  	}
   111  }
   112  

View as plain text