-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvtojson_test.go
99 lines (86 loc) · 2.98 KB
/
csvtojson_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"flag"
"io/ioutil"
"os"
"reflect"
"testing"
)
func Test_getOpts(t *testing.T) {
// Defining test slice -- each unit test should have the follow properties:
tests := []struct {
name string // name of the test
want options // what options instance our function should return
wantErr bool // whether we want an error
osArgs []string // The command args used for this test
}{
// Here we're declaring each unit test input and output data as defined above
{"Default parameters", options{"test.csv", "comma", false, ""}, false, []string{"cmd", "test.csv"}},
{"No parameters", options{}, true, []string{"cmd"}},
{"Semicolon enabled", options{"test.csv", "semicolon", false, ""}, false, []string{"cmd", "--separator=semicolon", "test.csv"}},
{"Pretty enabled", options{"test.csv", "comma", true, ""}, false, []string{"cmd", "--pretty", "test.csv"}},
{"Pretty and semicolon enabled", options{"test.csv", "semicolon", true, ""}, false, []string{"cmd", "--pretty", "--separator=semicolon", "test.csv"}},
{"Separator not identified", options{}, true, []string{"cmd", "--separator=pipe", "test.csv"}},
}
// Iterate over our test slice
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Save reference to original os.Args
actualOsArgs := os.Args
// Function to run once test is done
defer func() {
// Resetting the original os.Args
os.Args = actualOsArgs
// Resetting the Flag command ling so we can parse flags again
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
}()
// Setting the command line args for this specifc test
os.Args = tt.osArgs
// Running the function we wish to test
got, err := getOpts()
// An assertion of whether or not we want to get an error value
if (err != nil) != tt.wantErr {
t.Errorf("getOpts() error = %v, wantErr %v", err, tt.wantErr)
}
// Asserting we are getting the corrent "want" value
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("getOpts() = %v, want %v", got, tt.want)
}
})
}
}
func Test_checkIfValidFile(t *testing.T) {
// Creating a temporay empty csv file
tmpfile, err := ioutil.TempFile("", "test*.csv")
// Just in case creating the temporary file returns an error, shouldn't ever happen.
if err != nil {
panic(err)
}
// Remove the file once the function is done
defer os.Remove(tmpfile.Name())
type args struct {
filename string
}
tests := []struct {
name string
filename string
want bool
wantErr bool
}{
{"File does exist", tmpfile.Name(), true, false},
{"File does not exist", "nowhere/test.csv", false, true},
{"File is not csv", "test.txt", false, true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := checkIfValidFile(tt.filename)
if (err != nil) != tt.wantErr {
t.Errorf("checkIfValidFile() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("checkIfValidFile() = %v, want %v", got, tt.want)
}
})
}
}