Passed
Push — main ( 28dc56...130f2a )
by Adriano
57s queued 12s
created

csvqlctl_test.TestShouldExecuteWithSuccess   A

Complexity

Conditions 4

Size

Total Lines 22
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 18
nop 1
dl 0
loc 22
rs 9.5
c 0
b 0
f 0
1
package csvqlctl_test
2
3
import (
4
	"adrianolaselva.github.io/csvql/cmd/csvqlctl"
5
	"bytes"
6
	"fmt"
7
	"github.com/stretchr/testify/assert"
8
	"os"
9
	"path/filepath"
10
	"strings"
11
	"testing"
12
)
13
14
const (
15
	FileModeDefault os.FileMode = 0644
16
)
17
18
func TestShouldExecuteWithSuccess(t *testing.T) {
19
	var tests = []struct {
20
		args      []string
21
		filePath  string
22
		fileName  string
23
		data      string
24
		delimiter string
25
		out       string
26
		queries   []string
27
	}{
28
		{
29
			args:     []string{"-f", "./../../.tmp/0001.csv", "-d", ";"},
30
			filePath: "./../../.tmp",
31
			fileName: "0001.csv",
32
			data: strings.Join([]string{
33
				"id;name;email",
34
				"0001;teste_1;[email protected]",
35
				"0002;teste_2;[email protected]",
36
				"0003;teste_3;[email protected]",
37
				"0004;teste_4;[email protected]",
38
				"0005;teste_5;[email protected]",
39
			}, "\n"),
40
			delimiter: ";",
41
			out:       "",
42
			queries: []string{
43
				"select * from rows;",
44
			},
45
		},
46
	}
47
48
	cmd, err := csvqlctl.New().Command()
49
	assert.NoError(t, err)
50
51
	for _, test := range tests {
52
		_ = os.MkdirAll(test.filePath, os.ModePerm)
53
		err := os.WriteFile(filepath.Join(test.filePath, test.fileName), bytes.NewBufferString(test.data).Bytes(), FileModeDefault)
54
		assert.NoError(t, err)
55
56
		buf := new(bytes.Buffer)
57
		cmd.SetOut(buf)
58
		cmd.SetErr(buf)
59
		cmd.SetArgs(test.args)
60
61
		err = cmd.Execute()
62
		assert.NoError(t, err)
63
64
		for _, q := range test.queries {
65
			_, err = cmd.OutOrStdout().Write([]byte(q))
66
			assert.NoError(t, err)
67
			if out := cmd.OutOrStdout(); out != os.Stdout {
68
				assert.Equal(t, q, fmt.Sprintf("%s", out))
69
			}
70
		}
71
	}
72
}
73