forked from bold-commerce/go-shopify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
price_rule_test.go
218 lines (178 loc) · 6.2 KB
/
price_rule_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package goshopify
import (
"context"
"fmt"
"testing"
"github.com/jarcoal/httpmock"
)
func TestPriceRuleGet(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/price_rules/1.json", client.pathPrefix),
httpmock.NewBytesResponder(
200,
loadFixture("price_rule/get.json"),
),
)
rules, err := client.PriceRule.Get(context.Background(), 1)
if err != nil {
t.Errorf("PriceRule.Get returned error: %v", err)
}
expected := PriceRule{Id: 1}
if expected.Id != rules.Id {
t.Errorf("PriceRule.Get returned %+v, expected %+v", rules, expected)
}
}
func TestPriceRuleList(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"GET",
fmt.Sprintf("https://fooshop.myshopify.com/%s/price_rules.json", client.pathPrefix),
httpmock.NewBytesResponder(
200,
loadFixture("price_rule/list.json"),
),
)
rules, err := client.PriceRule.List(context.Background())
if err != nil {
t.Errorf("PriceRule.List returned error: %v", err)
}
expected := []PriceRule{{Id: 1}}
if expected[0].Id != rules[0].Id {
t.Errorf("PriceRule.List returned %+v, expected %+v", rules, expected)
}
}
func TestPriceRuleCreate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"POST",
fmt.Sprintf("https://fooshop.myshopify.com/%s/price_rules.json", client.pathPrefix),
httpmock.NewBytesResponder(
200,
loadFixture("price_rule/get.json"),
),
)
rules, err := client.PriceRule.Create(context.Background(), PriceRule{})
if err != nil {
t.Errorf("PriceRule.Create returned error: %v", err)
}
expected := PriceRule{Id: 1}
if expected.Id != rules.Id {
t.Errorf("PriceRule.Create returned %+v, expected %+v", rules, expected)
}
}
func TestPriceRuleUpdate(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"PUT",
fmt.Sprintf("https://fooshop.myshopify.com/%s/price_rules/1.json", client.pathPrefix),
httpmock.NewBytesResponder(
200,
loadFixture("price_rule/get.json"),
),
)
rules, err := client.PriceRule.Update(context.Background(), PriceRule{Id: 1})
if err != nil {
t.Errorf("PriceRule.Update returned error: %v", err)
}
expected := PriceRule{Id: 1}
if expected.Id != rules.Id {
t.Errorf("PriceRule.Update returned %+v, expected %+v", rules, expected)
}
}
func TestPriceRuleDelete(t *testing.T) {
setup()
defer teardown()
httpmock.RegisterResponder(
"DELETE",
fmt.Sprintf("https://fooshop.myshopify.com/%s/price_rules/1.json", client.pathPrefix),
httpmock.NewBytesResponder(
200,
loadFixture("price_rule/get.json"),
),
)
err := client.PriceRule.Delete(context.Background(), 1)
if err != nil {
t.Errorf("PriceRule.Delete returned error: %v", err)
}
}
func TestPriceRuleSetters(t *testing.T) {
pr := PriceRule{}
prereqSubtotalRange := "1.5"
prereqQuantityRange := 2
prereqShippingPrice := "5.5"
prereqRatioQuantity := 1
prereqRatioEntitledQuantity := 1
badMoneyString := "dog"
// Test bad money strings
err := pr.SetPrerequisiteSubtotalRange(&badMoneyString)
if err == nil {
t.Errorf("Expected error from setting bad string as prerequisite subtotal range: %s", badMoneyString)
}
err = pr.SetPrerequisiteShippingPriceRange(&badMoneyString)
if err == nil {
t.Errorf("Expected error from setting bad string as prerequisite shipping price: %s", badMoneyString)
}
// Test populating values
err = pr.SetPrerequisiteSubtotalRange(&prereqSubtotalRange)
if err != nil {
t.Errorf("Failed to set prerequisite subtotal range: %s", prereqSubtotalRange)
}
pr.SetPrerequisiteQuantityRange(&prereqQuantityRange)
err = pr.SetPrerequisiteShippingPriceRange(&prereqShippingPrice)
if err != nil {
t.Errorf("Failed to set prerequisite shipping price: %s", prereqSubtotalRange)
}
pr.SetPrerequisiteToEntitlementQuantityRatio(&prereqRatioQuantity, &prereqRatioEntitledQuantity)
if pr.PrerequisiteSubtotalRange.GreaterThanOrEqualTo != prereqSubtotalRange {
t.Errorf("Failed to set prerequisite subtotal range: %s", prereqSubtotalRange)
}
if pr.PrerequisiteQuantityRange.GreaterThanOrEqualTo != prereqQuantityRange {
t.Errorf("Failed to set prerequisite quantity range: %d", prereqQuantityRange)
}
if pr.PrerequisiteShippingPriceRange.LessThanOrEqualTo != prereqShippingPrice {
t.Errorf("Failed to set prerequisite shipping price: %s", prereqShippingPrice)
}
if pr.PrerequisiteToEntitlementQuantityRatio.PrerequisiteQuantity != prereqRatioQuantity {
t.Errorf("Failed to set prerequisite ratio quantity: %d", prereqRatioQuantity)
}
if pr.PrerequisiteToEntitlementQuantityRatio.EntitledQuantity != prereqRatioEntitledQuantity {
t.Errorf("Failed to set prerequisite ratio entitled quantity: %d", prereqRatioEntitledQuantity)
}
// Test clearing values by setting nil
err = pr.SetPrerequisiteSubtotalRange(nil)
if err != nil {
t.Errorf("Failed to set prerequisite subtotal range: %s", prereqSubtotalRange)
}
pr.SetPrerequisiteQuantityRange(nil)
err = pr.SetPrerequisiteShippingPriceRange(nil)
if err != nil {
t.Errorf("Failed to set prerequisite shipping price: %s", prereqSubtotalRange)
}
if pr.PrerequisiteSubtotalRange != nil {
t.Errorf("Failed to clear prerequisite subtotal range")
}
if pr.PrerequisiteQuantityRange != nil {
t.Errorf("Failed to clear prerequisite quantity range")
}
if pr.PrerequisiteShippingPriceRange != nil {
t.Errorf("Failed to clear prerequisite shipping price")
}
pr.SetPrerequisiteToEntitlementQuantityRatio(nil, &prereqRatioEntitledQuantity)
if pr.PrerequisiteToEntitlementQuantityRatio.PrerequisiteQuantity != 0 || pr.PrerequisiteToEntitlementQuantityRatio.EntitledQuantity != prereqRatioEntitledQuantity {
t.Errorf("Failed to clear prerequisite-to-entitlement-quantity-ratio prerequisite quantity")
}
pr.SetPrerequisiteToEntitlementQuantityRatio(&prereqRatioQuantity, nil)
if pr.PrerequisiteToEntitlementQuantityRatio.EntitledQuantity != 0 || pr.PrerequisiteToEntitlementQuantityRatio.PrerequisiteQuantity != prereqRatioQuantity {
t.Errorf("Failed to clear prerequisite-to-entitlement-quantity-ratio entitled quantity")
}
pr.SetPrerequisiteToEntitlementQuantityRatio(nil, nil)
if pr.PrerequisiteToEntitlementQuantityRatio != nil {
t.Errorf("Failed to clear wholly prerequisite to entitlement quantity ratio")
}
}