Spaces:
Runtime error

indicator-modeling / test_file_parser.py
Macbath's picture
feat: 数据指标建模助手 v3.0 - 初始版本
ce06eb4
Raw History Blame Contribute Delete
5.51 kB
"""
Test cases for FileParser
P0 Testing - File Parser Layer
"""
import sys
from pathlib import Path
import tempfile
import os
# Add project root to path
PROJECT_ROOT = Path(__file__).parent
sys.path.insert(0, str(PROJECT_ROOT))
try:
from utils.file_parser import FileParser, FileMetadata
from utils.file_parser_simple import FileParser as FileParserSimple
PARSER_AVAILABLE = True
except ImportError as e:
print(f"⚠ Cannot import FileParser: {e}")
PARSER_AVAILABLE = False
class TestFileMetadata:
"""Test cases for FileMetadata model"""
def test_create_file_metadata_success(self):
"""Test creating FileMetadata with valid data"""
if not PARSER_AVAILABLE:
print("⚠ Test 1.1: SKIPPED - FileParser not available")
return
metadata = FileMetadata(
filename="test.csv",
columns=["col1", "col2", "col3"],
dtypes={"col1": "int64", "col2": "float64", "col3": "object"},
sample_data=[{"col1": 1, "col2": 2.5, "col3": "test"}],
row_count=100
)
assert metadata.filename == "test.csv"
assert len(metadata.columns) == 3
assert metadata.row_count == 100
print("✓ Test 1.1: Create FileMetadata - PASSED")
def test_file_metadata_to_dict(self):
"""Test FileMetadata to_dict method"""
if not PARSER_AVAILABLE:
print("⚠ Test 1.2: SKIPPED - FileParser not available")
return
metadata = FileMetadata(
filename="test.csv",
columns=["col1"],
dtypes={"col1": "int64"},
sample_data=[{"col1": 1}],
row_count=10
)
# Use to_dict() method (custom for FileMetadata)
if hasattr(metadata, 'to_dict'):
metadata_dict = metadata.to_dict()
elif hasattr(metadata, 'model_dump'): # pydantic v2
metadata_dict = metadata.model_dump()
else: # pydantic v1
metadata_dict = metadata.dict()
assert isinstance(metadata_dict, dict)
assert metadata_dict["filename"] == "test.csv"
print("✓ Test 1.2: FileMetadata to_dict - PASSED")
class TestFileParser:
"""Test cases for FileParser"""
def setup_method(self):
"""Setup for each test"""
if not PARSER_AVAILABLE:
self.parser = None
return
try:
self.parser = FileParser(max_rows=1000, max_size_mb=10)
except Exception:
self.parser = None
try:
self.parser_simple = FileParserSimple(max_rows=1000)
except Exception:
self.parser_simple = None
def test_create_parser_success(self):
"""Test creating FileParser instance"""
if not PARSER_AVAILABLE or self.parser is None:
print("⚠ Test 2.1: SKIPPED - FileParser not available")
return
assert self.parser.max_rows == 1000
assert self.parser.max_size_mb == 10
print("✓ Test 2.1: Create FileParser - PASSED")
def test_parse_file_method_exists(self):
"""Test that parse_file method exists"""
if not PARSER_AVAILABLE or self.parser is None:
print("⚠ Test 2.2: SKIPPED - FileParser not available")
return
# Check if parse_file method exists
if hasattr(self.parser, 'parse_file'):
print("✓ Test 2.2: parse_file method exists - PASSED")
print(" ⚠ Note: parse_file() is a placeholder (returns None)")
else:
print("✗ Test 2.2: parse_file method not found")
class TestParseUploadedFile:
"""Test cases for parse_uploaded_file utility function"""
def test_parse_uploaded_file_function_exists(self):
"""Test that parse_uploaded_file function can be imported"""
try:
from utils.file_parser import parse_uploaded_file
print("✓ Test 3.1: parse_uploaded_file can be imported - PASSED")
except ImportError:
print("✗ Test 3.1: parse_uploaded_file cannot be imported")
def run_all_tests():
"""Run all FileParser tests"""
print("\n" + "="*60)
print("Running P0 Tests: FileParser")
print("="*60 + "\n")
if not PARSER_AVAILABLE:
print("⚠ FileParser module could not be imported.")
print(" This may be due to missing dependencies (chardet, pandas)")
print(" Some tests will be skipped.\n")
# Test FileMetadata
print("--- Test FileMetadata ---")
test_metadata = TestFileMetadata()
test_metadata.test_create_file_metadata_success()
test_metadata.test_file_metadata_to_dict()
# Test FileParser
print("\n--- Test FileParser ---")
test_parser = TestFileParser()
test_parser.setup_method()
test_parser.test_create_parser_success()
test_parser.test_parse_file_method_exists()
# Test parse_uploaded_file
print("\n--- Test parse_uploaded_file ---")
test_util = TestParseUploadedFile()
test_util.test_parse_uploaded_file_function_exists()
print("\n" + "="*60)
print("⚠ FileParser Tests Completed")
print(" NOTE: FileParser.parse_file() is not fully implemented yet!")
print(" Many tests are placeholders or skipped.")
print("="*60 + "\n")
if __name__ == "__main__":
run_all_tests()