-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomer_importer_test.go
177 lines (149 loc) · 4.17 KB
/
customer_importer_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package customerimporter
import (
"strings"
"testing"
"bytes"
"encoding/csv"
"reflect"
)
func emptyOption() Option { return func(f *CustomerImporter) {} }
// test with reader
func TestImport(t *testing.T) {
// set header of the csv file
header := "first_name,last_name,email,gender,ip_address"
// Test import with different inputs, options and results
t.Log("Test import with different inputs, options and results")
data := []struct {
records []string
option Option
err error
result EmailsByDomainQtyList
}{
// working case without options
{[]string{"Mildred,Hernandez,[email protected],Female,38.194.51.128"},
emptyOption(),
nil,
EmailsByDomainQtyList{{"github.io", 1}},
},
// working sorting case
{[]string{
"Mildred,Hernandez,[email protected],Female,38.194.51.128",
"Mildred,Hernandez,[email protected],Female,38.194.51.128",
"Mildred,Hernandez,[email protected],Female,38.194.51.128",
"Mildred,Hernandez,[email protected],Female,38.194.51.128",
},
emptyOption(),
nil,
EmailsByDomainQtyList{
{"a.io", 1},
{"b.io", 1},
{"c.io", 1},
{"d.io", 1},
},
},
// case with invalid email
{[]string{"Mildred,Hernandez,mhernandezgithub.io,Female,38.194.51.128"},
emptyOption(),
ErrEmailIsNotValid,
nil,
},
// case with empty email
{[]string{"Mildred,Hernandez,,Female,38.194.51.128"},
emptyOption(),
ErrEmailIsNotValid,
nil,
},
// case with invalid email but with SkipErrInvalidEmails option enabled
{[]string{"Mildred,Hernandez,mhernandezgithub.io,Female,38.194.51.128"},
SkipErrInvalidEmails(),
ErrNoValidEmailsFound,
nil,
},
// case with duplicate emails without options
{[]string{"Mildred,Hernandez,[email protected],Female,38.194.51.128",
"Mildred,Hernandez,[email protected],Female,38.194.51.128"},
emptyOption(),
ErrEmailDuplicate,
nil,
},
// case with duplicate emails but with SkipErrDuplicateEmails option
{[]string{"Mildred,Hernandez,[email protected],Female,38.194.51.128",
"Mildred,Hernandez,[email protected],Female,38.194.51.128"},
SkipErrDuplicateEmails(),
nil,
EmailsByDomainQtyList{{"github.io", 1}},
},
// case with wrong number of fields
{[]string{"Mildred,Hernandez"},
emptyOption(),
csv.ErrFieldCount,
nil,
},
// error should contain correct line and column
{[]string{"Mildred,Hernandez,mhernandezgithub.io,Female,38.194.51.128"},
emptyOption(),
&csv.ParseError{2, 2, ErrEmailIsNotValid},
nil,
},
}
for testNumber, d := range data {
t.Logf("Case: %v", testNumber)
// put data to buffer
b := bytes.NewBufferString(header + "\n")
for i, r := range d.records {
b.WriteString(r)
if i < len(d.records)-1 {
b.WriteString("\n")
}
}
// import from buffer
result, err := Import(b, "email", d.option)
// check for correct error handling
if err != nil && !strings.Contains(err.Error(), d.err.Error()) {
t.Errorf("should raise error: %v, but got error %v ", d.err, err)
}
//check for correct results
if result != nil && !reflect.DeepEqual(*result, d.result) {
t.Errorf("should result with: %v, but got %v", *result, d.result)
}
}
// test non existing email field
t.Log("Test non existing email field")
// put data to buffer
b := bytes.NewBufferString(header + "\n")
// import from buffer
result, err := Import(b, "invalid field")
// check for correct error handling
if err != nil && !strings.Contains(err.Error(), ErrFieldNotExists.Error()) {
t.Errorf("should raise error: %v, but got error %v ", err, ErrFieldNotExists)
}
// check for empty result
if result != nil {
t.Error("result should be empty")
}
}
// test with files
func TestImportFromFile(t *testing.T) {
// test with existing file
t.Log("Test existing file")
_, err := ImportFromFile(
"./customers.csv",
"email",
SkipErrInvalidEmails(),
SkipErrDuplicateEmails(),
)
if err != nil {
t.Errorf("should pass the test")
}
// test with non existing file
t.Log("Test non existing file")
_, err = ImportFromFile(
"./nonexisting.csv",
"email",
SkipErrInvalidEmails(),
SkipErrDuplicateEmails(),
)
if !strings.Contains(err.Error(), "no such file or directory") {
t.Errorf("should raise the error")
}
}