This repository has been archived by the owner on Feb 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
coupon_test.go
142 lines (125 loc) · 4.23 KB
/
coupon_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
package stripe
import (
"testing"
)
func init() {
// In order to execute Unit Test, you must set your Stripe API Key as
// environment variable, STRIPE_API_KEY=xxxx
if err := SetKeyEnv(); err != nil {
panic(err)
}
}
// Sample Coupons to use when creating, deleting, updating Coupon data.
var (
// Coupon with only the required fields
c1 = CouponParams{
ID: "test coupon 1",
PercentOff: 5,
Duration: DurationOnce,
}
// Coupon with all required + optional fields.
c2 = CouponParams{
ID: "test coupon 2",
PercentOff: 10,
Duration: DurationRepeating,
MaxRedemptions: 100,
DurationInMonths: 6,
}
)
// TestCreateCoupon will test that we can successfully Create a coupon, parse
// the JSON reponse from Stripe, and that all values are populated as expected.
//
// Second, we will test that error handling works correctly by attempting to
// create a duplicate Coupon, which should thrown an exception.
func TestCreateCoupon(t *testing.T) {
// Create the coupon, and defer its deletion
coupon, err := Coupons.Create(&c1)
defer Coupons.Delete(c1.ID)
if coupon.ID != c1.ID {
t.Errorf("Expected Coupon ID %s, got %s", c1.ID, coupon.ID)
}
if coupon.Duration != c1.Duration {
t.Errorf("Expected Coupon Duration %v, got %v",
c1.Duration, coupon.Duration)
}
if coupon.MaxRedemptions != c1.MaxRedemptions {
t.Errorf("Expected Coupon MaxRedemptions %v, got %v",
c1.MaxRedemptions, coupon.MaxRedemptions)
}
if coupon.PercentOff != c1.PercentOff {
t.Errorf("Expected Coupon PercentOff %v, got %v",
c1.PercentOff, coupon.PercentOff)
}
// Now try to re-create the existing coupon, which should throw an exception
coupon, err = Coupons.Create(&c1)
if err == nil {
t.Error("Expected non-null Error when creating a duplicate coupon.")
} else if err.Error() != "Coupon already exists." {
t.Errorf("Expected %s, got %s", "Coupon already exists.", err.Error())
}
}
// TestRetrieveCoupon will test that we can successfully Retrieve a Coupon,
// parse the JSON response, and that all values are populated as expected.
//
// Second, we will test that error handling works correctly by attempting to
// retrieve a coupon that does not exist. This should yield a Not Found error.
func TestRetrieveCoupon(t *testing.T) {
// create a request that we can retrieve, defer deletion in case test fails
Coupons.Create(&c2)
defer Coupons.Delete(c2.ID)
// now let's retrieve the recently added coupon
coupon, err := Coupons.Get(c2.ID)
if err != nil {
t.Errorf("Expected Coupon %s, got Error %s", c2.ID, err.Error())
}
if coupon.ID != c2.ID {
t.Errorf("Expected Coupon ID %s, got %s", c2.ID, coupon.ID)
}
if coupon.PercentOff != c2.PercentOff {
t.Errorf("Expected Coupon PercentOff %v, got %v",
c2.PercentOff, coupon.PercentOff)
}
if coupon.Duration != c2.Duration {
t.Errorf("Expected Coupon Duration %v, got %v",
c2.Duration, coupon.Duration)
}
// now let's try to retrieve a coupon that doesn't exist, and make sure
// we can handle the error
_, err = Coupons.Get("free for life")
if err == nil {
t.Error("Expected non-null Error when coupon not found.")
}
}
// TestDeleteCoupon will test that we can successfully remove a Coupon, parse
// the JSON reponse, and that the deletion flag is captured as a boolean value.
func TestDeleteCoupon(t *testing.T) {
// create a request that we can delete
Coupons.Create(&c1)
// let's try to delete the coupon
ok, err := Coupons.Delete(c1.ID)
if err != nil {
t.Errorf("Expected Coupon deletion, got Error %s", err.Error())
}
if !ok {
t.Errorf("Expected Coupon deletion true, got false")
}
}
// TestListCoupon will test that we can successfully retrieve a list of Coupons,
// parse the JSON reponse, and that the length of the coupon array matches our
// expectations.
func TestListCoupon(t *testing.T) {
// create 2 dummy coupons that we can retrieve
Coupons.Create(&c1)
Coupons.Create(&c2)
defer Coupons.Delete(c1.ID)
defer Coupons.Delete(c2.ID)
// get the list from Stripe
coupons, _, err := Coupons.List(10, "", "")
if err != nil {
t.Errorf("Expected Coupon List, got Error %s", err.Error())
}
// since we added 2 dummy coupons, we expect the array to be a size of 2
if len(coupons) != 2 {
t.Errorf("Expected 2 Coupons, got %d", len(coupons))
}
}