forked from andreaskoch/togglcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvmapper_test.go
337 lines (272 loc) · 9.94 KB
/
csvmapper_test.go
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package main
import (
"strings"
"testing"
"github.com/andreaskoch/togglapi/date"
"github.com/andreaskoch/togglcsv/toggl"
)
func Test_GetTimeRecord_InvalidNumberOfColumns_ErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
inputRows := [][]string{
// Too many
[]string{"2015-03-26T08:53:56Z", "2015-03-26T12:55:10+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"},
// Too few
[]string{"Project XY", "Client X", "", "Some stuff"},
}
for _, row := range inputRows {
// act
_, err := csvMapper.GetTimeRecord(row)
// assert
if err == nil {
t.Fail()
t.Logf("GetTimeRecord should return an error the number of columns is wrong")
}
}
}
func Test_GetTimeRecord_InvalidStartDateFormat_ErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
row := []string{"2015-03-26", "2015-03-26T12:55:10+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"}
// act
_, err := csvMapper.GetTimeRecord(row)
// assert
if err == nil {
t.Fail()
t.Logf("GetTimeRecord should return an error if the given start date format is invalid")
}
}
func Test_GetTimeRecord_InvalidStopDateFormat_ErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
row := []string{"2015-03-26T08:00:00+01:00", "2015-03-26", "Workspace", "Project XY", "Client X", "", "Some stuff"}
// act
_, err := csvMapper.GetTimeRecord(row)
// assert
if err == nil {
t.Fail()
t.Logf("GetTimeRecord should return an error if the given stop date format is invalid")
}
}
func Test_GetTimeRecord_TooLongDescription_ErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
oneHundredCharacters := `Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean m`
veryLongDescription := ""
for i := 0; i < 40; i++ {
veryLongDescription = veryLongDescription + oneHundredCharacters
}
row := []string{"2015-03-26T08:00:00+01:00", "2015-03-26T11:00:00+01:00", "Workspace", "Project XY", "Client X", "", veryLongDescription}
// act
_, err := csvMapper.GetTimeRecord(row)
// assert
if err == nil {
t.Fail()
t.Logf("GetTimeRecord should return an error if the given description is too long")
}
}
func Test_GetTimeRecord_ValidInput_NoErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
row := []string{"2015-03-26T08:00:00+01:00", "2015-03-26T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"}
// act
_, err := csvMapper.GetTimeRecord(row)
// assert
if err != nil {
t.Fail()
t.Logf("GetTimeRecord have returned a time entry but returned an error instead: %s", err)
}
}
func Test_GetTimeRecord_ValidInput_TimeRecordIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
row := []string{"2015-03-26T08:00:00+01:00", "2015-03-26T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"}
// act
timeRecord, _ := csvMapper.GetTimeRecord(row)
// assert
if timeRecord.WorkspaceName != "Workspace" {
t.Fail()
t.Logf("The workspace name was not returned correctly: %#v", timeRecord)
}
if timeRecord.ProjectName != "Project XY" {
t.Fail()
t.Logf("The project name was not returned correctly: %#v", timeRecord)
}
if len(timeRecord.Tags) == 0 {
t.Fail()
t.Logf("The tags were not returned correctly: %#v", timeRecord)
}
if timeRecord.Description != "Some stuff" {
t.Fail()
t.Logf("The description was not returned correctly: %#v", timeRecord)
}
}
func Test_GetTimeRecords_NoRowsGiven_EmptyListReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
var rows [][]string
// act
records, _ := csvMapper.GetTimeRecords(rows)
// assert
if len(records) > 0 {
t.Fail()
t.Logf("GetTimeRecords should not return time records if the given rows were empty")
}
}
func Test_GetTimeRecords_OnlyHeadlineGiven_EmptyListReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
rows := [][]string{
[]string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
}
// act
records, _ := csvMapper.GetTimeRecords(rows)
// assert
if len(records) > 0 {
t.Fail()
t.Logf("GetTimeRecords should not return time records if the given rows only contained the headline")
}
}
func Test_GetTimeRecords_HeadlineGiven_RowCannotBeParsed_ErrorIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
rows := [][]string{
[]string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
[]string{"Invalid Date", "2015-03-26T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"},
}
// act
_, err := csvMapper.GetTimeRecords(rows)
// assert
if err == nil {
t.Fail()
t.Logf("GetTimeRecords should have returned an error")
}
}
func Test_GetTimeRecords_HeadlineGiven_ValidRow_OneRecordIsReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
rows := [][]string{
[]string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
[]string{"2015-03-26T08:00:00+01:00", "2015-03-26T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"},
}
// act
records, _ := csvMapper.GetTimeRecords(rows)
// assert
if len(records) != 1 {
t.Fail()
t.Logf("GetTimeRecords should have returned one time record")
}
}
func Test_GetTimeRecords_HeadlineGiven_ThreeRows_ThreeRecordsAreReturned(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
rows := [][]string{
[]string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
[]string{"2015-03-26T08:00:00+01:00", "2015-03-26T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some stuff"},
[]string{"2015-03-27T08:00:00+01:00", "2015-03-27T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some more stuff"},
[]string{"2015-03-28T08:00:00+01:00", "2015-03-28T11:30:00+01:00", "Workspace", "Project XY", "Client X", "", "Some more stuff"},
}
// act
records, _ := csvMapper.GetTimeRecords(rows)
// assert
if len(records) != 3 {
t.Fail()
t.Logf("GetTimeRecords should have returned three time records")
}
}
func Test_GetColumnNames_GivenColumnNamesAreReturnedWithoutModification(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
// act
columnNames := csvMapper.GetColumnNames()
// assert
expected := "Start,Stop,Workspace Name,Project Name,Client Name,Tag(s),Description"
if strings.Join(columnNames, ",") != expected {
t.Fail()
t.Logf("GetColumnNames should have returned %q", expected)
}
}
func Test_GetRow(t *testing.T) {
// arrange
dateFormatter := date.NewISO8601Formatter()
csvMapper := &CSVTimeRecordMapper{
dateFormatter: dateFormatter,
columnNames: []string{"Start", "Stop", "Workspace Name", "Project Name", "Client Name", "Tag(s)", "Description"},
tagsSeparator: ",",
}
timeRecord := toggl.TimeRecord{
WorkspaceName: "Workspace",
ProjectName: "Project XY",
ClientName: "Client X",
Tags: []string{"Tag 1", "Tag 2", "XYZ"},
Description: "Some stuff",
}
// act
row := csvMapper.GetRow(timeRecord)
// assert
expected := "0001-01-01T00:00:00+00:00|0001-01-01T00:00:00+00:00|Workspace|Project XY|Client X|Tag 1,Tag 2,XYZ|Some stuff"
if strings.Join(row, "|") != expected {
t.Fail()
t.Logf("GetRow returned an invalid value. Expected: %q, Actual: %q", expected, strings.Join(row, "|"))
}
}