Files
BIPAuto/tests/test_extractor_component.py
Misaka_Company da567a2679 feat(extractor): Add DiscreteMaterialPlanExtractor class for batch data extraction
- Create DiscreteMaterialPlanExtractor class orchestrating web operations and post-processing
- Implement extract_and_process() combining download + Excel conversion
- Implement post_process_downloads() using ExcelConverter for merge
- Add extract_from_file() convenience function for file-based ID input
- Add component test file verifying class structure and methods
- Follow existing patterns: verbose logging, explicit parameters, English comments
2026-03-27 13:48:07 +08:00

121 lines
3.9 KiB
Python

"""
Test DiscreteMaterialPlanExtractor component structure
"""
import sys
from pathlib import Path
# Add project root to Python path
PROJECT_ROOT = Path(__file__).resolve().parent.parent
sys.path.insert(0, str(PROJECT_ROOT))
# Load environment variables
from dotenv import load_dotenv
load_dotenv(PROJECT_ROOT / ".env")
# Configure browser path (not used in this test, but consistent with other tests)
import os
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = os.getenv("PLAYWRIGHT_BROWSERS_PATH", "")
print("=" * 60)
print("Testing DiscreteMaterialPlanExtractor Component")
print("=" * 60)
try:
# Test 1: Import the component
print("\n[1/4] Importing DiscreteMaterialPlanExtractor...")
from utils.discrete_material_plan.extractor import (
DiscreteMaterialPlanExtractor,
extract_from_file
)
print("[OK] Import successful")
# Test 2: Verify class exists and has expected methods
print("\n[2/4] Verifying class structure...")
expected_methods = [
'__init__',
'_print',
'_cleanup_temp_files',
'_get_login_url',
'_chunk_order_ids',
'_download_batches',
'post_process_downloads',
'extract_and_process',
'extract_from_file'
]
missing_methods = []
for method in expected_methods:
if not hasattr(DiscreteMaterialPlanExtractor, method):
missing_methods.append(method)
if missing_methods:
print(f"[FAIL] Missing methods: {missing_methods}")
raise AttributeError(f"Missing methods: {missing_methods}")
print(f"[OK] All {len(expected_methods)} expected methods found")
# Test 3: Test instantiation with dummy parameters (no browser)
print("\n[3/4] Testing class instantiation...")
extractor = DiscreteMaterialPlanExtractor(
username="test_user",
password="test_password",
base_url="https://example.com",
headless=True,
ignore_https_errors=True,
verbose=True,
download_dir=None,
batch_size=10
)
# Verify attributes are set correctly
assert extractor.username == "test_user", "Username not set correctly"
assert extractor.password == "test_password", "Password not set correctly"
assert extractor.base_url == "https://example.com", "Base URL not set correctly"
assert extractor.headless == True, "Headless not set correctly"
assert extractor.ignore_https_errors == True, "HTTPS errors setting not set correctly"
assert extractor.verbose == True, "Verbose not set correctly"
assert extractor.batch_size == 10, "Batch size not set correctly"
assert hasattr(extractor, 'download_dir'), "Download dir not set"
print("[OK] Instantiation successful, all attributes verified")
# Test 4: Test helper methods
print("\n[4/4] Testing helper methods...")
# Test _get_login_url
login_url = extractor._get_login_url()
expected_url = "https://example.com/yonbip/resources/uap/rbac/login/main/index.html"
assert login_url == expected_url, f"Login URL incorrect: {login_url}"
print(f"[OK] _get_login_url() returns: {login_url}")
# Test _chunk_order_ids
test_ids = ["ID1", "ID2", "ID3", "ID4", "ID5"]
extractor.batch_size = 2
chunks = extractor._chunk_order_ids(test_ids)
expected_chunks = [["ID1", "ID2"], ["ID3", "ID4"], ["ID5"]]
assert chunks == expected_chunks, f"Chunking incorrect: {chunks}"
print(f"[OK] _chunk_order_ids() works correctly: {chunks}")
# Test _print (should not raise)
extractor._print("Test message")
print("[OK] _print() works correctly")
# Cleanup
extractor._cleanup_temp_files()
print("\n" + "=" * 60)
print("[SUCCESS] All component tests passed!")
print("=" * 60)
except Exception as e:
print(f"\n[ERROR] Component test failed!")
print(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
print("\n" + "=" * 60)
print("Test completed successfully")
print("=" * 60)