forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usagecharge_test.go
156 lines (131 loc) · 4.7 KB
/
usagecharge_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
package goshopify
import (
"fmt"
"testing"
"time"
"github.com/jarcoal/httpmock"
"github.com/shopspring/decimal"
)
func usageChargeTests(t *testing.T, usageCharge UsageCharge) {
price := decimal.NewFromFloat(1.0)
createdAt, _ := time.Parse(time.RFC3339, "2018-07-05T13:05:43-04:00")
billingOn, _ := time.Parse("2006-01-02", "2018-08-04")
balanceUsed := decimal.NewFromFloat(11.0)
balancedRemaining := decimal.NewFromFloat(89.0)
riskLevel := decimal.NewFromFloat(0.08)
expected := UsageCharge{
ID: 1034618208,
Description: "Super Mega Plan 1000 emails",
Price: &price,
CreatedAt: &createdAt,
BillingOn: &billingOn,
BalanceRemaining: &balancedRemaining,
BalanceUsed: &balanceUsed,
RiskLevel: &riskLevel,
}
if usageCharge.ID != expected.ID {
t.Errorf("UsageCharge.ID returned %v, expected %v", usageCharge.ID, expected.ID)
}
if usageCharge.Description != expected.Description {
t.Errorf("UsageCharge.Description returned %v, expected %v", usageCharge.Description, expected.Description)
}
if !usageCharge.Price.Equal(*expected.Price) {
t.Errorf("UsageCharge.Price returned %v, expected %v", usageCharge.Price, expected.Price)
}
if !usageCharge.CreatedAt.Equal(*expected.CreatedAt) {
t.Errorf("UsageCharge.CreatedAt returned %v, expected %v", usageCharge.CreatedAt, expected.CreatedAt)
}
if !usageCharge.BillingOn.Equal(*expected.BillingOn) {
t.Errorf("UsageCharge.BillingOn returned %v, expected %v", usageCharge.BillingOn, expected.BillingOn)
}
if !usageCharge.BalanceRemaining.Equal(*expected.BalanceRemaining) {
t.Errorf("UsageCharge.BalanceRemaining returned %v, expected %v", usageCharge.BalanceRemaining, expected.BalanceRemaining)
}
if !usageCharge.BalanceUsed.Equal(*expected.BalanceUsed) {
t.Errorf("UsageCharge.BalanceUsed returned %v, expected %v", usageCharge.BalanceUsed, expected.BalanceUsed)
}
if !usageCharge.RiskLevel.Equal(*expected.RiskLevel) {
t.Errorf("UsageCharge.RiskLevel returned %v, expected %v", usageCharge.RiskLevel, expected.RiskLevel)
}
}
func TestUsageChargeServiceOp_Create(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/recurring_application_charges/455696195/usage_charges.json", client.pathPrefix),
httpmock.NewBytesResponder(
200, loadFixture("usagecharge.json"),
),
)
p := decimal.NewFromFloat(1.0)
charge := UsageCharge{
Description: "Super Mega Plan 1000 emails",
Price: &p,
}
returnedCharge, err := client.UsageCharge.Create(455696195, charge)
if err != nil {
t.Errorf("UsageCharge.Create returned an error: %v", err)
}
usageChargeTests(t, *returnedCharge)
}
func TestUsageChargeServiceOp_Get(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/recurring_application_charges/455696195/usage_charges/1034618210.json", client.pathPrefix),
httpmock.NewBytesResponder(
200, loadFixture("usagecharge.json"),
),
)
charge, err := client.UsageCharge.Get(455696195, 1034618210, nil)
if err != nil {
t.Errorf("UsageCharge.Get returned an error: %v", err)
}
usageChargeTests(t, *charge)
}
func TestUsageChargeServiceOp_List(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/recurring_application_charges/455696195/usage_charges.json", client.pathPrefix),
httpmock.NewBytesResponder(
200, loadFixture("usagecharges.json"),
),
)
charges, err := client.UsageCharge.List(455696195, nil)
if err != nil {
t.Errorf("UsageCharge.List returned an error: %v", err)
}
// Check that usage charges were parsed
if len(charges) != 1 {
t.Errorf("UsageCharage.List got %v usage charges, expected: 1", len(charges))
}
usageChargeTests(t, charges[0])
}
func TestUsageChargeServiceOp_GetBadFields(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/recurring_application_charges/455696195/usage_charges/1034618210.json", client.pathPrefix),
httpmock.NewStringResponder(
200, `{"usage_charge":{"id":"wrong_id_type"}}`,
),
)
if _, err := client.UsageCharge.Get(455696195, 1034618210, nil); err == nil {
t.Errorf("UsageCharge.Get should have returned an error")
}
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/recurring_application_charges/455696195/usage_charges/1034618210.json", client.pathPrefix),
httpmock.NewStringResponder(
200, `{"usage_charge":{"billing_on":"2018-14-01"}}`,
),
)
if _, err := client.UsageCharge.Get(455696195, 1034618210, nil); err == nil {
t.Errorf("UsageCharge.Get should have returned an error")
}
}