Debian Codegraph @

pkg/validation/strfmt/time_test.go

raw
1 // Copyright 2015 go-swagger maintainers
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package strfmt
16
17 import (
18 "bytes"
19 "testing"
20 "time"
21
22 "github.com/stretchr/testify/assert"
23 )
24
25 var (
26 p, _ = time.Parse(time.RFC3339Nano, "2011-08-18T19:03:37.000000000+01:00")
27
28 testCases = []struct {
29 in []byte // externally sourced data -- to be unmarshalled
30 time time.Time // its representation in time.Time
31 str string // its marshalled representation
32 }{
33 {[]byte("2014-12-15T08:00:00"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
34 {[]byte("2014-12-15T08:00:00.000Z"), time.Date(2014, 12, 15, 8, 0, 0, 0, time.UTC), "2014-12-15T08:00:00.000Z"},
35 {[]byte("2011-08-18T19:03:37.000000000+01:00"), time.Date(2011, 8, 18, 19, 3, 37, 0, p.Location()), "2011-08-18T19:03:37.000+01:00"},
36 {[]byte("2014-12-15T19:30:20Z"), time.Date(2014, 12, 15, 19, 30, 20, 0, time.UTC), "2014-12-15T19:30:20.000Z"},
37 {[]byte("0001-01-01T00:00:00Z"), time.Time{}.UTC(), "0001-01-01T00:00:00.000Z"},
38 {[]byte(""), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z"},
39 {[]byte(nil), time.Unix(0, 0).UTC(), "1970-01-01T00:00:00.000Z"},
40 }
41 )
42
43 func TestNewDateTime(t *testing.T) {
44 assert.EqualValues(t, time.Unix(0, 0).UTC(), NewDateTime())
45 }
46
47 func TestParseDateTime_errorCases(t *testing.T) {
48 _, err := ParseDateTime("yada")
49 assert.Error(t, err)
50 }
51
52 // TestParseDateTime tests the full cycle:
53 // parsing -> marshalling -> unmarshalling / scanning
54 func TestParseDateTime_fullCycle(t *testing.T) {
55 for caseNum, example := range testCases {
56 t.Logf("Case #%d", caseNum)
57
58 parsed, err := ParseDateTime(example.str)
59 assert.NoError(t, err)
60 assert.EqualValues(t, example.time, parsed)
61
62 mt, err := parsed.MarshalText()
63 assert.NoError(t, err)
64 assert.Equal(t, []byte(example.str), mt)
65
66 if example.str != "" {
67 v := IsDateTime(example.str)
68 assert.True(t, v)
69 } else {
70 t.Logf("IsDateTime() skipped for empty testcases")
71 }
72
73 pp := NewDateTime()
74 err = pp.UnmarshalText(mt)
75 assert.NoError(t, err)
76 assert.EqualValues(t, example.time, pp)
77
78 }
79 }
80
81 func TestDateTime_IsDateTime_errorCases(t *testing.T) {
82 v := IsDateTime("zor")
83 assert.False(t, v)
84
85 v = IsDateTime("zorg")
86 assert.False(t, v)
87
88 v = IsDateTime("zorgTx")
89 assert.False(t, v)
90
91 v = IsDateTime("1972-12-31Tx")
92 assert.False(t, v)
93
94 v = IsDateTime("1972-12-31T24:40:00.000Z")
95 assert.False(t, v)
96
97 v = IsDateTime("1972-12-31T23:63:00.000Z")
98 assert.False(t, v)
99
100 v = IsDateTime("1972-12-31T23:59:60.000Z")
101 assert.False(t, v)
102
103 }
104 func TestDateTime_UnmarshalText_errorCases(t *testing.T) {
105 pp := NewDateTime()
106 err := pp.UnmarshalText([]byte("yada"))
107 assert.Error(t, err)
108 err = pp.UnmarshalJSON([]byte("yada"))
109 assert.Error(t, err)
110 }
111
112 func TestDateTime_UnmarshalText(t *testing.T) {
113 for caseNum, example := range testCases {
114 t.Logf("Case #%d", caseNum)
115 pp := NewDateTime()
116 err := pp.UnmarshalText(example.in)
117 assert.NoError(t, err)
118 assert.EqualValues(t, example.time, pp)
119 }
120 }
121 func TestDateTime_UnmarshalJSON(t *testing.T) {
122 for caseNum, example := range testCases {
123 t.Logf("Case #%d", caseNum)
124 pp := NewDateTime()
125 err := pp.UnmarshalJSON(esc(example.in))
126 assert.NoError(t, err)
127 assert.EqualValues(t, example.time, pp)
128 }
129
130 // Check UnmarshalJSON failure with no lexed items
131 pp := NewDateTime()
132 err := pp.UnmarshalJSON([]byte("zorg emperor"))
133 assert.Error(t, err)
134
135 // Check lexer failure
136 err = pp.UnmarshalJSON([]byte(`"zorg emperor"`))
137 assert.Error(t, err)
138
139 // Check null case
140 err = pp.UnmarshalJSON([]byte("null"))
141 assert.Nil(t, err)
142 }
143
144 func esc(v []byte) []byte {
145 var buf bytes.Buffer
146 buf.WriteByte('"')
147 buf.Write(v)
148 buf.WriteByte('"')
149 return buf.Bytes()
150 }
151
152 func TestDateTime_MarshalText(t *testing.T) {
153 for caseNum, example := range testCases {
154 t.Logf("Case #%d", caseNum)
155 dt := DateTime(example.time)
156 mt, err := dt.MarshalText()
157 assert.NoError(t, err)
158 assert.Equal(t, []byte(example.str), mt)
159 }
160 }
161 func TestDateTime_MarshalJSON(t *testing.T) {
162 for caseNum, example := range testCases {
163 t.Logf("Case #%d", caseNum)
164 dt := DateTime(example.time)
165 bb, err := dt.MarshalJSON()
166 assert.NoError(t, err)
167 assert.EqualValues(t, esc([]byte(example.str)), bb)
168 }
169 }
170
171 func TestDeepCopyDateTime(t *testing.T) {
172 p, err := ParseDateTime("2011-08-18T19:03:37.000000000+01:00")
173 assert.NoError(t, err)
174 in := &p
175
176 out := new(DateTime)
177 in.DeepCopyInto(out)
178 assert.Equal(t, in, out)
179
180 out2 := in.DeepCopy()
181 assert.Equal(t, in, out2)
182
183 var inNil *DateTime
184 out3 := inNil.DeepCopy()
185 assert.Nil(t, out3)
186 }