-
Notifications
You must be signed in to change notification settings - Fork 0
/
time_test.go
127 lines (107 loc) · 3.07 KB
/
time_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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package nulls
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"testing"
"time"
)
// testTime for usage in tests.
var testTime = time.UnixMilli(1532765)
// TestNewTime tests NewTime.
func TestNewTime(t *testing.T) {
tt := NewTime(testTime)
assert.True(t, tt.Valid, "should be valid")
assert.True(t, testTime.Equal(tt.Time), "should contain correct value")
}
// TimeMarshalJSONSuite tests Time.MarshalJSON.
type TimeMarshalJSONSuite struct {
suite.Suite
}
func (suite *TimeMarshalJSONSuite) TestNotValid() {
tt := Time{Time: testTime}
raw, err := json.Marshal(tt)
suite.Require().NoError(err, "should not fail")
suite.Equal(jsonNull, raw, "shoudl return correct value")
}
func (suite *TimeMarshalJSONSuite) TestOK() {
tt := NewTime(testTime)
raw, err := json.Marshal(tt)
suite.Require().NoError(err, "should not fail")
suite.Equal(marshalMust(testTime), raw, "should return correct value")
}
func TestTime_MarshalJSON(t *testing.T) {
suite.Run(t, new(TimeMarshalJSONSuite))
}
// TimeUnmarshalJSONSuite tests Time.UnmarshalJSON.
type TimeUnmarshalJSONSuite struct {
suite.Suite
}
func (suite *TimeUnmarshalJSONSuite) TestNull() {
var tt Time
err := tt.UnmarshalJSON(jsonNull)
suite.Require().NoError(err, "should not fail")
suite.False(tt.Valid, "should not be valid")
}
func (suite *TimeUnmarshalJSONSuite) TestOK() {
var tt Time
err := tt.UnmarshalJSON(marshalMust(testTime))
suite.Require().NoError(err, "should not fail")
suite.True(tt.Valid, "should be valid")
suite.True(testTime.Equal(tt.Time), "should unmarshal correct value")
}
func TestTime_UnmarshalJSON(t *testing.T) {
suite.Run(t, new(TimeUnmarshalJSONSuite))
}
// TimeScanSuite tests Time.Scan.
type TimeScanSuite struct {
suite.Suite
}
func (suite *TimeScanSuite) TestNull() {
var tt Time
err := tt.Scan(nil)
suite.Require().NoError(err, "should not fail")
suite.False(tt.Valid, "should not be valid")
}
func (suite *TimeScanSuite) TestOK() {
var tt Time
err := tt.Scan(testTime)
suite.Require().NoError(err, "should not fail")
suite.True(tt.Valid, "should be valid")
suite.True(testTime.Equal(tt.Time), "should scan correct value")
}
func TestTime_Scan(t *testing.T) {
suite.Run(t, new(TimeScanSuite))
}
// TimeValueSuite tests Time.Value.
type TimeValueSuite struct {
suite.Suite
}
func (suite *TimeValueSuite) TestNull() {
tt := Time{Time: testTime}
raw, err := tt.Value()
suite.Require().NoError(err, "should not fail")
suite.Nil(raw, "should return correct value")
}
func (suite *TimeValueSuite) TestOK() {
tt := NewTime(testTime)
raw, err := tt.Value()
suite.Require().NoError(err, "should not fail")
rawTime, ok := raw.(time.Time)
suite.Require().True(ok, "returned valued should be time")
suite.True(testTime.Equal(rawTime), "should return correct value")
}
func TestTime_Value(t *testing.T) {
suite.Run(t, new(TimeValueSuite))
}
func TestTime_UTC(t *testing.T) {
tt := Time{
Time: time.UnixMilli(1958323),
Valid: true,
}
utc := tt.UTC()
assert.Equal(t, Time{
Time: tt.Time.UTC(),
Valid: tt.Valid,
}, utc, "should return correct value")
}