1
|
|
|
package csvqlctl_test |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"adrianolaselva.github.io/csvql/cmd/csvqlctl" |
5
|
|
|
"bytes" |
6
|
|
|
"context" |
7
|
|
|
"fmt" |
8
|
|
|
"github.com/spf13/cobra" |
9
|
|
|
"github.com/stretchr/testify/assert" |
10
|
|
|
"os" |
11
|
|
|
"path/filepath" |
12
|
|
|
"strings" |
13
|
|
|
"testing" |
14
|
|
|
) |
15
|
|
|
|
16
|
|
|
const ( |
17
|
|
|
FileModeDefault os.FileMode = 0644 |
18
|
|
|
) |
19
|
|
|
|
20
|
|
|
//func TestShouldExecuteWithSuccess(t *testing.T) { |
21
|
|
|
// tests := []struct { |
22
|
|
|
// args []string |
23
|
|
|
// filePath string |
24
|
|
|
// fileName string |
25
|
|
|
// data string |
26
|
|
|
// delimiter string |
27
|
|
|
// out string |
28
|
|
|
// queries []string |
29
|
|
|
// }{ |
30
|
|
|
// { |
31
|
|
|
// args: []string{"-f", "./../../.tmp/0001.csv", "-d", ";"}, |
32
|
|
|
// filePath: "./../../.tmp", |
33
|
|
|
// fileName: "0001.csv", |
34
|
|
|
// data: strings.Join([]string{ |
35
|
|
|
// "id;name;email", |
36
|
|
|
// "0001;teste_1;[email protected]", |
37
|
|
|
// "0002;teste_2;[email protected]", |
38
|
|
|
// "0003;teste_3;[email protected]", |
39
|
|
|
// "0004;teste_4;[email protected]", |
40
|
|
|
// "0005;teste_5;[email protected]", |
41
|
|
|
// }, "\n"), |
42
|
|
|
// delimiter: ";", |
43
|
|
|
// out: "", |
44
|
|
|
// queries: []string{ |
45
|
|
|
// "select * from rows;", |
46
|
|
|
// }, |
47
|
|
|
// }, |
48
|
|
|
// } |
49
|
|
|
// |
50
|
|
|
// ctx := context.TODO() |
51
|
|
|
// cmd, err := csvqlctl.New().Command() |
52
|
|
|
// assert.NoError(t, err) |
53
|
|
|
// |
54
|
|
|
// for _, test := range tests { |
55
|
|
|
// _ = os.MkdirAll(test.filePath, os.ModePerm) |
56
|
|
|
// err := os.WriteFile(filepath.Join(test.filePath, test.fileName), bytes.NewBufferString(test.data).Bytes(), FileModeDefault) |
57
|
|
|
// assert.NoError(t, err) |
58
|
|
|
// |
59
|
|
|
// //buf := new(bytes.Buffer) |
60
|
|
|
// //cmd.SetOut(buf) |
61
|
|
|
// //cmd.SetErr(buf) |
62
|
|
|
// |
63
|
|
|
// //cmd.SetArgs(test.args) |
64
|
|
|
// // |
65
|
|
|
// //err = cmd.Execute() |
66
|
|
|
// //assert.NoError(t, err) |
67
|
|
|
// // |
68
|
|
|
// ////cmd.SetIn(strings.NewReader("select * from rows;^D")) |
69
|
|
|
// // |
70
|
|
|
// //_, err = cmd.InOrStdin().Read([]byte("select * from rows;")) |
71
|
|
|
// |
72
|
|
|
// //fmt.Printf("result: %s", buf.String()) |
73
|
|
|
// |
74
|
|
|
// // |
75
|
|
|
// //_, err = cmd.InOrStdin().Read([]byte("select * from rows")) |
76
|
|
|
// //assert.NoError(t, err) |
77
|
|
|
// |
78
|
|
|
// err = executeCommandWithContext(ctx, cmd, test.args...) |
79
|
|
|
// assert.NoError(t, err) |
80
|
|
|
// |
81
|
|
|
// //_, err = cmd.OutOrStdout().Write([]byte("select * from rows;\x0D")) |
82
|
|
|
// //assert.NoError(t, err) |
83
|
|
|
// |
84
|
|
|
// //cmd.Print("select * from rows;\x0D") |
85
|
|
|
// cmd.Println("select * from rows;") |
86
|
|
|
// |
87
|
|
|
// //_, err = cmd.InOrStdin().Read([]byte("select * from rows;\x0D")) |
88
|
|
|
// //assert.NoError(t, err) |
89
|
|
|
// |
90
|
|
|
// //fmt.Printf("rs> out: %s\n", out) |
91
|
|
|
// |
92
|
|
|
// //out := cmd.OutOrStdout() |
93
|
|
|
// //assert.NoError(t, err) |
94
|
|
|
// // |
95
|
|
|
// //c.Printf("select * from rows;") |
96
|
|
|
// |
97
|
|
|
// for i := 0; i < 10; i++ { |
98
|
|
|
// if out := cmd.OutOrStdout(); out != os.Stdout { |
99
|
|
|
// fmt.Printf("out: %s\n", out) |
100
|
|
|
// } |
101
|
|
|
// |
102
|
|
|
// if in := cmd.InOrStdin(); in != os.Stdin { |
103
|
|
|
// fmt.Printf("in:%s\n", in) |
104
|
|
|
// } |
105
|
|
|
// } |
106
|
|
|
// |
107
|
|
|
// //fmt.Println(buf.String()) |
108
|
|
|
// |
109
|
|
|
// //cmd.SetIn(strings.NewReader("select * from rows;")) |
110
|
|
|
// |
111
|
|
|
// //fmt.Printf("->%s\n", scanner.Text()) |
112
|
|
|
// |
113
|
|
|
// //cmd.InOrStdin().Read([]byte("select * from rows;")) |
114
|
|
|
// // |
115
|
|
|
// //for i := 0; i < 10; i++ { |
116
|
|
|
// // fmt.Printf("[%s]\n", cmd.InOrStdin()) |
117
|
|
|
// // time.Sleep(1 * time.Second) |
118
|
|
|
// //} |
119
|
|
|
// |
120
|
|
|
// //ctx.Done() |
121
|
|
|
// |
122
|
|
|
// //cmd.SetIn(strings.NewReader("select * from rows;")) |
123
|
|
|
// |
124
|
|
|
// //if out := cmd.InOrStdin(); out != os.Stdout { |
125
|
|
|
// // fmt.Printf("%s", out) |
126
|
|
|
// // //t.Errorf("Expected setting input to nil to revert back to stdin") |
127
|
|
|
// //} |
128
|
|
|
// |
129
|
|
|
// //fmt.Printf("->out: %s\n", out) |
130
|
|
|
// } |
131
|
|
|
//} |
132
|
|
|
|
133
|
|
|
func executeCommandWithContext(ctx context.Context, root *cobra.Command, args ...string) error { |
134
|
|
|
//buf := new(bytes.Buffer) |
135
|
|
|
root.SetOut(os.Stdout) |
136
|
|
|
root.SetErr(os.Stderr) |
137
|
|
|
root.SetArgs(args) |
138
|
|
|
|
139
|
|
|
err := root.ExecuteContext(ctx) |
140
|
|
|
|
141
|
|
|
return err |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
func TestShouldExecuteWithSuccess(t *testing.T) { |
145
|
|
|
tests := []struct { |
146
|
|
|
args []string |
147
|
|
|
filePath string |
148
|
|
|
fileName string |
149
|
|
|
data string |
150
|
|
|
delimiter string |
151
|
|
|
out string |
152
|
|
|
queries []string |
153
|
|
|
}{ |
154
|
|
|
{ |
155
|
|
|
args: []string{"-f", "./../../.tmp/0001.csv", "-d", ";"}, |
156
|
|
|
filePath: "./../../.tmp", |
157
|
|
|
fileName: "0001.csv", |
158
|
|
|
data: strings.Join([]string{ |
159
|
|
|
"id;name;email", |
160
|
|
|
"0001;teste_1;[email protected]", |
161
|
|
|
"0002;teste_2;[email protected]", |
162
|
|
|
"0003;teste_3;[email protected]", |
163
|
|
|
"0004;teste_4;[email protected]", |
164
|
|
|
"0005;teste_5;[email protected]", |
165
|
|
|
}, "\n"), |
166
|
|
|
delimiter: ";", |
167
|
|
|
out: "", |
168
|
|
|
queries: []string{ |
169
|
|
|
"select * from rows;", |
170
|
|
|
}, |
171
|
|
|
}, |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
//ctx := context.TODO() |
175
|
|
|
cmd, err := csvqlctl.New().Command() |
176
|
|
|
assert.NoError(t, err) |
177
|
|
|
|
178
|
|
|
for _, test := range tests { |
179
|
|
|
_ = os.MkdirAll(test.filePath, os.ModePerm) |
180
|
|
|
err := os.WriteFile(filepath.Join(test.filePath, test.fileName), bytes.NewBufferString(test.data).Bytes(), FileModeDefault) |
181
|
|
|
assert.NoError(t, err) |
182
|
|
|
|
183
|
|
|
buf := new(bytes.Buffer) |
184
|
|
|
cmd.SetOut(buf) |
185
|
|
|
cmd.SetErr(buf) |
186
|
|
|
cmd.SetArgs(test.args) |
187
|
|
|
|
188
|
|
|
err = cmd.Execute() |
189
|
|
|
assert.NoError(t, err) |
190
|
|
|
|
191
|
|
|
for _, q := range test.queries { |
192
|
|
|
_, err = cmd.OutOrStdout().Write([]byte(q)) |
193
|
|
|
assert.NoError(t, err) |
194
|
|
|
if out := cmd.OutOrStdout(); out != os.Stdout { |
195
|
|
|
assert.Equal(t, q, fmt.Sprintf("%s", out)) |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|