refactor(extractor): Convert to stateless pure functions
- Remove DiscreteMaterialPlanExtractor class (stateful) - Replace with pure functions: chunk_order_ids, get_login_url, extract_batch, etc. - All functions are stateless, accept explicit parameters - Caller manages browser/session lifecycle (consistent with extractor_core.py) - Lower coupling: no direct dependency on utils.auth.login - Update tests to match new function signatures Breaking Changes: - DiscreteMaterialPlanExtractor class removed - Use extract_and_post_process() or extract_from_file() instead of class methods - Caller must manage browser session before calling extractor functions
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
"""
|
||||
Test DiscreteMaterialPlanExtractor component structure
|
||||
Test discrete_material_plan.extractor module functions
|
||||
|
||||
Tests pure functions for data extraction and post-processing.
|
||||
Does not require actual browser or ERP connection.
|
||||
"""
|
||||
|
||||
import sys
|
||||
@@ -18,98 +21,128 @@ import os
|
||||
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = os.getenv("PLAYWRIGHT_BROWSERS_PATH", "")
|
||||
|
||||
print("=" * 60)
|
||||
print("Testing DiscreteMaterialPlanExtractor Component")
|
||||
print("Testing discrete_material_plan.extractor Functions")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
# Test 1: Import the component
|
||||
print("\n[1/4] Importing DiscreteMaterialPlanExtractor...")
|
||||
# Test 1: Import all functions
|
||||
print("\n[1/5] Importing extractor functions...")
|
||||
from utils.discrete_material_plan.extractor import (
|
||||
DiscreteMaterialPlanExtractor,
|
||||
extract_from_file
|
||||
chunk_order_ids,
|
||||
get_login_url,
|
||||
extract_batch,
|
||||
extract_batches,
|
||||
extract_and_post_process,
|
||||
read_order_ids_from_file,
|
||||
extract_from_file,
|
||||
)
|
||||
print("[OK] Import successful")
|
||||
print("[OK] All functions imported successfully")
|
||||
|
||||
# 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'
|
||||
]
|
||||
# Test 2: Test chunk_order_ids
|
||||
print("\n[2/5] Testing chunk_order_ids...")
|
||||
|
||||
missing_methods = []
|
||||
for method in expected_methods:
|
||||
if not hasattr(DiscreteMaterialPlanExtractor, method):
|
||||
missing_methods.append(method)
|
||||
# Test normal chunking
|
||||
result = chunk_order_ids(["A", "B", "C", "D", "E"], 2)
|
||||
expected = [["A", "B"], ["C", "D"], ["E"]]
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" chunk_order_ids(['A','B','C','D','E'], 2) = {result}")
|
||||
|
||||
if missing_methods:
|
||||
print(f"[FAIL] Missing methods: {missing_methods}")
|
||||
raise AttributeError(f"Missing methods: {missing_methods}")
|
||||
# Test exact division
|
||||
result = chunk_order_ids(["A", "B", "C", "D"], 2)
|
||||
expected = [["A", "B"], ["C", "D"]]
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" chunk_order_ids(['A','B','C','D'], 2) = {result}")
|
||||
|
||||
print(f"[OK] All {len(expected_methods)} expected methods found")
|
||||
# Test batch size larger than list
|
||||
result = chunk_order_ids(["A", "B"], 10)
|
||||
expected = [["A", "B"]]
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" chunk_order_ids(['A','B'], 10) = {result}")
|
||||
|
||||
# Test empty list
|
||||
result = chunk_order_ids([], 5)
|
||||
expected = []
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" chunk_order_ids([], 5) = {result}")
|
||||
|
||||
print("[OK] chunk_order_ids works correctly")
|
||||
|
||||
# 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
|
||||
# Test 3: Test get_login_url
|
||||
print("\n[3/5] Testing get_login_url...")
|
||||
|
||||
# Test with trailing slash
|
||||
result = get_login_url("https://erp.example.com/")
|
||||
expected = "https://erp.example.com/yonbip/resources/uap/rbac/login/main/index.html"
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" get_login_url('https://erp.example.com/') = {result}")
|
||||
|
||||
# Test without trailing slash
|
||||
result = get_login_url("https://erp.example.com")
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" get_login_url('https://erp.example.com') = {result}")
|
||||
|
||||
# Test with path
|
||||
result = get_login_url("https://erp.example.com/some/path/")
|
||||
expected = "https://erp.example.com/some/path/yonbip/resources/uap/rbac/login/main/index.html"
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" get_login_url('https://erp.example.com/some/path/') = {result}")
|
||||
|
||||
print("[OK] get_login_url works correctly")
|
||||
|
||||
# Test 4: Test read_order_ids_from_file
|
||||
print("\n[4/5] Testing read_order_ids_from_file...")
|
||||
|
||||
# Create a temporary test file
|
||||
import tempfile
|
||||
with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False, encoding='utf-8') as f:
|
||||
f.write("ID001\n")
|
||||
f.write("ID002\n")
|
||||
f.write("\n") # Empty line
|
||||
f.write("ID003\n")
|
||||
f.write(" \n") # Whitespace only
|
||||
f.write("ID004\n")
|
||||
temp_file = f.name
|
||||
|
||||
try:
|
||||
result = read_order_ids_from_file(temp_file)
|
||||
expected = ["ID001", "ID002", "ID003", "ID004"]
|
||||
assert result == expected, f"Expected {expected}, got {result}"
|
||||
print(f" read_order_ids_from_file(temp_file) = {result}")
|
||||
print("[OK] read_order_ids_from_file works correctly")
|
||||
finally:
|
||||
# Cleanup temp file
|
||||
Path(temp_file).unlink()
|
||||
|
||||
# Test FileNotFoundError
|
||||
try:
|
||||
read_order_ids_from_file("nonexistent_file.txt")
|
||||
print("[FAIL] Should have raised FileNotFoundError")
|
||||
raise AssertionError("Should have raised FileNotFoundError")
|
||||
except FileNotFoundError:
|
||||
print(f" read_order_ids_from_file('nonexistent') raises FileNotFoundError [OK]")
|
||||
|
||||
# Test 5: Test module exports
|
||||
print("\n[5/5] Testing module exports...")
|
||||
from utils.discrete_material_plan import (
|
||||
chunk_order_ids as exported_chunk,
|
||||
get_login_url as exported_get_url,
|
||||
extract_batch as exported_extract_batch,
|
||||
extract_batches as exported_extract_batches,
|
||||
extract_and_post_process as exported_extract_and_post,
|
||||
read_order_ids_from_file as exported_read_ids,
|
||||
extract_from_file as exported_extract_file,
|
||||
)
|
||||
|
||||
# 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("[OK] All functions exported correctly from module")
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("[SUCCESS] All component tests passed!")
|
||||
print("[SUCCESS] All extractor function tests passed!")
|
||||
print("=" * 60)
|
||||
print("\nNote: extract_batch, extract_batches, extract_and_post_process,")
|
||||
print(" and extract_from_file require actual browser session and")
|
||||
print(" are not tested here. Integration tests cover those cases.")
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n[ERROR] Component test failed!")
|
||||
print(f"\n[ERROR] Function test failed!")
|
||||
print(f"Error: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
Reference in New Issue
Block a user