| | | 1 | | #include "../../../database/inventory_service.h" |
| | | 2 | | #include <stdio.h> |
| | | 3 | | #include <assert.h> |
| | | 4 | | #include <string.h> |
| | | 5 | | |
| | 1 | 6 | | void test_inventory_struct_size() { |
| | | 7 | | InventoryItem item; |
| | | 8 | | assert(sizeof(item.part_name) == 100); |
| | | 9 | | assert(sizeof(item.sku) == 50); |
| | | 10 | | assert(sizeof(item.last_restocked_at) == 32); |
| | 1 | 11 | | printf("[PASS] InventoryItem struct: correct field sizes\n"); |
| | 1 | 12 | | } |
| | | 13 | | |
| | 1 | 14 | | void test_inventory_initialization() { |
| | 1 | 15 | | InventoryItem item = {0}; |
| | 1 | 16 | | item.id = 1; |
| | 1 | 17 | | strcpy(item.part_name, "Bearing 608"); |
| | 1 | 18 | | strcpy(item.sku, "BRG-608-2RS"); |
| | 1 | 19 | | item.quantity = 50; |
| | 1 | 20 | | item.min_stock_level = 10; |
| | 1 | 21 | | item.unit_cost = 2.50; |
| | 1 | 22 | | assert(item.id == 1); |
| | 1 | 23 | | assert(strcmp(item.part_name, "Bearing 608") == 0); |
| | 1 | 24 | | assert(strcmp(item.sku, "BRG-608-2RS") == 0); |
| | 1 | 25 | | assert(item.quantity == 50); |
| | 1 | 26 | | assert(item.min_stock_level == 10); |
| | 1 | 27 | | assert(item.unit_cost == 2.50); |
| | 1 | 28 | | printf("[PASS] InventoryItem initialization: all fields correct\n"); |
| | 1 | 29 | | } |
| | | 30 | | |
| | 1 | 31 | | void test_inventory_low_stock_detection() { |
| | 1 | 32 | | InventoryItem item1 = {.quantity = 5, .min_stock_level = 10}; |
| | 1 | 33 | | InventoryItem item2 = {.quantity = 15, .min_stock_level = 10}; |
| | 1 | 34 | | assert(item1.quantity < item1.min_stock_level); |
| | 1 | 35 | | assert(item2.quantity >= item2.min_stock_level); |
| | 1 | 36 | | printf("[PASS] Inventory: low stock detection logic\n"); |
| | 1 | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | void test_inventory_cost_calculation() { |
| | 1 | 40 | | InventoryItem item = {.quantity = 100, .unit_cost = 5.75}; |
| | 1 | 41 | | double total_value = item.quantity *item.unit_cost; |
| | 1 | 42 | | assert(total_value == 575.0); |
| | 1 | 43 | | printf("[PASS] Inventory: cost calculation\n"); |
| | 1 | 44 | | } |
| | | 45 | | |
| | 1 | 46 | | void test_inventory_sku_format() { |
| | 1 | 47 | | InventoryItem item = {0}; |
| | 1 | 48 | | strcpy(item.sku, "PART-2024-001"); |
| | 1 | 49 | | assert(strlen(item.sku) > 0); |
| | 1 | 50 | | assert(strstr(item.sku, "PART") != NULL); |
| | 1 | 51 | | printf("[PASS] Inventory: SKU format validation\n"); |
| | 1 | 52 | | } |
| | | 53 | | |
| | 1 | 54 | | void test_inventory_negative_quantity_guard() { |
| | 1 | 55 | | InventoryItem item = {.quantity = 10}; |
| | 1 | 56 | | int change = -15; |
| | | 57 | | // Simulating update logic |
| | 1 | 58 | | int new_quantity = item.quantity + change; |
| | 1 | 59 | | assert(new_quantity < 0); // Should be caught by service layer |
| | 1 | 60 | | printf("[PASS] Inventory: negative quantity detection\n"); |
| | 1 | 61 | | } |
| | | 62 | | |
| | 1 | 63 | | int main() { |
| | 1 | 64 | | printf("=== Inventory Service Unit Tests ===\n"); |
| | 1 | 65 | | test_inventory_struct_size(); |
| | 1 | 66 | | test_inventory_initialization(); |
| | 1 | 67 | | test_inventory_low_stock_detection(); |
| | 1 | 68 | | test_inventory_cost_calculation(); |
| | 1 | 69 | | test_inventory_sku_format(); |
| | 1 | 70 | | test_inventory_negative_quantity_guard(); |
| | 1 | 71 | | printf("\n✅ All Inventory Service unit tests passed!\n"); |
| | 1 | 72 | | return 0; |
| | | 73 | | } |