Debian Codegraph @

domain/time_test.go

raw
1 package domain
2
3 import (
4 "encoding/json"
5 "reflect"
6 "testing"
7 "time"
8 )
9
10 func TestNewShortDate(t *testing.T) {
11 dateTime := time.Date(2004, 02, 01, 5, 18, 47, 321, time.UTC)
12 expectedDate := time.Date(2004, 02, 01, 0, 0, 0, 0, time.UTC)
13
14 shortDate := NewShortDate(dateTime)
15
16 if !reflect.DeepEqual(expectedDate, shortDate.Time) {
17 t.Logf("Expected date to equal '%s', got '%s'\n", expectedDate, shortDate.Time)
18 t.Fail()
19 }
20
21 }
22
23 func TestDate(t *testing.T) {
24 date := time.Date(2004, 02, 01, 0, 0, 0, 0, time.UTC)
25
26 shortDate := Date(2004, 02, 01, time.UTC)
27
28 if !reflect.DeepEqual(date, shortDate.Time) {
29 t.Logf("Expected date to equal '%s', got '%s'\n", date, shortDate.Time)
30 t.Fail()
31 }
32 }
33
34 func TestShortDateUnmarshalJSON(t *testing.T) {
35 testJSON := `"2014-02-01"`
36
37 var date ShortDate
38
39 err := json.Unmarshal([]byte(testJSON), &date)
40
41 if err != nil {
42 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
43 t.Fail()
44 }
45
46 if &date == nil {
47 t.Logf("Expected date not to be nil\n")
48 t.Fail()
49 }
50
51 expectedDate, err := time.Parse("2006-01-02", "2014-02-01")
52 expectedShortDate := ShortDate{expectedDate}
53
54 if err != nil {
55 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
56 t.Fail()
57 }
58
59 if !reflect.DeepEqual(expectedShortDate, date) {
60 t.Logf("Expected date to be '%+#v', got '%+#v'\n", expectedShortDate, date)
61 t.Fail()
62 }
63 }
64
65 func TestShortDateMarshalJSON(t *testing.T) {
66 date := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}
67
68 bytes, err := json.Marshal(&date)
69
70 if err != nil {
71 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
72 t.Fail()
73 }
74
75 expectedJSON := `"2014-02-01"`
76
77 if !reflect.DeepEqual(string(bytes), expectedJSON) {
78 t.Logf("Expected date to be '%s', got '%s'\n", expectedJSON, string(bytes))
79 t.Fail()
80 }
81
82 // Date is Zero
83 date = ShortDate{}
84
85 bytes, err = json.Marshal(&date)
86
87 if err != nil {
88 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
89 t.Fail()
90 }
91
92 expectedJSON = `""`
93
94 if !reflect.DeepEqual(string(bytes), expectedJSON) {
95 t.Logf("Expected date to be '%s', got '%s'\n", expectedJSON, string(bytes))
96 t.Fail()
97 }
98 }
99
100 func TestFrom(t *testing.T) {
101 now := time.Now()
102 today := Date(now.Year(), now.Month(), now.Day(), now.Location())
103 locations := []*time.Location{
104 time.UTC,
105 mustLoad(time.LoadLocation("America/New_York")),
106 mustLoad(time.LoadLocation("Australia/Perth")),
107 }
108
109 var tests = []struct {
110 startDate ShortDate
111 expectedEndDate ShortDate
112 }{
113 {
114 startDate: Date(2010, 02, 01, locations[0]),
115 expectedEndDate: today,
116 },
117 {
118 startDate: Date(2010, 02, 01, locations[1]),
119 expectedEndDate: Date(now.Year(), now.Month(), now.Day(), locations[1]),
120 },
121 {
122 startDate: Date(2010, 02, 01, locations[2]),
123 expectedEndDate: Date(now.Year(), now.Month(), now.Day(), locations[2]),
124 },
125 }
126
127 for _, test := range tests {
128 actualEndDate := TimeframeFromDate(test.startDate).EndDate
129 if !reflect.DeepEqual(test.expectedEndDate, actualEndDate) {
130 t.Logf("Expected EndDate to equal '%s', got '%s'\n", test.expectedEndDate, actualEndDate)
131 t.Fail()
132 }
133 }
134 }
135
136 func mustLoad(loc *time.Location, err error) *time.Location {
137 if err != nil {
138 panic(err)
139 }
140 return loc
141 }
142
143 func TestTimeframeMarshalJSON(t *testing.T) {
144 startDate := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}
145 endDate := ShortDate{time.Date(2014, time.April, 01, 0, 0, 0, 0, time.UTC)}
146
147 var tests = []struct {
148 timeframe Timeframe
149 expectedJSON string
150 }{
151 {
152 timeframe: Timeframe{StartDate: startDate, EndDate: endDate},
153 expectedJSON: `"2014-02-01,2014-04-01"`,
154 },
155 {
156 timeframe: Timeframe{StartDate: startDate},
157 expectedJSON: `""`,
158 },
159 {
160 timeframe: Timeframe{EndDate: endDate},
161 expectedJSON: `""`,
162 },
163 {
164 timeframe: Timeframe{},
165 expectedJSON: `""`,
166 },
167 }
168
169 for _, test := range tests {
170 bytes, err := json.Marshal(&test.timeframe)
171 if err != nil {
172 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
173 t.Fail()
174 }
175
176 if !reflect.DeepEqual(string(bytes), test.expectedJSON) {
177 t.Logf("Expected date to be '%s', got '%s'\n", test.expectedJSON, string(bytes))
178 t.Fail()
179 }
180 }
181
182 }
183
184 func TestTimeframeUnmarshalJSON(t *testing.T) {
185 startDate := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}
186 endDate := ShortDate{time.Date(2014, time.April, 01, 0, 0, 0, 0, time.UTC)}
187
188 var tests = []struct {
189 testJSON string
190 expectedTimeframe Timeframe
191 }{
192 {
193 `"2014-02-01,2014-04-01"`,
194 Timeframe{StartDate: startDate, EndDate: endDate},
195 },
196 {
197 `"2014-02-01,"`,
198 Timeframe{},
199 },
200 {
201 `""`,
202 Timeframe{},
203 },
204 {
205 `","`,
206 Timeframe{},
207 },
208 {
209 `"2014-02-01,abcde"`,
210 Timeframe{},
211 },
212 {
213 `"abcde,2014-04-01"`,
214 Timeframe{},
215 },
216 {
217 `"abcde,abcde"`,
218 Timeframe{},
219 },
220 }
221
222 for _, test := range tests {
223 var timeframe Timeframe
224 err := json.Unmarshal([]byte(test.testJSON), &timeframe)
225 if err != nil {
226 t.Logf("Expected error to be nil, got %T: %v\n", err, err)
227 t.Fail()
228 }
229
230 if !reflect.DeepEqual(timeframe, test.expectedTimeframe) {
231 t.Logf("Expected date to be '%+#v', got '%+#v'\n", test.expectedTimeframe, timeframe)
232 t.Fail()
233 }
234 }
235 }