""" Unit tests for spare-parts classification logic. Tests the spare_parts_whitelist module's classification functions to ensure accurate categorization of items as spare parts or consumables. """ import pytest from backend.ai.spare_parts_whitelist import ( classify_as_spare_part, is_consumable, get_spare_part_type ) class TestSparePartsClassification: """Test spare-parts classification logic.""" # Exact match tests def test_exact_match_ram(self): """Exact match for RAM should return True.""" assert classify_as_spare_part("RAM") is True assert classify_as_spare_part("DDR4") is True assert classify_as_spare_part("DRAM") is True def test_exact_match_storage(self): """Exact match for storage should return True.""" assert classify_as_spare_part("SSD") is True assert classify_as_spare_part("NVME") is True assert classify_as_spare_part("HDD") is True def test_exact_match_processor(self): """Exact match for processors should return True.""" assert classify_as_spare_part("CPU") is True assert classify_as_spare_part("GPU") is True assert classify_as_spare_part("PROCESSOR") is True def test_exact_match_power(self): """Exact match for power supplies should return True.""" assert classify_as_spare_part("PSU") is True assert classify_as_spare_part("POWER SUPPLY UNIT") is True # Consumable tests def test_consumable_cables(self): """Cables should return False.""" assert classify_as_spare_part("6ft SATA Cable") is False assert classify_as_spare_part("USB Power Cable") is False assert classify_as_spare_part("Ethernet Cable") is False def test_consumable_fasteners(self): """Fasteners should return False.""" assert classify_as_spare_part("CPU Mounting Hardware Kit") is False assert classify_as_spare_part("Screw Kit") is False assert classify_as_spare_part("Standoff Set") is False def test_consumable_thermal_materials(self): """Thermal materials should return False.""" assert classify_as_spare_part("Thermal Paste") is False assert classify_as_spare_part("Thermal Pad") is False assert classify_as_spare_part("Adhesive Tape") is False # Fuzzy match tests def test_fuzzy_match_ram(self): """Fuzzy match for RAM variants should return True.""" assert classify_as_spare_part("Random Access Memory") is True assert classify_as_spare_part("DDR 4") is True # with space assert classify_as_spare_part("ddr4") is True # lowercase def test_fuzzy_match_storage(self): """Fuzzy match for storage variants should return True.""" assert classify_as_spare_part("Solid State Drive") is True assert classify_as_spare_part("NVMe Drive") is True # Case insensitivity tests def test_case_insensitivity(self): """Classification should be case-insensitive.""" assert classify_as_spare_part("ram") is True assert classify_as_spare_part("SsD") is True assert classify_as_spare_part("sata cable") is False # Edge case tests def test_edge_case_power_cable_vs_psu(self): """Power supply is spare part; power cable is consumable.""" assert classify_as_spare_part("Corsair RM850x 850W Power Supply") is True assert classify_as_spare_part("6ft Power Cable AC Cord") is False def test_edge_case_psu_without_cable(self): """PSU without cable reference is spare part.""" assert classify_as_spare_part("Corsair RM850x 850W PSU") is True # is_consumable function tests def test_is_consumable_function(self): """is_consumable should be inverse of classify_as_spare_part.""" assert is_consumable("RAM") is False assert is_consumable("SATA Cable") is True # get_spare_part_type tests def test_get_spare_part_type_returns_normalized_type(self): """get_spare_part_type returns normalized category or None.""" assert get_spare_part_type("DDR4 RAM") is not None assert get_spare_part_type("Kingston SSD") is not None assert get_spare_part_type("Intel CPU") is not None assert get_spare_part_type("SATA Cable") is None def test_get_spare_part_type_returns_none_for_consumables(self): """get_spare_part_type returns None for consumables.""" assert get_spare_part_type("Power Cable") is None assert get_spare_part_type("Thermal Paste") is None assert get_spare_part_type("Screw Kit") is None # Real-world examples def test_real_world_examples_from_plan(self): """Test real-world examples from the plan.""" assert classify_as_spare_part("Kingston Fury 16GB DDR4-3200") is True assert classify_as_spare_part("Samsung 970 EVO 1TB NVMe") is True assert classify_as_spare_part("Intel Core i7-12700K") is True assert classify_as_spare_part("Corsair RM850x 850W Power Supply") is True def test_real_world_counter_examples(self): """Test real-world counter-examples from the plan.""" assert classify_as_spare_part("6ft SATA Cable") is False assert classify_as_spare_part("CPU Mounting Hardware Kit") is False assert classify_as_spare_part("Thermal Paste Tube") is False # Empty and edge cases def test_empty_string(self): """Empty string should return False.""" assert classify_as_spare_part("") is False def test_whitespace_only(self): """Whitespace-only string should return False.""" assert classify_as_spare_part(" ") is False def test_single_word_consumable(self): """Single word consumable should be classified correctly.""" assert classify_as_spare_part("CABLE") is False assert classify_as_spare_part("SCREW") is False def test_gpu_variant(self): """GPU variants should be classified as spare parts.""" assert classify_as_spare_part("GRAPHICS CARD") is True assert classify_as_spare_part("DISCRETE GPU") is True class TestEdgeCasesAndPatterns: """Test edge cases and pattern matching.""" def test_motherboard_variations(self): """Motherboard variations should be spare parts.""" assert classify_as_spare_part("MOTHERBOARD") is True assert classify_as_spare_part("CHIPSET") is True assert classify_as_spare_part("BIOS") is True def test_memory_dimm_variants(self): """Various DIMM types should be spare parts.""" assert classify_as_spare_part("SODIMM") is True assert classify_as_spare_part("DDR3") is True assert classify_as_spare_part("DDR5") is True assert classify_as_spare_part("DIMM") is True def test_storage_sata_variants(self): """SATA variants should be spare parts.""" assert classify_as_spare_part("SATA") is True assert classify_as_spare_part("M.2") is True assert classify_as_spare_part("HARD DRIVE") is True assert classify_as_spare_part("SOLID STATE DRIVE") is True def test_expansion_cards(self): """Expansion cards should be spare parts.""" assert classify_as_spare_part("PCIE") is True assert classify_as_spare_part("PCI") is True assert classify_as_spare_part("RAID CONTROLLER") is True assert classify_as_spare_part("NETWORK CARD") is True assert classify_as_spare_part("NIC") is True def test_cooling_solutions(self): """Cooling solutions should be spare parts.""" assert classify_as_spare_part("HEATSINK") is True assert classify_as_spare_part("CPU COOLER") is True assert classify_as_spare_part("THERMAL SOLUTION") is True def test_consumable_variations(self): """Various consumable types should be correctly identified.""" assert classify_as_spare_part("CORD") is False assert classify_as_spare_part("FASTENER") is False assert classify_as_spare_part("WASHER") is False assert classify_as_spare_part("BOLT") is False assert classify_as_spare_part("STANDOFF") is False assert classify_as_spare_part("CONNECTOR") is False assert classify_as_spare_part("PLUG") is False assert classify_as_spare_part("SOCKET") is False