-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegration_test.go
75 lines (68 loc) · 1.47 KB
/
integration_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
package receipt
import "testing"
func TestIntegration(t *testing.T) {
PrepareDB(":memory:")
defer db.Close()
if _, err := NewCart(map[string]float64{"illegal-product-id": 1}); err != errorBadProductId {
t.Errorf("expected errorBadProductId, got %s", err)
}
if _, err := NewCart(map[string]float64{"1": 1}); err != errorBadProduct {
t.Errorf("expected errorBadProduct, got %s", err)
}
tables := [][]interface {
}{
{
ProductId(1),
1,
"Product 1",
"kilogram",
},
{
ProductId(2),
2,
"Product 2",
"Piece",
},
}
for _, values := range tables {
db.MustExec(`INSERT INTO product(id, price, name, unitname) values(?, ?, ?, ?)`, values...)
}
cart, err := NewCart(map[string]float64{"1": 1, "2": 7})
if err != nil {
t.Errorf("expected no error, got %s", err)
}
discountData := [...][]interface{}{
{
1,
DiscountPercentage95,
"",
"[1]",
},
{
2,
DiscountBuy2Free1,
"",
"[2]",
},
}
for _, values := range discountData {
db.MustExec(`INSERT INTO discount(id, discounttype, disabled, productids) values(?, ?, ?, ?)`, values...)
}
LoadDiscounts()
if len(Discounts) != len(discountData) {
t.Errorf("LoadDiscounts fail")
}
cart.Checkout()
for _, item := range cart.Items {
switch item.Product.Id {
case ProductId(1):
if item.Paid != 1*1*0.95 {
t.Errorf("discount percentage calculated error")
}
case ProductId(2):
if item.Paid != 2*(7-2) {
t.Errorf("discount free caculated error")
}
}
}
}