1 package pgtype_test
2
3 import (
4 "testing"
5 "time"
6
7 "github.com/jackc/pgtype"
8 "github.com/jackc/pgtype/testutil"
9 "github.com/stretchr/testify/assert"
10 "github.com/stretchr/testify/require"
11 )
12
13 func TestIntervalTranscode(t *testing.T) {
14 testutil.TestSuccessfulTranscode(t, "interval", []interface{}{
15 &pgtype.Interval{Microseconds: 1, Status: pgtype.Present},
16 &pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present},
17 &pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present},
18 &pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present},
19 &pgtype.Interval{Days: 1, Status: pgtype.Present},
20 &pgtype.Interval{Months: 1, Status: pgtype.Present},
21 &pgtype.Interval{Months: 12, Status: pgtype.Present},
22 &pgtype.Interval{Months: 13, Days: 15, Microseconds: 1000001, Status: pgtype.Present},
23 &pgtype.Interval{Microseconds: -1, Status: pgtype.Present},
24 &pgtype.Interval{Microseconds: -1000000, Status: pgtype.Present},
25 &pgtype.Interval{Microseconds: -1000001, Status: pgtype.Present},
26 &pgtype.Interval{Microseconds: -123202800000000, Status: pgtype.Present},
27 &pgtype.Interval{Days: -1, Status: pgtype.Present},
28 &pgtype.Interval{Months: -1, Status: pgtype.Present},
29 &pgtype.Interval{Months: -12, Status: pgtype.Present},
30 &pgtype.Interval{Months: -13, Days: -15, Microseconds: -1000001, Status: pgtype.Present},
31 &pgtype.Interval{Status: pgtype.Null},
32 })
33 }
34
35 func TestIntervalNormalize(t *testing.T) {
36 testutil.TestSuccessfulNormalize(t, []testutil.NormalizeTest{
37 {
38 SQL: "select '1 second'::interval",
39 Value: &pgtype.Interval{Microseconds: 1000000, Status: pgtype.Present},
40 },
41 {
42 SQL: "select '1.000001 second'::interval",
43 Value: &pgtype.Interval{Microseconds: 1000001, Status: pgtype.Present},
44 },
45 {
46 SQL: "select '34223 hours'::interval",
47 Value: &pgtype.Interval{Microseconds: 123202800000000, Status: pgtype.Present},
48 },
49 {
50 SQL: "select '1 day'::interval",
51 Value: &pgtype.Interval{Days: 1, Status: pgtype.Present},
52 },
53 {
54 SQL: "select '1 month'::interval",
55 Value: &pgtype.Interval{Months: 1, Status: pgtype.Present},
56 },
57 {
58 SQL: "select '1 year'::interval",
59 Value: &pgtype.Interval{Months: 12, Status: pgtype.Present},
60 },
61 {
62 SQL: "select '-13 mon'::interval",
63 Value: &pgtype.Interval{Months: -13, Status: pgtype.Present},
64 },
65 })
66 }
67
68 func TestIntervalLossyConversionToDuration(t *testing.T) {
69 interval := &pgtype.Interval{Months: 1, Days: 1, Status: pgtype.Present}
70 var d time.Duration
71 err := interval.AssignTo(&d)
72 require.NoError(t, err)
73 assert.EqualValues(t, int64(2678400000000000), d.Nanoseconds())
74 }
75
View as plain text