Passed
Pull Request — main (#3)
by Adriano
02:14
created

cmd/csvqlctl/csvqlctl_test.go   A

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 53
dl 0
loc 68
c 0
b 0
f 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A csvqlctl_test.TestShouldExecuteWithSuccess 0 22 4
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
var asserts = []struct {
19
	args      []string
20
	filePath  string
21
	fileName  string
22
	data      string
23
	delimiter string
24
	out       string
25
	queries   []string
26
}{
27
	{
28
		args:     []string{"-f", "./../../.tmp/0001.csv", "-d", ";"},
29
		filePath: "./../../.tmp",
30
		fileName: "0001.csv",
31
		data: strings.Join([]string{
32
			"id;name;email",
33
			"0001;teste_1;[email protected]",
34
			"0002;teste_2;[email protected]",
35
			"0003;teste_3;[email protected]",
36
			"0004;teste_4;[email protected]",
37
			"0005;teste_5;[email protected]",
38
		}, "\n"),
39
		delimiter: ";",
40
		out:       "",
41
		queries: []string{
42
			"select * from rows;",
43
		},
44
	},
45
}
46
47
func TestShouldExecuteWithSuccess(t *testing.T) {
48
	cmd, err := csvqlctl.New().Command()
49
	assert.NoError(t, err)
50
51
	for _, test := range asserts {
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