File size: 6,438 Bytes
111538d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""Test script for DTOs"""

import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))

from application.dtos import AudioUploadDto, ProcessingRequestDto, ProcessingResultDto, ValidationError

def test_audio_upload_dto():
    """Test AudioUploadDto"""
    print("Testing AudioUploadDto...")
    
    # Test valid DTO
    try:
        audio_dto = AudioUploadDto(
            filename="test.wav",
            content=b"fake audio content" * 100,  # Make it larger than 1KB
            content_type="audio/wav"
        )
        print(f"βœ“ Valid AudioUploadDto created: {audio_dto.filename}")
        print(f"  Size: {audio_dto.size} bytes")
        print(f"  Extension: {audio_dto.file_extension}")
        print(f"  Base filename: {audio_dto.base_filename}")
    except Exception as e:
        print(f"βœ— Failed to create valid AudioUploadDto: {e}")
    
    # Test invalid extension
    try:
        AudioUploadDto(
            filename="test.txt",
            content=b"fake content" * 100,
            content_type="text/plain"
        )
        print("βœ— Should have failed with invalid extension")
    except ValueError as e:
        print(f"βœ“ Correctly rejected invalid extension: {e}")
    
    # Test empty content
    try:
        AudioUploadDto(
            filename="test.wav",
            content=b"",
            content_type="audio/wav"
        )
        print("βœ— Should have failed with empty content")
    except ValueError as e:
        print(f"βœ“ Correctly rejected empty content: {e}")

def test_processing_request_dto():
    """Test ProcessingRequestDto"""
    print("\nTesting ProcessingRequestDto...")
    
    # Create valid audio DTO first
    audio_dto = AudioUploadDto(
        filename="test.wav",
        content=b"fake audio content" * 100,
        content_type="audio/wav"
    )
    
    # Test valid DTO
    try:
        request_dto = ProcessingRequestDto(
            audio=audio_dto,
            asr_model="whisper-small",
            target_language="es",
            voice="kokoro",
            speed=1.2,
            source_language="en"
        )
        print(f"βœ“ Valid ProcessingRequestDto created")
        print(f"  ASR Model: {request_dto.asr_model}")
        print(f"  Target Language: {request_dto.target_language}")
        print(f"  Requires Translation: {request_dto.requires_translation}")
        print(f"  Dict representation keys: {list(request_dto.to_dict().keys())}")
    except Exception as e:
        print(f"βœ— Failed to create valid ProcessingRequestDto: {e}")
    
    # Test invalid speed
    try:
        ProcessingRequestDto(
            audio=audio_dto,
            asr_model="whisper-small",
            target_language="es",
            voice="kokoro",
            speed=3.0  # Invalid speed
        )
        print("βœ— Should have failed with invalid speed")
    except ValueError as e:
        print(f"βœ“ Correctly rejected invalid speed: {e}")
    
    # Test invalid ASR model
    try:
        ProcessingRequestDto(
            audio=audio_dto,
            asr_model="invalid-model",
            target_language="es",
            voice="kokoro"
        )
        print("βœ— Should have failed with invalid ASR model")
    except ValueError as e:
        print(f"βœ“ Correctly rejected invalid ASR model: {e}")

def test_processing_result_dto():
    """Test ProcessingResultDto"""
    print("\nTesting ProcessingResultDto...")
    
    # Test successful result
    try:
        success_result = ProcessingResultDto.success_result(
            original_text="Hello world",
            translated_text="Hola mundo",
            audio_path="/tmp/output.wav",
            processing_time=2.5
        )
        print(f"βœ“ Valid success result created")
        print(f"  Success: {success_result.success}")
        print(f"  Has text output: {success_result.has_text_output}")
        print(f"  Has audio output: {success_result.has_audio_output}")
        print(f"  Is complete: {success_result.is_complete}")
    except Exception as e:
        print(f"βœ— Failed to create success result: {e}")
    
    # Test error result
    try:
        error_result = ProcessingResultDto.error_result(
            error_message="TTS generation failed",
            error_code="TTS_ERROR",
            processing_time=1.0
        )
        print(f"βœ“ Valid error result created")
        print(f"  Success: {error_result.success}")
        print(f"  Error message: {error_result.error_message}")
        print(f"  Error code: {error_result.error_code}")
    except Exception as e:
        print(f"βœ— Failed to create error result: {e}")
    
    # Test invalid success result (no outputs)
    try:
        ProcessingResultDto(success=True)  # No outputs provided
        print("βœ— Should have failed with no outputs for success")
    except ValueError as e:
        print(f"βœ“ Correctly rejected success result with no outputs: {e}")
    
    # Test invalid error result (no error message)
    try:
        ProcessingResultDto(success=False)  # No error message
        print("βœ— Should have failed with no error message for failure")
    except ValueError as e:
        print(f"βœ“ Correctly rejected error result with no message: {e}")

def test_dto_serialization():
    """Test DTO serialization/deserialization"""
    print("\nTesting DTO serialization...")
    
    # Test ProcessingResultDto serialization
    try:
        original_result = ProcessingResultDto.success_result(
            original_text="Test text",
            translated_text="Texto de prueba",
            audio_path="/tmp/test.wav",
            processing_time=1.5
        )
        
        # Convert to dict and back
        result_dict = original_result.to_dict()
        restored_result = ProcessingResultDto.from_dict(result_dict)
        
        print(f"βœ“ ProcessingResultDto serialization successful")
        print(f"  Original success: {original_result.success}")
        print(f"  Restored success: {restored_result.success}")
        print(f"  Original text matches: {original_result.original_text == restored_result.original_text}")
        
    except Exception as e:
        print(f"βœ— ProcessingResultDto serialization failed: {e}")

if __name__ == "__main__":
    test_audio_upload_dto()
    test_processing_request_dto()
    test_processing_result_dto()
    test_dto_serialization()
    print("\nDTO testing completed!")