test(logging): unify test file logging to standard logging module

- Replace print() statements with logging module in all test files
- Use consistent logger naming: bipauto.tests.<test_name>
- Apply unified log format: [%(levelname)s] %(name)s: %(message)s
- Migrates test_extractor_real.py, test_login.py, test_auth_config.py, test_extractor_component.py

This change ensures consistent log output format between test files and utils modules, matching the existing bipauto.* logger hierarchy.
This commit is contained in:
Misaka_Company
2026-03-27 16:28:00 +08:00
parent e7bbbbc194
commit 440b74d09a
4 changed files with 164 additions and 124 deletions

View File

@@ -6,6 +6,7 @@ Does not require actual browser or ERP connection.
"""
import sys
import logging
from pathlib import Path
# Add project root to Python path
@@ -20,13 +21,22 @@ load_dotenv(PROJECT_ROOT / ".env")
import os
os.environ["PLAYWRIGHT_BROWSERS_PATH"] = os.getenv("PLAYWRIGHT_BROWSERS_PATH", "")
print("=" * 60)
print("Testing discrete_material_plan.extractor Functions")
print("=" * 60)
# Setup logging
logger = logging.getLogger('bipauto.tests.extractor_component')
logger.setLevel(logging.INFO)
if not logger.handlers:
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("[%(levelname)s] %(name)s: %(message)s"))
logger.addHandler(handler)
logger.propagate = False
logger.info("=" * 60)
logger.info("Testing discrete_material_plan.extractor Functions")
logger.info("=" * 60)
try:
# Test 1: Import all functions
print("\n[1/5] Importing extractor functions...")
logger.info("Importing extractor functions...")
from utils.discrete_material_plan.extractor import (
chunk_order_ids,
get_login_url,
@@ -36,61 +46,61 @@ try:
read_order_ids_from_file,
extract_from_file,
)
print("[OK] All functions imported successfully")
logger.info("All functions imported successfully")
# Test 2: Test chunk_order_ids
print("\n[2/5] Testing chunk_order_ids...")
logger.info("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}")
logger.info(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}")
logger.info(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}")
logger.info(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}")
logger.info(f" chunk_order_ids([], 5) = {result}")
print("[OK] chunk_order_ids works correctly")
logger.info("chunk_order_ids works correctly")
# Test 3: Test get_login_url
print("\n[3/5] Testing get_login_url...")
logger.info("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}")
logger.info(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}")
logger.info(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}")
logger.info(f" get_login_url('https://erp.example.com/some/path/') = {result}")
print("[OK] get_login_url works correctly")
logger.info("get_login_url works correctly")
# Test 4: Test read_order_ids_from_file
print("\n[4/5] Testing read_order_ids_from_file...")
logger.info("Testing read_order_ids_from_file...")
# Create a temporary test file
import tempfile
@@ -107,8 +117,8 @@ 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")
logger.info(f" read_order_ids_from_file(temp_file) = {result}")
logger.info("read_order_ids_from_file works correctly")
finally:
# Cleanup temp file
Path(temp_file).unlink()
@@ -116,13 +126,13 @@ try:
# Test FileNotFoundError
try:
read_order_ids_from_file("nonexistent_file.txt")
print("[FAIL] Should have raised FileNotFoundError")
logger.error("Should have raised FileNotFoundError")
raise AssertionError("Should have raised FileNotFoundError")
except FileNotFoundError:
print(f" read_order_ids_from_file('nonexistent') raises FileNotFoundError [OK]")
logger.info(" read_order_ids_from_file('nonexistent') raises FileNotFoundError")
# Test 5: Test module exports
print("\n[5/5] Testing module exports...")
logger.info("Testing module exports...")
from utils.discrete_material_plan import (
chunk_order_ids as exported_chunk,
get_login_url as exported_get_url,
@@ -132,22 +142,22 @@ try:
read_order_ids_from_file as exported_read_ids,
extract_from_file as exported_extract_file,
)
print("[OK] All functions exported correctly from module")
logger.info("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.")
logger.info("=" * 60)
logger.info("ALL EXTRACTOR FUNCTION TESTS PASSED")
logger.info("=" * 60)
logger.info("Note: extract_batch, extract_batches, extract_and_post_process,")
logger.info(" and extract_from_file require actual browser session and")
logger.info(" are not tested here. Integration tests cover those cases.")
except Exception as e:
print(f"\n[ERROR] Function test failed!")
print(f"Error: {e}")
logger.error(f"Function test failed!")
logger.error(f"Error: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
print("\n" + "=" * 60)
print("Test completed successfully")
print("=" * 60)
logger.info("=" * 60)
logger.info("Test completed successfully")
logger.info("=" * 60)