-
Notifications
You must be signed in to change notification settings - Fork 3
/
chartmogul_test.go
111 lines (102 loc) · 2.73 KB
/
chartmogul_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
package chartmogul
import (
"flag"
"os"
"testing"
)
var (
cm = flag.Bool("cm", false, "run integration library tests against ChartMogul")
api_key = flag.String("api_key", "", "API key for CM test")
api = API{}
)
func TestMain(m *testing.M) {
flag.Parse()
if *api_key == "" {
if *cm {
panic("Please supply testing API key on cmd line to run live tests.")
}
*cm = false
} else {
api.ApiKey = *api_key
}
result := m.Run()
os.Exit(result)
}
func TestPing(t *testing.T) {
if !*cm {
t.SkipNow()
return
}
b, err := api.Ping()
if err != nil {
t.Error(err)
} else if !b {
t.Error("ping returned false")
}
}
var matchersCases = map[string]struct {
Errors
expectations map[string]bool
}{
"invoice & transaction exist": {
Errors{
"transactions.external_id": "has already been taken",
"external_id": "The external ID for this invoice already exists in our system.",
}, map[string]bool{
"IsAlreadyExists": false,
"IsInvoiceAndTransactionAlreadyExist": true,
"IsInvoiceAndItsEntitiesAlreadyExist": true,
},
},
"invoice exists": {
Errors{
"external_id": "The external ID for this invoice already exists in our system.",
}, map[string]bool{
"IsAlreadyExists": true,
"IsInvoiceAndTransactionAlreadyExist": false,
"IsInvoiceAndItsEntitiesAlreadyExist": true,
},
},
"invoice, line items and transactions exist": {
Errors{
"external_id": "The external ID for this invoice already exists in our system.",
"transactions.external_id": "has already been taken",
"line_items.external_id": "The external ID for this line item already exists in our system.",
}, map[string]bool{
"IsAlreadyExists": false,
"IsInvoiceAndTransactionAlreadyExist": false,
"IsInvoiceAndItsEntitiesAlreadyExist": true,
},
},
"transaction exists": {
Errors{
"transactions.external_id": "has already been taken",
}, map[string]bool{
"IsAlreadyExists": true,
"IsInvoiceAndTransactionAlreadyExist": false,
"IsInvoiceAndItsEntitiesAlreadyExist": false,
},
},
}
func TestErrorMatchers(t *testing.T) {
for testName, testCase := range matchersCases {
for fnName, expected := range testCase.expectations {
t := t
expected := expected
var fn func() bool
switch fnName {
case "IsAlreadyExists":
fn = testCase.Errors.IsAlreadyExists
case "IsInvoiceAndTransactionAlreadyExist":
fn = testCase.Errors.IsInvoiceAndTransactionAlreadyExist
case "IsInvoiceAndItsEntitiesAlreadyExist":
fn = testCase.Errors.IsInvoiceAndItsEntitiesAlreadyExist
}
t.Run(testName+"/"+fnName, func(t *testing.T) {
if fn() != expected {
t.Error("unexpected match")
}
})
}
}
}