Skip to content

Commit

Permalink
Imporove test
Browse files Browse the repository at this point in the history
Fixed golangci-lint's suggestion.
add cmd/cmd_test.go
add output_csv_test.go
  • Loading branch information
noborus committed Dec 25, 2019
1 parent 7fe70be commit 413e4a6
Show file tree
Hide file tree
Showing 8 changed files with 319 additions and 11 deletions.
4 changes: 4 additions & 0 deletions analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,13 @@ func Analyze(fileName string, opts *AnalyzeOpts, readOpts *ReadOpts) error {

func examples(tableName string, names []string, results []string) []string {
queries := []string{
// #nosec G201
fmt.Sprintf("SELECT %s FROM %s", strings.Join(names, ", "), tableName),
// #nosec G201
fmt.Sprintf("SELECT %s FROM %s WHERE %s = '%s'", strings.Join(names, ", "), tableName, names[0], results[0]),
// #nosec G201
fmt.Sprintf("SELECT %s, count(%s) FROM %s GROUP BY %s", names[0], names[0], tableName, names[0]),
// #nosec G201
fmt.Sprintf("SELECT %s FROM %s ORDER BY %s LIMIT 10", strings.Join(names, ", "), tableName, names[0]),
}
return queries
Expand Down
1 change: 1 addition & 0 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ func getQuery(args []string, fileName string) (string, error) {
} else {
query = strings.Join(args, " ")
}

if strings.HasSuffix(query, ";") {
query = query[:len(query)-1]
}
Expand Down
211 changes: 211 additions & 0 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
package cmd

import (
"path/filepath"
"reflect"
"testing"

"github.com/noborus/trdsql"
)

func Test_inputFormat(t *testing.T) {
type args struct {
i inputFlag
}
tests := []struct {
name string
args args
want trdsql.Format
}{
{
name: "testCSV",
args: args{
i: inputFlag{
CSV: true,
},
},
want: trdsql.CSV,
},
{
name: "testTBLN",
args: args{
i: inputFlag{
TBLN: true,
},
},
want: trdsql.TBLN,
},
{
name: "testGUESS",
args: args{
i: inputFlag{},
},
want: trdsql.GUESS,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := inputFormat(tt.args.i); !reflect.DeepEqual(got, tt.want) {
t.Errorf("inputFormat() = %v, want %v", got, tt.want)
}
})
}
}

func Test_outputFormat(t *testing.T) {
type args struct {
o outputFlag
}
tests := []struct {
name string
args args
want trdsql.Format
}{
{
name: "testCSV",
args: args{
o: outputFlag{
CSV: true,
},
},
want: trdsql.CSV,
},
{
name: "testTBLN",
args: args{
o: outputFlag{
TBLN: true,
},
},
want: trdsql.TBLN,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := outputFormat(tt.args.o); !reflect.DeepEqual(got, tt.want) {
t.Errorf("outputFormat() = %v, want %v", got, tt.want)
}
})
}
}

func Test_getQuery(t *testing.T) {
type argss struct {
args []string
fileName string
}
tests := []struct {
name string
argss argss
want string
wantErr bool
}{
{
name: "testARGS",
argss: argss{
[]string{"SELECT 1"},
"",
},
want: "SELECT 1",
wantErr: false,
},
{
name: "testARGS2",
argss: argss{
[]string{"SELECT", "1"},
"",
},
want: "SELECT 1",
wantErr: false,
},
{
name: "testFile",
argss: argss{
[]string{},
filepath.Join("..", "testdata", "test.sql"),
},
want: "SELECT * FROM testdata/test.csv\n",
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getQuery(tt.argss.args, tt.argss.fileName)
if (err != nil) != tt.wantErr {
t.Errorf("getQuery() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getQuery() = %v, want %v", got, tt.want)
}
})
}
}

func Test_getDB(t *testing.T) {
type args struct {
cfg *config
cDB string
cDriver string
cDSN string
}
tests := []struct {
name string
args args
want string
want1 string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, got1 := getDB(tt.args.cfg, tt.args.cDB, tt.args.cDriver, tt.args.cDSN)
if got != tt.want {
t.Errorf("getDB() got = %v, want %v", got, tt.want)
}
if got1 != tt.want1 {
t.Errorf("getDB() got1 = %v, want %v", got1, tt.want1)
}
})
}
}

func Test_getCommand(t *testing.T) {
type args struct {
args []string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := getCommand(tt.args.args); got != tt.want {
t.Errorf("getCommand() = %v, want %v", got, tt.want)
}
})
}
}

func Test_quotedArg(t *testing.T) {
type args struct {
arg string
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := quotedArg(tt.args.arg); got != tt.want {
t.Errorf("quotedArg() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Ken,Thompson,ken
trdsql.NewImporter(),
trdsql.NewExporter(trdsql.NewWriter()),
)
// #nosec G201
query := fmt.Sprintf("SELECT c1 FROM %s ORDER BY c1", tmpfile.Name())
err = trd.Exec(query)
if err != nil {
Expand Down Expand Up @@ -72,6 +73,7 @@ Ken,Thompson,ken
exporter := trdsql.NewExporter(writer)

trd := trdsql.NewTRDSQL(importer, exporter)
// #nosec G201
query := fmt.Sprintf("SELECT * FROM %s ORDER BY username", tmpfile.Name())
err = trd.Exec(query)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions input_json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,16 @@ func arraySortEqual(t *testing.T, a []string, b []string) bool {
return false
}

a_copy := make([]string, len(a))
b_copy := make([]string, len(b))
copyA := make([]string, len(a))
copyB := make([]string, len(b))

copy(a_copy, a)
copy(b_copy, b)
copy(copyA, a)
copy(copyB, b)

sort.Strings(a_copy)
sort.Strings(b_copy)
sort.Strings(copyA)
sort.Strings(copyB)

return reflect.DeepEqual(a_copy, b_copy)
return reflect.DeepEqual(copyA, copyB)
}

func TestNewJSONReader(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions input_tbln.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func NewTBLNReader(reader io.Reader) (*TBLNRead, error) {
func (r *TBLNRead) Names() ([]string, error) {
reader := r.reader
if reader == nil {
return nil, fmt.Errorf("No Definition")
return nil, fmt.Errorf("no Definition")
}
d := reader.GetDefinition()
return d.Names(), nil
Expand All @@ -77,7 +77,7 @@ func (r *TBLNRead) Names() ([]string, error) {
func (r *TBLNRead) Types() ([]string, error) {
reader := r.reader
if reader == nil {
return nil, fmt.Errorf("No Definition")
return nil, fmt.Errorf("no Definition")
}
d := reader.GetDefinition()
return d.Types(), nil
Expand Down
3 changes: 1 addition & 2 deletions output_csv.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@ func (w *CSVWriter) WriteRow(values []interface{}, columns []string) error {
for i, col := range values {
w.results[i] = ValString(col)
}
err := w.writer.Write(w.results)
return err
return w.writer.Write(w.results)
}

// PostWrite is flush.
Expand Down
91 changes: 91 additions & 0 deletions output_csv_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package trdsql

import (
"bytes"
"reflect"
"testing"
)

func TestNewCSVWriter(t *testing.T) {
outStream := new(bytes.Buffer)
type args struct {
writeOpts *WriteOpts
}
tests := []struct {
name string
args args
want rune
}{
{
name: "testDefault",
args: args{
writeOpts: &WriteOpts{
OutDelimiter: ",",
OutStream: outStream,
},
},
want: ',',
},
{
name: "invalidDelimiter",
args: args{
writeOpts: &WriteOpts{
OutDelimiter: "--",
OutStream: outStream,
},
},
want: ',',
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := NewCSVWriter(tt.args.writeOpts)
if !reflect.DeepEqual(got.writer.Comma, tt.want) {
t.Errorf("NewCSVWriter() = %v, want %v", got.writer.Comma, tt.want)
}
})
}
}

func TestCSVWriter_PreWrite(t *testing.T) {
type args struct {
columns []string
types []string
}
tests := []struct {
name string
writeOpts WriteOpts
args args
wantErr bool
}{
{
name: "empty",
writeOpts: WriteOpts{
OutDelimiter: ",",
},
args: args{
columns: []string{},
types: []string{},
},
},
{
name: "emptyHeader",
writeOpts: WriteOpts{
OutDelimiter: ",",
OutHeader: true,
},
args: args{
columns: []string{"h1", "h2"},
types: []string{"text", "text"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
w := NewCSVWriter(&tt.writeOpts)
if err := w.PreWrite(tt.args.columns, tt.args.types); (err != nil) != tt.wantErr {
t.Errorf("CSVWriter.PreWrite() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}

0 comments on commit 413e4a6

Please sign in to comment.