-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
334 lines (294 loc) · 9.31 KB
/
main_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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package menubotlib_test
import (
"database/sql"
"encoding/json"
"reflect"
"testing"
mb "github.com/JeremyJalpha/MenuBotLib"
"github.com/stretchr/testify/assert"
_ "modernc.org/sqlite"
)
func Test_ParseUpdateOrderCommand(t *testing.T) {
tests := []struct {
commandText string
expected []mb.MenuIndication
expectError bool
}{
{
commandText: "update order 6:0",
expected: []mb.MenuIndication{
{ItemMenuNum: 6, ItemAmount: "0"},
},
expectError: false,
},
{
commandText: "update order 9:12, 10: 1x3, 3x2, 2x1, 6:5",
expected: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "12"},
{ItemMenuNum: 6, ItemAmount: "5"},
},
expectError: false,
},
}
for _, test := range tests {
result, err := mb.ParseUpdateOrderCommand(test.commandText)
if (err != nil) != test.expectError {
t.Errorf("ParseUpdateOrderCommand(%q) error = %v, expectError %v", test.commandText, err, test.expectError)
continue
}
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("ParseUpdateOrderCommand(%q) = %v, want %v", test.commandText, result, test.expected)
}
}
}
// baseline: "Update order 9:12, 10: 1x3, 3x2, 2x1, 6: 5"
func Test_UpdateCustOrdItems(t *testing.T) {
var err error
tests := []struct {
given mb.CustomerOrder
update mb.OrderItems
expected mb.OrderItems
expectError bool
}{
{
given: mb.CustomerOrder{
OrderItems: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3"},
{ItemMenuNum: 9, ItemAmount: "5"},
{ItemMenuNum: 8, ItemAmount: "6"},
{ItemMenuNum: 6, ItemAmount: "5"},
{ItemMenuNum: 5, ItemAmount: "9"},
},
},
},
update: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 6, ItemAmount: "0"},
{ItemMenuNum: 7, ItemAmount: "7"},
},
},
expected: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "5"},
{ItemMenuNum: 8, ItemAmount: "6"},
{ItemMenuNum: 5, ItemAmount: "9"},
{ItemMenuNum: 7, ItemAmount: "7"},
},
},
expectError: false,
},
}
for _, test := range tests {
err = test.given.UpdateCustOrdItems(test.update)
assert.NoError(t, err)
if (err != nil) != test.expectError {
t.Errorf("UpdateCustOrdItems(%q) error = %v, expectError %v", test.update, err, test.expectError)
continue
}
if !reflect.DeepEqual(test.given.OrderItems, test.expected) {
t.Errorf("UpdateCustOrdItems(%q) = %v, want %v", test.update, test.given.OrderItems, test.expected)
}
}
}
func Test_NewOrder_UpdateOrInsertCurrentOrder(t *testing.T) {
db, err := setupTestDBInstance()
assert.NoError(t, err)
defer db.Close()
_, err = db.Exec(crtCustomerOrderTbl)
assert.NoError(t, err)
tests := []struct {
custOrd mb.CustomerOrder
expected mb.OrderItems
expectError bool
}{
{
custOrd: mb.CustomerOrder{
OrderID: 12345,
CellNumber: "0766140000",
CatalogueID: catalogueID,
OrderItems: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "12"},
{ItemMenuNum: 6, ItemAmount: "5"},
},
},
},
expected: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "12"},
{ItemMenuNum: 6, ItemAmount: "5"},
},
},
expectError: false,
},
}
for _, test := range tests {
err = test.custOrd.UpdateOrInsertCurrentOrder(db, test.custOrd.CellNumber, test.expected, true)
assert.NoError(t, err)
var readOrderItems string
query := `SELECT orderitems FROM customerorder WHERE orderID = ?`
err = db.QueryRow(query, test.custOrd.OrderID).Scan(&readOrderItems)
assert.NoError(t, err)
// Unmarshal the JSON string back to []OrderItem
var actual mb.OrderItems
err = json.Unmarshal([]byte(readOrderItems), &actual)
assert.NoError(t, err)
if (err != nil) != test.expectError {
t.Errorf("UpdateOrInsertCurrentOrder(%q) error = %v, expectError %v", test.custOrd.OrderItems, err, test.expectError)
continue
}
result := mb.OrderItems{MenuIndications: actual.MenuIndications}
if !reflect.DeepEqual(result, test.expected) {
t.Errorf("UpdateOrInsertCurrentOrder(%q) = %v, want %v", test.custOrd.OrderItems, result, test.expected)
}
}
}
func Test_CheckoutNow(t *testing.T) {
db, err := setupTestDBInstance()
assert.NoError(t, err)
defer db.Close()
senderNum := "0000000000"
pymntRtrnBase := "payment_return"
pymntCnclBase := "payment_canceled"
returnBaseURL := "/" + pymntRtrnBase
cancelBaseURL := "/" + pymntCnclBase
notifyBaseURL := "/payment_notify"
ItemNamePrefix := "Order"
HomebaseURL := "https://yourhomedomain.com"
MerchantId := "10000100"
MerchantKey := "*************"
Passphrase := "*************"
PfHost := "https://sandbox.payfast.co.za/eng/process"
checkoutInfo := mb.CheckoutInfo{
ReturnURL: HomebaseURL + returnBaseURL,
CancelURL: HomebaseURL + cancelBaseURL,
NotifyURL: HomebaseURL + notifyBaseURL,
MerchantId: MerchantId,
MerchantKey: MerchantKey,
Passphrase: Passphrase,
HostURL: PfHost,
ItemNamePrefix: ItemNamePrefix,
}
tests := []struct {
ctlgSelections []mb.CatalogueSelection
userInfo mb.UserInfo
custOrd mb.CustomerOrder
expected mb.OrderItems
expectError bool
}{
{
//Remember to change the static definitions to match your pricelist defenition.
ctlgSelections: selections,
userInfo: mb.UserInfo{
CellNumber: senderNum,
NickName: mb.NullString{NullString: sql.NullString{String: "testSplurge", Valid: true}},
Email: mb.NullString{NullString: sql.NullString{String: "sbtu01@payfast.io", Valid: true}},
},
custOrd: mb.CustomerOrder{
OrderID: 12345,
CellNumber: senderNum,
CatalogueID: catalogueID,
OrderItems: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "12"},
{ItemMenuNum: 6, ItemAmount: "5"},
},
},
},
expected: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 10, ItemAmount: "1x3, 3x2, 2x1"},
{ItemMenuNum: 9, ItemAmount: "12"},
{ItemMenuNum: 6, ItemAmount: "5"},
},
},
expectError: false,
},
}
for _, test := range tests {
mb.BeginCheckout(db, test.userInfo, test.ctlgSelections, test.custOrd, checkoutInfo, true)
assert.NoError(t, err)
// ...
// if (err != nil) != test.expectError {
// t.Errorf("UpdateOrInsertCurrentOrder(%q) error = %v, expectError %v", test.custOrd.OrderItems, err, test.expectError)
// continue
// }
}
}
func extractItemsFromSelections(selections []mb.CatalogueSelection) []mb.CatalogueItem {
var items []mb.CatalogueItem
for _, selection := range selections {
items = append(items, selection.Items...)
}
return items
}
func Test_PrintPriceList(t *testing.T) {
db, err := setupTestDBInstance()
assert.NoError(t, err)
defer db.Close()
_, err = db.Exec(crtCatalogueItemTbl)
assert.NoError(t, err)
err = mb.InsertCatalogueItems(db, selections)
assert.NoError(t, err)
expectedItems := extractItemsFromSelections(selections)
ctlgItms, err := mb.GetCatalogueItemsFromDB(db, catalogueID)
assert.NoError(t, err)
if !reflect.DeepEqual(ctlgItms, expectedItems) {
t.Errorf("getCatalogueItemsFromDB(db, %s...) = %v, want %v", catalogueID, ctlgItms, expectedItems)
}
}
func Test_ComposedCatalogueSelections(t *testing.T) {
db, err := setupTestDBInstance()
assert.NoError(t, err)
defer db.Close()
_, err = db.Exec(crtCatalogueItemTbl)
assert.NoError(t, err)
err = mb.InsertCatalogueItems(db, selections)
assert.NoError(t, err)
ctlgItms, err := mb.GetCatalogueItemsFromDB(db, catalogueID)
assert.NoError(t, err)
convertedctlgItms := mb.CmpsCtlgSlctnsFromCtlgItms(ctlgItms)
if !reflect.DeepEqual(convertedctlgItms, selections) {
t.Errorf("getCatalogueItemsFromDB(db, %s...) = %v, want %v", catalogueID, ctlgItms, selections)
}
}
func Test_CalculatePriceWithNoOptions(t *testing.T) {
// Static Menu definition should say:
// Denitrified fertilizer:
// - "5g @ R110 p.g.",
// - "10g @ R90 p.g.",
// Therefore Expected Total: 12 * 90 = 1080
tests := []struct {
ordItems mb.OrderItems
expctdTotal int
expctdSummary string
expctError bool
}{
{
ordItems: mb.OrderItems{
MenuIndications: []mb.MenuIndication{
{ItemMenuNum: 1, ItemAmount: "12"},
},
},
expctdTotal: 1080,
expctdSummary: "",
expctError: false,
},
}
for _, test := range tests {
total, summary := test.ordItems.CalculatePrice(selections)
if summary != test.expctdSummary {
t.Errorf("CalculatePrice(selections).summary = %v, want %v", summary, test.expctdSummary)
}
if total != test.expctdTotal {
t.Errorf("CalculatePrice(selections).total = %v, want %v", total, test.expctdTotal)
}
}
}