- 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
154 lines
5.5 KiB
Python
154 lines
5.5 KiB
Python
"""
|
|
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
|
|
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 discrete_material_plan.extractor Functions")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
# Test 1: Import all functions
|
|
print("\n[1/5] Importing extractor functions...")
|
|
from utils.discrete_material_plan.extractor import (
|
|
chunk_order_ids,
|
|
get_login_url,
|
|
extract_batch,
|
|
extract_batches,
|
|
extract_and_post_process,
|
|
read_order_ids_from_file,
|
|
extract_from_file,
|
|
)
|
|
print("[OK] All functions imported successfully")
|
|
|
|
# Test 2: Test chunk_order_ids
|
|
print("\n[2/5] Testing chunk_order_ids...")
|
|
|
|
# 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}")
|
|
|
|
# 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}")
|
|
|
|
# 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 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,
|
|
)
|
|
print("[OK] All functions exported correctly from module")
|
|
|
|
print("\n" + "=" * 60)
|
|
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] Function test failed!")
|
|
print(f"Error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
sys.exit(1)
|
|
|
|
print("\n" + "=" * 60)
|
|
print("Test completed successfully")
|
|
print("=" * 60)
|