| 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 | pp = NewDateTime() |
| 79 | err = pp.Scan(mt) |
| 80 | assert.NoError(t, err) |
| 81 | assert.Equal(t, DateTime(example.time), pp) |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | func TestDateTime_IsDateTime_errorCases(t *testing.T) { |
| 86 | v := IsDateTime("zor") |
| 87 | assert.False(t, v) |
| 88 | |
| 89 | v = IsDateTime("zorg") |
| 90 | assert.False(t, v) |
| 91 | |
| 92 | v = IsDateTime("zorgTx") |
| 93 | assert.False(t, v) |
| 94 | |
| 95 | v = IsDateTime("1972-12-31Tx") |
| 96 | assert.False(t, v) |
| 97 | |
| 98 | v = IsDateTime("1972-12-31T24:40:00.000Z") |
| 99 | assert.False(t, v) |
| 100 | |
| 101 | v = IsDateTime("1972-12-31T23:63:00.000Z") |
| 102 | assert.False(t, v) |
| 103 | |
| 104 | v = IsDateTime("1972-12-31T23:59:60.000Z") |
| 105 | assert.False(t, v) |
| 106 | |
| 107 | } |
| 108 | func TestDateTime_UnmarshalText_errorCases(t *testing.T) { |
| 109 | pp := NewDateTime() |
| 110 | err := pp.UnmarshalText([]byte("yada")) |
| 111 | assert.Error(t, err) |
| 112 | err = pp.UnmarshalJSON([]byte("yada")) |
| 113 | assert.Error(t, err) |
| 114 | } |
| 115 | |
| 116 | func TestDateTime_UnmarshalText(t *testing.T) { |
| 117 | for caseNum, example := range testCases { |
| 118 | t.Logf("Case #%d", caseNum) |
| 119 | pp := NewDateTime() |
| 120 | err := pp.UnmarshalText(example.in) |
| 121 | assert.NoError(t, err) |
| 122 | assert.EqualValues(t, example.time, pp) |
| 123 | } |
| 124 | } |
| 125 | func TestDateTime_UnmarshalJSON(t *testing.T) { |
| 126 | for caseNum, example := range testCases { |
| 127 | t.Logf("Case #%d", caseNum) |
| 128 | pp := NewDateTime() |
| 129 | err := pp.UnmarshalJSON(esc(example.in)) |
| 130 | assert.NoError(t, err) |
| 131 | assert.EqualValues(t, example.time, pp) |
| 132 | } |
| 133 | |
| 134 | // Check UnmarshalJSON failure with no lexed items |
| 135 | pp := NewDateTime() |
| 136 | err := pp.UnmarshalJSON([]byte("zorg emperor")) |
| 137 | assert.Error(t, err) |
| 138 | |
| 139 | // Check lexer failure |
| 140 | err = pp.UnmarshalJSON([]byte(`"zorg emperor"`)) |
| 141 | assert.Error(t, err) |
| 142 | |
| 143 | // Check null case |
| 144 | err = pp.UnmarshalJSON([]byte("null")) |
| 145 | assert.Nil(t, err) |
| 146 | } |
| 147 | |
| 148 | func esc(v []byte) []byte { |
| 149 | var buf bytes.Buffer |
| 150 | buf.WriteByte('"') |
| 151 | buf.Write(v) |
| 152 | buf.WriteByte('"') |
| 153 | return buf.Bytes() |
| 154 | } |
| 155 | |
| 156 | func TestDateTime_MarshalText(t *testing.T) { |
| 157 | for caseNum, example := range testCases { |
| 158 | t.Logf("Case #%d", caseNum) |
| 159 | dt := DateTime(example.time) |
| 160 | mt, err := dt.MarshalText() |
| 161 | assert.NoError(t, err) |
| 162 | assert.Equal(t, []byte(example.str), mt) |
| 163 | } |
| 164 | } |
| 165 | func TestDateTime_MarshalJSON(t *testing.T) { |
| 166 | for caseNum, example := range testCases { |
| 167 | t.Logf("Case #%d", caseNum) |
| 168 | dt := DateTime(example.time) |
| 169 | bb, err := dt.MarshalJSON() |
| 170 | assert.NoError(t, err) |
| 171 | assert.EqualValues(t, esc([]byte(example.str)), bb) |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | func TestDateTime_Scan(t *testing.T) { |
| 176 | for caseNum, example := range testCases { |
| 177 | t.Logf("Case #%d", caseNum) |
| 178 | |
| 179 | pp := NewDateTime() |
| 180 | err := pp.Scan(example.in) |
| 181 | assert.NoError(t, err) |
| 182 | assert.Equal(t, DateTime(example.time), pp) |
| 183 | |
| 184 | pp = NewDateTime() |
| 185 | err = pp.Scan(string(example.in)) |
| 186 | assert.NoError(t, err) |
| 187 | assert.Equal(t, DateTime(example.time), pp) |
| 188 | |
| 189 | pp = NewDateTime() |
| 190 | err = pp.Scan(example.time) |
| 191 | assert.NoError(t, err) |
| 192 | assert.Equal(t, DateTime(example.time), pp) |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | func TestDateTime_Scan_Failed(t *testing.T) { |
| 197 | pp := NewDateTime() |
| 198 | zero := NewDateTime() |
| 199 | |
| 200 | err := pp.Scan(nil) |
| 201 | assert.NoError(t, err) |
| 202 | // Zero values differ... |
| 203 | //assert.Equal(t, zero, pp) |
| 204 | assert.Equal(t, DateTime{}, pp) |
| 205 | |
| 206 | err = pp.Scan("") |
| 207 | assert.NoError(t, err) |
| 208 | assert.Equal(t, zero, pp) |
| 209 | |
| 210 | err = pp.Scan(int64(0)) |
| 211 | assert.Error(t, err) |
| 212 | |
| 213 | err = pp.Scan(float64(0)) |
| 214 | assert.Error(t, err) |
| 215 | } |
| 216 | |
| 217 | func TestDeepCopyDateTime(t *testing.T) { |
| 218 | p, err := ParseDateTime("2011-08-18T19:03:37.000000000+01:00") |
| 219 | assert.NoError(t, err) |
| 220 | in := &p |
| 221 | |
| 222 | out := new(DateTime) |
| 223 | in.DeepCopyInto(out) |
| 224 | assert.Equal(t, in, out) |
| 225 | |
| 226 | out2 := in.DeepCopy() |
| 227 | assert.Equal(t, in, out2) |
| 228 | |
| 229 | var inNil *DateTime |
| 230 | out3 := inNil.DeepCopy() |
| 231 | assert.Nil(t, out3) |
| 232 | } |