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