File: time_test.go

package info (click to toggle)
golang-github-mitch000001-go-hbci 0.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 2,468 kB
  • sloc: java: 1,092; makefile: 5
file content (235 lines) | stat: -rw-r--r-- 5,233 bytes parent folder | download | duplicates (2)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package domain

import (
	"encoding/json"
	"reflect"
	"testing"
	"time"
)

func TestNewShortDate(t *testing.T) {
	dateTime := time.Date(2004, 02, 01, 5, 18, 47, 321, time.UTC)
	expectedDate := time.Date(2004, 02, 01, 0, 0, 0, 0, time.UTC)

	shortDate := NewShortDate(dateTime)

	if !reflect.DeepEqual(expectedDate, shortDate.Time) {
		t.Logf("Expected date to equal '%s', got '%s'\n", expectedDate, shortDate.Time)
		t.Fail()
	}

}

func TestDate(t *testing.T) {
	date := time.Date(2004, 02, 01, 0, 0, 0, 0, time.UTC)

	shortDate := Date(2004, 02, 01, time.UTC)

	if !reflect.DeepEqual(date, shortDate.Time) {
		t.Logf("Expected date to equal '%s', got '%s'\n", date, shortDate.Time)
		t.Fail()
	}
}

func TestShortDateUnmarshalJSON(t *testing.T) {
	testJSON := `"2014-02-01"`

	var date ShortDate

	err := json.Unmarshal([]byte(testJSON), &date)

	if err != nil {
		t.Logf("Expected error to be nil, got %T: %v\n", err, err)
		t.Fail()
	}

	if &date == nil {
		t.Logf("Expected date not to be nil\n")
		t.Fail()
	}

	expectedDate, err := time.Parse("2006-01-02", "2014-02-01")
	expectedShortDate := ShortDate{expectedDate}

	if err != nil {
		t.Logf("Expected error to be nil, got %T: %v\n", err, err)
		t.Fail()
	}

	if !reflect.DeepEqual(expectedShortDate, date) {
		t.Logf("Expected date to be '%+#v', got '%+#v'\n", expectedShortDate, date)
		t.Fail()
	}
}

func TestShortDateMarshalJSON(t *testing.T) {
	date := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}

	bytes, err := json.Marshal(&date)

	if err != nil {
		t.Logf("Expected error to be nil, got %T: %v\n", err, err)
		t.Fail()
	}

	expectedJSON := `"2014-02-01"`

	if !reflect.DeepEqual(string(bytes), expectedJSON) {
		t.Logf("Expected date to be '%s', got '%s'\n", expectedJSON, string(bytes))
		t.Fail()
	}

	// Date is Zero
	date = ShortDate{}

	bytes, err = json.Marshal(&date)

	if err != nil {
		t.Logf("Expected error to be nil, got %T: %v\n", err, err)
		t.Fail()
	}

	expectedJSON = `""`

	if !reflect.DeepEqual(string(bytes), expectedJSON) {
		t.Logf("Expected date to be '%s', got '%s'\n", expectedJSON, string(bytes))
		t.Fail()
	}
}

func TestFrom(t *testing.T) {
	now := time.Now()
	today := Date(now.Year(), now.Month(), now.Day(), now.Location())
	locations := []*time.Location{
		time.UTC,
		mustLoad(time.LoadLocation("America/New_York")),
		mustLoad(time.LoadLocation("Australia/Perth")),
	}

	var tests = []struct {
		startDate       ShortDate
		expectedEndDate ShortDate
	}{
		{
			startDate:       Date(2010, 02, 01, locations[0]),
			expectedEndDate: today,
		},
		{
			startDate:       Date(2010, 02, 01, locations[1]),
			expectedEndDate: Date(now.Year(), now.Month(), now.Day(), locations[1]),
		},
		{
			startDate:       Date(2010, 02, 01, locations[2]),
			expectedEndDate: Date(now.Year(), now.Month(), now.Day(), locations[2]),
		},
	}

	for _, test := range tests {
		actualEndDate := TimeframeFromDate(test.startDate).EndDate
		if !reflect.DeepEqual(test.expectedEndDate, actualEndDate) {
			t.Logf("Expected EndDate to equal '%s', got '%s'\n", test.expectedEndDate, actualEndDate)
			t.Fail()
		}
	}
}

func mustLoad(loc *time.Location, err error) *time.Location {
	if err != nil {
		panic(err)
	}
	return loc
}

func TestTimeframeMarshalJSON(t *testing.T) {
	startDate := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}
	endDate := ShortDate{time.Date(2014, time.April, 01, 0, 0, 0, 0, time.UTC)}

	var tests = []struct {
		timeframe    Timeframe
		expectedJSON string
	}{
		{
			timeframe:    Timeframe{StartDate: startDate, EndDate: endDate},
			expectedJSON: `"2014-02-01,2014-04-01"`,
		},
		{
			timeframe:    Timeframe{StartDate: startDate},
			expectedJSON: `""`,
		},
		{
			timeframe:    Timeframe{EndDate: endDate},
			expectedJSON: `""`,
		},
		{
			timeframe:    Timeframe{},
			expectedJSON: `""`,
		},
	}

	for _, test := range tests {
		bytes, err := json.Marshal(&test.timeframe)
		if err != nil {
			t.Logf("Expected error to be nil, got %T: %v\n", err, err)
			t.Fail()
		}

		if !reflect.DeepEqual(string(bytes), test.expectedJSON) {
			t.Logf("Expected date to be '%s', got '%s'\n", test.expectedJSON, string(bytes))
			t.Fail()
		}
	}

}

func TestTimeframeUnmarshalJSON(t *testing.T) {
	startDate := ShortDate{time.Date(2014, time.February, 01, 0, 0, 0, 0, time.UTC)}
	endDate := ShortDate{time.Date(2014, time.April, 01, 0, 0, 0, 0, time.UTC)}

	var tests = []struct {
		testJSON          string
		expectedTimeframe Timeframe
	}{
		{
			`"2014-02-01,2014-04-01"`,
			Timeframe{StartDate: startDate, EndDate: endDate},
		},
		{
			`"2014-02-01,"`,
			Timeframe{},
		},
		{
			`""`,
			Timeframe{},
		},
		{
			`","`,
			Timeframe{},
		},
		{
			`"2014-02-01,abcde"`,
			Timeframe{},
		},
		{
			`"abcde,2014-04-01"`,
			Timeframe{},
		},
		{
			`"abcde,abcde"`,
			Timeframe{},
		},
	}

	for _, test := range tests {
		var timeframe Timeframe
		err := json.Unmarshal([]byte(test.testJSON), &timeframe)
		if err != nil {
			t.Logf("Expected error to be nil, got %T: %v\n", err, err)
			t.Fail()
		}

		if !reflect.DeepEqual(timeframe, test.expectedTimeframe) {
			t.Logf("Expected date to be '%+#v', got '%+#v'\n", test.expectedTimeframe, timeframe)
			t.Fail()
		}
	}
}