forked from andreaskoch/togglcsv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli_import_test.go
185 lines (144 loc) · 4.53 KB
/
cli_import_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
package main
import (
"bufio"
"bytes"
"fmt"
"strings"
"testing"
)
func Test_togglCli_Execute_ImportActionIsGiven_NoTokenArgumemtGiven_ErrorIsPrinted(t *testing.T) {
// arrange
inputString := ``
inputReader := strings.NewReader(inputString)
var outputBuffer bytes.Buffer
outputWriter := bufio.NewWriter(&outputBuffer)
var errorBuffer bytes.Buffer
errorWriter := bufio.NewWriter(&errorBuffer)
arguments := []string{
"import",
}
cli := togglCli{
importerFactory: func(string) CSVImporter {
return getMockCSVImporter(nil)
},
}
// act
cli.Execute(inputReader, outputWriter, errorWriter, arguments)
// assert
outputWriter.Flush()
errorWriter.Flush()
if !strings.Contains(errorBuffer.String(), `required argument 'token' not provided`) {
t.Fail()
t.Logf("togglCli_Execute should print an error text if the required token parameter is not given but wrote this instead: %s", errorBuffer.String())
}
}
func Test_togglCli_Execute_ImportActionIsGiven_TokenArgumemtGiven_InvalidCSVInputGiven_ErrorIsPrinted(t *testing.T) {
// arrange
inputString := `ddas; -das ... -> dasdas;,,:`
inputReader := strings.NewReader(inputString)
var outputBuffer bytes.Buffer
outputWriter := bufio.NewWriter(&outputBuffer)
var errorBuffer bytes.Buffer
errorWriter := bufio.NewWriter(&errorBuffer)
arguments := []string{
"import",
"1971800d4d82861d8f2c1651fea4d212",
}
cli := togglCli{
importerFactory: func(string) CSVImporter {
return getMockCSVImporter(fmt.Errorf("invalid csv"))
},
}
// act
cli.Execute(inputReader, outputWriter, errorWriter, arguments)
// assert
outputWriter.Flush()
errorWriter.Flush()
if !strings.Contains(errorBuffer.String(), "invalid csv") {
t.Fail()
t.Logf("togglCli_Execute should print an error text if the given input is not valid CSV but wrote this instead: %s", errorBuffer.String())
}
}
func Test_togglCli_Execute_ImportActionIsGiven_TokenArgumemtGiven_NoInputGiven_NoErrorIsPrinted(t *testing.T) {
// arrange
inputString := ``
inputReader := strings.NewReader(inputString)
var outputBuffer bytes.Buffer
outputWriter := bufio.NewWriter(&outputBuffer)
var errorBuffer bytes.Buffer
errorWriter := bufio.NewWriter(&errorBuffer)
arguments := []string{
"import",
"1971800d4d82861d8f2c1651fea4d212",
}
cli := togglCli{
importerFactory: func(string) CSVImporter {
return getMockCSVImporter(nil)
},
}
// act
cli.Execute(inputReader, outputWriter, errorWriter, arguments)
// assert
outputWriter.Flush()
errorWriter.Flush()
if len(errorBuffer.String()) != 0 {
t.Fail()
t.Logf("togglCli_Execute should not print any output if no input was given but returned this instead: %s", errorBuffer.String())
}
}
func Test_togglCli_Execute_ImportActionIsGiven_TokenArgumemtGiven_ValidCSV_ImportSucceeds_NoErrorIsReturned(t *testing.T) {
// arrange
inputString := `Start,Stop,Workspace Name,Project Name,Tag(s),Description
2016-08-12T07:54:47+01:00,2016-08-12T08:19:02+01:00,My Workspace,Project A,"Meetings, Sprint",Retrospective
` // doesn't really matter if this is valid or not
inputReader := strings.NewReader(inputString)
var outputBuffer bytes.Buffer
outputWriter := bufio.NewWriter(&outputBuffer)
var errorBuffer bytes.Buffer
errorWriter := bufio.NewWriter(&errorBuffer)
arguments := []string{
"import",
"1971800d4d82861d8f2c1651fea4d212",
}
cli := togglCli{
importerFactory: func(string) CSVImporter {
return getMockCSVImporter(nil)
},
}
// act
cli.Execute(inputReader, outputWriter, errorWriter, arguments)
// assert
outputWriter.Flush()
errorWriter.Flush()
if len(errorBuffer.String()) != 0 {
t.Fail()
t.Logf("togglCli_Execute should not print an error if the import succeeded: %s", errorBuffer.String())
}
}
func Test_togglCli_Execute_ImportActionIsGiven_TokenArgumemtGiven_ImportFails_ErrorIsReturned(t *testing.T) {
// arrange
inputString := ``
inputReader := strings.NewReader(inputString)
var outputBuffer bytes.Buffer
outputWriter := bufio.NewWriter(&outputBuffer)
var errorBuffer bytes.Buffer
errorWriter := bufio.NewWriter(&errorBuffer)
arguments := []string{
"import",
"1971800d4d82861d8f2c1651fea4d212",
}
cli := togglCli{
importerFactory: func(string) CSVImporter {
return getMockCSVImporter(fmt.Errorf("Import failed"))
},
}
// act
cli.Execute(inputReader, outputWriter, errorWriter, arguments)
// assert
outputWriter.Flush()
errorWriter.Flush()
if !strings.Contains(errorBuffer.String(), "Import failed") {
t.Fail()
t.Logf("togglCli_Execute should print an error if the CSV importer returns one: %s", errorBuffer.String())
}
}