File size: 3,415 Bytes
e947850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5d84ff
e947850
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f5d84ff
e947850
 
 
 
 
 
 
f5d84ff
e947850
 
 
f5d84ff
e947850
 
 
 
f5d84ff
e947850
 
 
 
 
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
import app as app
from unittest.mock import patch, MagicMock

@patch("app.client_gr.chat.completions.create")
def test_translate_transcript(mock_create):
    # Setup fake response JSON
    mock_create.return_value = MagicMock(
        choices=[MagicMock(message=MagicMock(content="This is the translated text."))]
    )
    # Input for the test
    spanish_input = "Hola, ¿cómo estás?"

    # Call the function under test
    result = app.translate_transcript(spanish_input)

    # Assertions
    assert result == "This is the translated text."
    mock_create.assert_called_once()

@patch("app.client_gr.chat.completions.create")
def test_run_quality_check(mock_create):
    # Mock QA check result
    mock_create.return_value = MagicMock(
        choices=[MagicMock(message=MagicMock(content="Fluency: 9/10, Accuracy: 10/10"))]
    )
    spanish_input = "Hola, ¿cómo estás?"
    english_input = "Hello, how are you?"

    # Call the function under test
    result = app.run_quality_check(spanish_input, english_input)
    # Assertions
    assert result == "Fluency: 9/10, Accuracy: 10/10"
    mock_create.assert_called_once()

@patch("app.client_el.text_to_speech.convert")
def test_generate_dub(mock_create):
    # Simulate the ElevenLabs-like response structure
    mock_create.return_value = MagicMock(
        choices=[MagicMock(
            message=MagicMock(
                content="This is the generated audio stream."
            )
        )])

    input_text = "Hello there!"
    result = app.generate_dub(input_text)

    assert result == "This is the generated audio stream."
    mock_create.assert_called_once()

# ----------------------------
# Edge cases / negative tests
# ----------------------------

@patch("app.client_gr.chat.completions.create")
def test_translate_transcript_empty_input(mock_create):
    # Setup fake response JSON
    mock_create.return_value = MagicMock(
        choices=[MagicMock(message=MagicMock(content=""))]
    )
    # Input for the test
    spanish_input = ""

    # Call the function under test
    result = app.translate_transcript(spanish_input)

    # Assertions
    assert result == ""
    mock_create.assert_called_once()

@patch("app.client_gr.chat.completions.create")
def test_run_quality_check_incomplete(mock_create):
    mock_create.return_value = MagicMock(
        choices=[MagicMock(message=MagicMock(content=""))]
    )
    spanish_input = "Hola, ¿cómo estás?"
    english_input = ""

    # Call the function under test
    result = app.run_quality_check(spanish_input, english_input)
    # Assertions
    assert result == ""
    mock_create.assert_called_once()

@patch("app.client_el.text_to_speech.convert")
def test_generate_dub_empty_text(mock_create):
    mock_create.return_value = MagicMock(
        choices=[MagicMock(message=MagicMock(content=""))])
    
    result = app.generate_dub("")
    assert result == ""

# ----------------------------
# Exception Handling Tests
# ----------------------------

@patch("app.client_gr.chat.completions.create")
def test_translate_transcript_exception(mock_create):
    mock_create.side_effect = Exception("API error")
    spanish_input = "Hola, ¿cómo estás?"

    # Call the function under test
    with patch("builtins.print") as mock_print:
        result = app.translate_transcript(spanish_input)
        assert result is None
        mock_print.assert_called_once_with("API error")