-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathetl_test.go
85 lines (70 loc) · 2.84 KB
/
etl_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
package sparkypmtatracking_test
import (
"strings"
"testing"
spmta "github.com/tuck1s/sparkypmtatracking"
)
const exampleCSV = `type,rcpt,header_x-sp-message-id,header_x-sp-subaccount-id
d,[email protected],0000123456789abcdef0,0
d,[email protected],0000123456789abcdef1,1
d,[email protected],0000123456789abcdef2,2
`
const validMinimalHeader = "type,header_x-sp-message-id\n"
const validHeaderWithRcpt = "type,rcpt,header_x-sp-message-id\n"
func loadCSV(csv string) error {
f := strings.NewReader(csv)
return spmta.AccountETL(f)
}
func loadCSVandCheckError(t *testing.T, csv string) {
if err := loadCSV(csv); err != nil {
t.Error(err)
}
}
func TestAccountETL(t *testing.T) {
loadCSVandCheckError(t, exampleCSV)
loadCSVandCheckError(t, "\n") // empty file, should read OK
loadCSVandCheckError(t, validMinimalHeader+"d,f00dbeef") // small input
}
// checks err contains a substring of an "expected" error
func checkExpectedError(t *testing.T, err error, s string) {
if err == nil {
t.Errorf("was expecting an error with %s, got %v", s, err)
} else if !strings.Contains(err.Error(), s) {
t.Error(err)
}
}
func TestAccountETLFaultyInputs(t *testing.T) {
// missing required header field
err := loadCSV("type,rcpt\n" + "d,[email protected]")
checkExpectedError(t, err, "header_x-sp-message-id is not present")
// incorrect record type
err = loadCSV(validHeaderWithRcpt + "x,[email protected],f00dbeef")
checkExpectedError(t, err, "record not of expected type")
// missing data
err = loadCSV("type\n" + "d")
checkExpectedError(t, err, "Insufficient data fields")
}
func TestAccountETLFaultyStoredHeader(t *testing.T) {
// Load in a valid, minimal header, then delete it, which should cause the ETL of a data line to fail
loadCSVandCheckError(t, validMinimalHeader)
client := spmta.MyRedis()
client.Del(spmta.RedisAcctHeaders)
err := loadCSV("d,[email protected],f00dbeef")
checkExpectedError(t, err, "key acct_headers not found")
// Load in a valid, minimal header ... then overwrite it, which will cause the ETL of a data line to fail
loadCSVandCheckError(t, validMinimalHeader)
client.Set(spmta.RedisAcctHeaders, `{"bananas":1,"type":0}`, 0)
err = loadCSV("d,[email protected],f00dbeef")
checkExpectedError(t, err, "missing field header_x-sp-message-id")
// Corrupt the header so it's not JSON
client.Set(spmta.RedisAcctHeaders, `{gooseberry}`, 0)
err = loadCSV("d,[email protected],f00dbeef")
checkExpectedError(t, err, "invalid character")
}
func TestAccountETLFaultyRedis(t *testing.T) {
hdr := strings.Split(strings.TrimSuffix(validHeaderWithRcpt, "\n"), ",")
client := spmta.MyRedis()
err := client.Close() // Damage the Redis client connection
err = spmta.StoreHeaders(hdr, client)
checkExpectedError(t, err, "closed")
}