Conditions | 4 |
Total Lines | 52 |
Code Lines | 42 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package csvqlctl_test |
||
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 | } |
||
200 |