-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathsolution-template_test.go
More file actions
390 lines (331 loc) · 8.39 KB
/
Copy pathsolution-template_test.go
File metadata and controls
390 lines (331 loc) · 8.39 KB
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
package main
import (
"database/sql"
"os"
"testing"
)
const testDBPath = "test_inventory.db"
func setupTestDB(t *testing.T) *sql.DB {
// Remove any existing test database
os.Remove(testDBPath)
// Initialize a new test database
db, err := InitDB(testDBPath)
if err != nil {
t.Fatalf("Failed to initialize test database: %v", err)
}
return db
}
func cleanupTestDB() {
os.Remove(testDBPath)
}
func TestCreateProduct(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
testCases := []struct {
name string
product Product
}{
{
name: "Create Basic Product",
product: Product{
Name: "Test Product",
Price: 9.99,
Quantity: 100,
Category: "Test",
},
},
{
name: "Create Zero Price Product",
product: Product{
Name: "Free Product",
Price: 0,
Quantity: 10,
Category: "Free",
},
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
product := tc.product
err := store.CreateProduct(&product)
if err != nil {
t.Fatalf("Failed to create product: %v", err)
}
if product.ID <= 0 {
t.Errorf("Expected product ID to be set after creation, got %d", product.ID)
}
// Verify product was created by retrieving it
retrieved, err := store.GetProduct(product.ID)
if err != nil {
t.Fatalf("Failed to retrieve created product: %v", err)
}
if retrieved.Name != product.Name {
t.Errorf("Expected name %s, got %s", product.Name, retrieved.Name)
}
if retrieved.Price != product.Price {
t.Errorf("Expected price %f, got %f", product.Price, retrieved.Price)
}
})
}
}
func TestGetProduct(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create a product to retrieve
product := &Product{
Name: "Test Product",
Price: 9.99,
Quantity: 100,
Category: "Test",
}
err := store.CreateProduct(product)
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
testCases := []struct {
name string
id int64
expectError bool
}{
{
name: "Get Existing Product",
id: product.ID,
expectError: false,
},
{
name: "Get Non-Existent Product",
id: product.ID + 1000, // ID that doesn't exist
expectError: true,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
retrieved, err := store.GetProduct(tc.id)
if tc.expectError {
if err == nil {
t.Errorf("Expected error retrieving non-existent product, got nil")
}
} else {
if err != nil {
t.Fatalf("Failed to retrieve product: %v", err)
}
if retrieved.ID != product.ID {
t.Errorf("Expected ID %d, got %d", product.ID, retrieved.ID)
}
if retrieved.Name != product.Name {
t.Errorf("Expected name %s, got %s", product.Name, retrieved.Name)
}
}
})
}
}
func TestUpdateProduct(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create a product to update
product := &Product{
Name: "Original Name",
Price: 9.99,
Quantity: 100,
Category: "Test",
}
err := store.CreateProduct(product)
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
// Update the product
product.Name = "Updated Name"
product.Price = 19.99
product.Quantity = 50
err = store.UpdateProduct(product)
if err != nil {
t.Fatalf("Failed to update product: %v", err)
}
// Verify the update
updated, err := store.GetProduct(product.ID)
if err != nil {
t.Fatalf("Failed to retrieve updated product: %v", err)
}
if updated.Name != "Updated Name" {
t.Errorf("Update failed: expected name 'Updated Name', got '%s'", updated.Name)
}
if updated.Price != 19.99 {
t.Errorf("Update failed: expected price 19.99, got %f", updated.Price)
}
if updated.Quantity != 50 {
t.Errorf("Update failed: expected quantity 50, got %d", updated.Quantity)
}
}
func TestDeleteProduct(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create a product to delete
product := &Product{
Name: "To Be Deleted",
Price: 9.99,
Quantity: 100,
Category: "Test",
}
err := store.CreateProduct(product)
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
// Delete the product
err = store.DeleteProduct(product.ID)
if err != nil {
t.Fatalf("Failed to delete product: %v", err)
}
// Verify the deletion
_, err = store.GetProduct(product.ID)
if err == nil {
t.Errorf("Expected error when retrieving deleted product, got nil")
}
}
func TestListProducts(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create products with different categories
productsToCreate := []Product{
{Name: "Product 1", Price: 9.99, Quantity: 10, Category: "Electronics"},
{Name: "Product 2", Price: 19.99, Quantity: 20, Category: "Electronics"},
{Name: "Product 3", Price: 29.99, Quantity: 30, Category: "Books"},
{Name: "Product 4", Price: 39.99, Quantity: 40, Category: "Clothing"},
}
for i := range productsToCreate {
err := store.CreateProduct(&productsToCreate[i])
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
}
testCases := []struct {
name string
category string
expectedSize int
}{
{
name: "List All Products",
category: "",
expectedSize: 4,
},
{
name: "List Electronics Products",
category: "Electronics",
expectedSize: 2,
},
{
name: "List Books Products",
category: "Books",
expectedSize: 1,
},
{
name: "List Non-Existent Category",
category: "NonExistent",
expectedSize: 0,
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
products, err := store.ListProducts(tc.category)
if err != nil {
t.Fatalf("Failed to list products: %v", err)
}
if len(products) != tc.expectedSize {
t.Errorf("Expected %d products, got %d", tc.expectedSize, len(products))
}
if tc.category != "" {
for _, p := range products {
if p.Category != tc.category {
t.Errorf("Expected category %s, got %s", tc.category, p.Category)
}
}
}
})
}
}
func TestBatchUpdateInventory(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create products
products := []Product{
{Name: "Product 1", Price: 9.99, Quantity: 10, Category: "Test"},
{Name: "Product 2", Price: 19.99, Quantity: 20, Category: "Test"},
}
for i := range products {
err := store.CreateProduct(&products[i])
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
}
// Prepare batch updates
updates := map[int64]int{
products[0].ID: 5, // Reduce by 5
products[1].ID: 15, // Reduce by 5
}
// Perform batch update
err := store.BatchUpdateInventory(updates)
if err != nil {
t.Fatalf("Failed to perform batch update: %v", err)
}
// Verify updates
p1, err := store.GetProduct(products[0].ID)
if err != nil {
t.Fatalf("Failed to retrieve product 1: %v", err)
}
if p1.Quantity != 5 {
t.Errorf("Expected quantity 5 for product 1, got %d", p1.Quantity)
}
p2, err := store.GetProduct(products[1].ID)
if err != nil {
t.Fatalf("Failed to retrieve product 2: %v", err)
}
if p2.Quantity != 15 {
t.Errorf("Expected quantity 15 for product 2, got %d", p2.Quantity)
}
}
func TestBatchUpdateInventoryRollback(t *testing.T) {
db := setupTestDB(t)
defer db.Close()
defer cleanupTestDB()
store := NewProductStore(db)
// Create one product
product := &Product{
Name: "Product 1",
Price: 9.99,
Quantity: 10,
Category: "Test",
}
err := store.CreateProduct(product)
if err != nil {
t.Fatalf("Failed to create test product: %v", err)
}
// Try to update with a non-existent product ID
updates := map[int64]int{
product.ID: 5,
product.ID + 99: 15, // This product doesn't exist
}
// This should fail and roll back
err = store.BatchUpdateInventory(updates)
if err == nil {
t.Fatalf("Expected error for non-existent product, got nil")
}
// Verify the first product was not updated (rollback worked)
p1, err := store.GetProduct(product.ID)
if err != nil {
t.Fatalf("Failed to retrieve product: %v", err)
}
if p1.Quantity != 10 {
t.Errorf("Expected quantity to remain 10 after rollback, got %d", p1.Quantity)
}
}