github.com/jackc/pgx/v5@v5.5.5/pgtype/interval_test.go (about) 1 package pgtype_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/jackc/pgx/v5/pgtype" 9 "github.com/jackc/pgx/v5/pgxtest" 10 ) 11 12 func TestIntervalCodec(t *testing.T) { 13 pgxtest.RunValueRoundTripTests(context.Background(), t, defaultConnTestRunner, nil, "interval", []pgxtest.ValueRoundTripTest{ 14 { 15 pgtype.Interval{Microseconds: 1, Valid: true}, 16 new(pgtype.Interval), 17 isExpectedEq(pgtype.Interval{Microseconds: 1, Valid: true}), 18 }, 19 { 20 pgtype.Interval{Microseconds: 1000000, Valid: true}, 21 new(pgtype.Interval), 22 isExpectedEq(pgtype.Interval{Microseconds: 1000000, Valid: true}), 23 }, 24 { 25 pgtype.Interval{Microseconds: 1000001, Valid: true}, 26 new(pgtype.Interval), 27 isExpectedEq(pgtype.Interval{Microseconds: 1000001, Valid: true}), 28 }, 29 { 30 pgtype.Interval{Microseconds: 123202800000000, Valid: true}, 31 new(pgtype.Interval), 32 isExpectedEq(pgtype.Interval{Microseconds: 123202800000000, Valid: true}), 33 }, 34 { 35 pgtype.Interval{Days: 1, Valid: true}, 36 new(pgtype.Interval), 37 isExpectedEq(pgtype.Interval{Days: 1, Valid: true}), 38 }, 39 { 40 pgtype.Interval{Months: 1, Valid: true}, 41 new(pgtype.Interval), 42 isExpectedEq(pgtype.Interval{Months: 1, Valid: true}), 43 }, 44 { 45 pgtype.Interval{Months: 12, Valid: true}, 46 new(pgtype.Interval), 47 isExpectedEq(pgtype.Interval{Months: 12, Valid: true}), 48 }, 49 { 50 pgtype.Interval{Months: 13, Days: 15, Microseconds: 1000001, Valid: true}, 51 new(pgtype.Interval), 52 isExpectedEq(pgtype.Interval{Months: 13, Days: 15, Microseconds: 1000001, Valid: true}), 53 }, 54 { 55 pgtype.Interval{Microseconds: -1, Valid: true}, 56 new(pgtype.Interval), 57 isExpectedEq(pgtype.Interval{Microseconds: -1, Valid: true}), 58 }, 59 { 60 pgtype.Interval{Microseconds: -1000000, Valid: true}, 61 new(pgtype.Interval), 62 isExpectedEq(pgtype.Interval{Microseconds: -1000000, Valid: true}), 63 }, 64 { 65 pgtype.Interval{Microseconds: -1000001, Valid: true}, 66 new(pgtype.Interval), 67 isExpectedEq(pgtype.Interval{Microseconds: -1000001, Valid: true}), 68 }, 69 { 70 pgtype.Interval{Microseconds: -123202800000000, Valid: true}, 71 new(pgtype.Interval), 72 isExpectedEq(pgtype.Interval{Microseconds: -123202800000000, Valid: true}), 73 }, 74 { 75 pgtype.Interval{Days: -1, Valid: true}, 76 new(pgtype.Interval), 77 isExpectedEq(pgtype.Interval{Days: -1, Valid: true}), 78 }, 79 { 80 pgtype.Interval{Months: -1, Valid: true}, 81 new(pgtype.Interval), 82 isExpectedEq(pgtype.Interval{Months: -1, Valid: true}), 83 }, 84 { 85 pgtype.Interval{Months: -12, Valid: true}, 86 new(pgtype.Interval), 87 isExpectedEq(pgtype.Interval{Months: -12, Valid: true}), 88 }, 89 { 90 pgtype.Interval{Months: -13, Days: -15, Microseconds: -1000001, Valid: true}, 91 new(pgtype.Interval), 92 isExpectedEq(pgtype.Interval{Months: -13, Days: -15, Microseconds: -1000001, Valid: true}), 93 }, 94 { 95 "1 second", 96 new(pgtype.Interval), 97 isExpectedEq(pgtype.Interval{Microseconds: 1000000, Valid: true}), 98 }, 99 { 100 "1.000001 second", 101 new(pgtype.Interval), 102 isExpectedEq(pgtype.Interval{Microseconds: 1000001, Valid: true}), 103 }, 104 { 105 "34223 hours", 106 new(pgtype.Interval), 107 isExpectedEq(pgtype.Interval{Microseconds: 123202800000000, Valid: true}), 108 }, 109 { 110 "1 day", 111 new(pgtype.Interval), 112 isExpectedEq(pgtype.Interval{Days: 1, Valid: true}), 113 }, 114 { 115 "1 month", 116 new(pgtype.Interval), 117 isExpectedEq(pgtype.Interval{Months: 1, Valid: true}), 118 }, 119 { 120 "1 year", 121 new(pgtype.Interval), 122 isExpectedEq(pgtype.Interval{Months: 12, Valid: true}), 123 }, 124 { 125 "-13 mon", 126 new(pgtype.Interval), 127 isExpectedEq(pgtype.Interval{Months: -13, Valid: true}), 128 }, 129 {time.Hour, new(time.Duration), isExpectedEq(time.Hour)}, 130 { 131 pgtype.Interval{Months: 1, Days: 1, Valid: true}, 132 new(time.Duration), 133 isExpectedEq(time.Duration(2678400000000000)), 134 }, 135 {pgtype.Interval{}, new(pgtype.Interval), isExpectedEq(pgtype.Interval{})}, 136 {nil, new(pgtype.Interval), isExpectedEq(pgtype.Interval{})}, 137 }) 138 }