Spaces:
Runtime error

File size: 5,508 Bytes
ce06eb4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
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()