Debian Codegraph @

pkg/validation/strfmt/date_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 "testing"
19 "time"
20
21 "github.com/stretchr/testify/assert"
22 )
23
24 func TestDate(t *testing.T) {
25 pp := Date{}
26 err := pp.UnmarshalText([]byte{})
27 assert.NoError(t, err)
28 err = pp.UnmarshalText([]byte("yada"))
29 assert.Error(t, err)
30
31 orig := "2014-12-15"
32 bj := []byte("\"" + orig + "\"")
33 err = pp.UnmarshalText([]byte(orig))
34 assert.NoError(t, err)
35
36 txt, err := pp.MarshalText()
37 assert.NoError(t, err)
38 assert.Equal(t, orig, string(txt))
39
40 err = pp.UnmarshalJSON(bj)
41 assert.NoError(t, err)
42 assert.EqualValues(t, orig, pp.String())
43
44 err = pp.UnmarshalJSON([]byte(`"1972/01/01"`))
45 assert.Error(t, err)
46
47 b, err := pp.MarshalJSON()
48 assert.NoError(t, err)
49 assert.Equal(t, bj, b)
50
51 var dateZero Date
52 err = dateZero.UnmarshalJSON([]byte(jsonNull))
53 assert.NoError(t, err)
54 assert.Equal(t, Date{}, dateZero)
55 }
56
57 func TestDate_IsDate(t *testing.T) {
58 tests := []struct {
59 value string
60 valid bool
61 }{
62 {"2017-12-22", true},
63 {"2017-1-1", false},
64 {"17-13-22", false},
65 {"2017-02-29", false}, // not a valid date : 2017 is not a leap year
66 {"1900-02-29", false}, // not a valid date : 1900 is not a leap year
67 {"2100-02-29", false}, // not a valid date : 2100 is not a leap year
68 {"2000-02-29", true}, // a valid date : 2000 is a leap year
69 {"2400-02-29", true}, // a valid date : 2000 is a leap year
70 {"2017-13-22", false},
71 {"2017-12-32", false},
72 {"20171-12-32", false},
73 {"YYYY-MM-DD", false},
74 {"20-17-2017", false},
75 {"2017-12-22T01:02:03Z", false},
76 }
77 for _, test := range tests {
78 assert.Equal(t, test.valid, IsDate(test.value), "value [%s] should be valid: [%t]", test.value, test.valid)
79 }
80 }
81
82 func TestDeepCopyDate(t *testing.T) {
83 ref := time.Now().Truncate(24 * time.Hour).UTC()
84 date := Date(ref)
85 in := &date
86
87 out := new(Date)
88 in.DeepCopyInto(out)
89 assert.Equal(t, in, out)
90
91 out2 := in.DeepCopy()
92 assert.Equal(t, in, out2)
93
94 var inNil *Date
95 out3 := inNil.DeepCopy()
96 assert.Nil(t, out3)
97 }