-
Notifications
You must be signed in to change notification settings - Fork 0
/
gross_store_test.go
255 lines (233 loc) · 5.22 KB
/
gross_store_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
package gross
import (
"testing"
)
type entry struct {
name string
unit string
qty int
}
func TestUnits(t *testing.T) {
tests := []struct {
name string
qty int
}{
{"quarter_of_a_dozen", 3},
{"half_of_a_dozen", 6},
{"dozen", 12},
{"small_gross", 120},
{"gross", 144},
{"great_gross", 1728},
}
units := Units()
for _, tt := range tests {
qty, ok := units[tt.name]
if !ok {
t.Errorf(`Unit "%s" not found!`, tt.name)
continue
}
if qty != tt.qty {
t.Errorf(`Unit "%s" should have quantity %d, found %d`, tt.name, tt.qty, qty)
}
}
}
func TestAddItem(t *testing.T) {
tests := []struct {
name string
entry []entry
expected bool
}{
{
"Invalid measurement unit",
[]entry{
{"pasta", "", 0},
{"onion", "quarter", 0},
{"pasta", "pound", 0},
},
false,
},
{
"Valid measurement unit",
[]entry{
{"peas", "quarter_of_a_dozen", 3},
{"tomato", "half_of_a_dozen", 6},
{"chili", "dozen", 12},
{"cucumber", "small_gross", 120},
{"potato", "gross", 144},
{"zucchini", "great_gross", 1728},
},
true,
},
{
"check quantity of item added twice",
[]entry{
{"peas", "quarter_of_a_dozen", 3},
{"peas", "quarter_of_a_dozen", 6},
{"tomato", "half_of_a_dozen", 6},
{"tomato", "quarter_of_a_dozen", 9},
},
true,
},
}
units := Units()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bill := NewBill()
for _, item := range tt.entry {
ok := AddItem(bill, units, item.name, item.unit)
if ok != tt.expected {
t.Errorf("Expected %t from AddItem, found %t at %v", tt.expected, ok, item.name)
}
itemQty, ok := bill[item.name]
if ok != tt.expected {
t.Errorf("Could not find item %s in customer bill", item.name)
}
if itemQty != item.qty {
t.Errorf("Expected %s to have quantity %d in customer bill, found %d", item.name, item.qty, itemQty)
}
}
})
}
}
func TestRemoveItem(t *testing.T) {
type expectedItem struct {
name string
unit string
qty int
exists bool
}
tests := []struct {
name string
remove []expectedItem
expected bool
}{
{"Item Not found in bill",
[]expectedItem{
{"papaya", "gross", 0, false},
},
false,
},
{"Invalid measurement unit",
[]expectedItem{
{"peas", "pound", 3, true},
{"tomato", "kilogram", 6, true},
{"cucumber", "stone", 120, true},
},
false,
},
{"Resulted qty less than 0",
[]expectedItem{
{"peas", "half_of_a_dozen", 3, true},
{"tomato", "dozen", 6, true},
{"chili", "small_gross", 12, true},
{"cucumber", "gross", 120, true},
{"potato", "great_gross", 144, true},
},
false,
},
{"Should delete the item if 0",
[]expectedItem{
{"peas", "quarter_of_a_dozen", 0, false},
{"tomato", "half_of_a_dozen", 0, false},
{"chili", "dozen", 0, false},
{"cucumber", "small_gross", 0, false},
{"potato", "gross", 0, false},
{"zucchini", "great_gross", 0, false},
},
true,
},
{"Should reduce the qty",
[]expectedItem{
{"chili", "half_of_a_dozen", 6, true},
{"cucumber", "dozen", 108, true},
{"zucchini", "gross", 1584, true},
},
true,
},
}
units := Units()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
bill := setupInitialBillData()
for _, item := range tt.remove {
ok := RemoveItem(bill, units, item.name, item.unit)
if ok != tt.expected {
t.Errorf("Expected %t from RemoveItem, found %t at %v", tt.expected, ok, item.name)
}
itemQty, ok := bill[item.name]
if ok != item.exists {
t.Errorf("Could not find item %s in customer bill", item.name)
}
if itemQty != item.qty {
t.Errorf("Expected %s to have quantity %d in customer bill, found %d", item.name, item.qty, itemQty)
}
}
})
}
}
func TestNewBill(t *testing.T) {
// Success, zero out the bill
t.Run("Should reset customerbill", func(t *testing.T) {
bill := NewBill()
if len(bill) != 0 {
t.Error("Customer bill must be empty")
}
})
}
func TestGetItem(t *testing.T) {
type expectedItem struct {
name string
expected bool
qty int
}
test := []struct {
name string
getItem []expectedItem
}{
{
"Item Not found in bill",
[]expectedItem{
{"grape", false, 0},
},
},
{
"Success",
[]expectedItem{
{"peas", true, 3},
{"tomato", true, 6},
{"chili", true, 12},
{"cucumber", true, 120},
{"potato", true, 144},
{"zucchini", true, 1728},
},
},
}
for _, tt := range test {
t.Run(tt.name, func(t *testing.T) {
bill := setupInitialBillData()
for _, item := range tt.getItem {
itemQty, ok := GetItem(bill, item.name)
if ok != item.expected {
msg := "Could not find item %s in customer bill, expected %t"
if item.expected == false {
msg = "Found item %s in customer bill, expected %t"
}
t.Errorf(msg, item.name, item.expected)
}
if itemQty != item.qty {
t.Errorf("Expected %s to have quantity %d in customer bill, found %d", item.name, item.qty, itemQty)
}
}
})
}
}
func setupInitialBillData() map[string]int {
bill := NewBill()
bill["peas"] = 3
bill["tomato"] = 6
bill["chili"] = 12
bill["cucumber"] = 120
bill["potato"] = 144
bill["zucchini"] = 1728
return bill
}