forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
usagecharge_test.go
135 lines (110 loc) · 3.43 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
package goshopify
import (
"reflect"
"testing"
"time"
"github.com/shopspring/decimal"
"gopkg.in/jarcoal/httpmock.v1"
)
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 !reflect.DeepEqual(usageCharge, expected) {
t.Errorf("UsageCharge.Create returned %+v, expected %+v", usageCharge, expected)
}
}
func TestUsageChargeServiceOp_Create(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
"https://fooshop.myshopify.com/admin/recurring_application_charges/455696195/usage_charges.json",
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",
"https://fooshop.myshopify.com/admin/recurring_application_charges/455696195/usage_charges/1034618210.json",
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",
"https://fooshop.myshopify.com/admin/recurring_application_charges/455696195/usage_charges.json",
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",
"https://fooshop.myshopify.com/admin/recurring_application_charges/455696195/usage_charges/1034618210.json",
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",
"https://fooshop.myshopify.com/admin/recurring_application_charges/455696195/usage_charges/1034618210.json",
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")
}
}