-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_app.py
131 lines (88 loc) · 4.56 KB
/
test_app.py
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
import pytest
import os
import numpy as np
import openai
from app import extract_entities, load_model, translate_text
# Mocking a simple function to simulate the `extract_entities` behavior
def test_extract_entities():
transcribed_text = "My name is John Doe. You can reach me at [email protected] or call me at 123-456-7890."
entities = extract_entities(transcribed_text)
assert "Name:" in entities, "Extracted entities should contain 'Name'"
assert "Phone Numbers:" in entities, "Extracted entities should contain 'Phone Numbers'"
assert "Email:" in entities, "Extracted entities should contain 'Email'"
assert "Addresses:" in entities, "Extracted entities should contain 'Addresses'"
def test_transcribe_empty_audio(tmp_path):
model_id = "small"
model_path = tmp_path # Use a temporary path for testing
model = load_model(model_id, model_path)
# Create an empty audio array
empty_audio_data = np.array([], dtype=np.float32)
options = dict(beam_size=5, best_of=5, language="en")
transcribe_options = dict(task="transcribe", **options)
try:
transcript = model.transcribe(empty_audio_data, **transcribe_options)
assert False, "Function should raise an exception for empty audio data"
except Exception as e:
assert isinstance(e, Exception), "Function should raise an exception for empty audio data"
# New test cases for audio format and size handling
def test_audio_format(tmp_path):
# Simulate downloading a mock audio file in .wav format
audio_file_wav = os.path.join(tmp_path, "test_audio.wav")
with open(audio_file_wav, "wb") as f:
f.write(b"Mock audio content in .wav format")
# Test .wav format
assert audio_file_wav.endswith(".wav"), "Downloaded audio file should be in .wav format"
def test_audio_file_size(tmp_path):
# Simulate downloading a mock audio file with specific size
audio_file = os.path.join(tmp_path, "test_audio.mp3")
mock_audio_size = 1024 # 1 KB
with open(audio_file, "wb") as f:
f.write(b"Mock audio content with specific size" * mock_audio_size)
# Get size of the downloaded file
file_size = os.path.getsize(audio_file)
assert file_size > 0, "Downloaded audio file size should be greater than 0 bytes"
# New test case for handling OpenAI key expiration
def test_openai_key_expiration():
# Backup the current OpenAI key
backup_key = openai.api_key
# Set an invalid key to simulate expiration
openai.api_key = "invalid_key"
text = "Sample text for translation."
source_language = "hi" # Assuming translating from Hindi
try:
translated_text = translate_text(text, source_language)
assert False, "Function should raise an exception for invalid OpenAI key"
except Exception as e:
assert isinstance(e, Exception), "Function should raise an exception for invalid OpenAI key"
# Restore the original OpenAI key
openai.api_key = backup_key
# Ensure OpenAI key is initialized for the tests
@pytest.fixture(autouse=True)
def setup_openai_key():
openai.api_key = "your_openai_key_here" # Replace with your actual OpenAI key
# Ensure pytest runs with a temporary directory for each test
@pytest.fixture(scope="function", autouse=True)
def temp_directory_for_test(request, tmp_path):
request.cls.tmp_path = tmp_path
# If running pytest directly, include this block
if __name__ == "__main__":
pytest.main()
# Mocking a simple function to simulate the `load_model` behavior
# def test_load_model():
# model_id = "small"
# model_path = "whisper_model"
# model = load_model(model_id, model_path)
# assert model is not None, "Model should be loaded successfully"
# # Mocking a simple function to simulate the `transcribe` behavior
# def test_transcribe_audio():
# model_id = "small"
# model_path = "whisper_model"
# model = load_model(model_id, model_path)
# # Load a sample audio file (you need to provide a valid audio file path here)
# audio_file_path = r"C:\Users\Josh\whisper\audio.mp4"
# audio_data, _ = librosa.load(audio_file_path, sr=16000)
# options = dict(beam_size=5, best_of=5, language="en")
# transcribe_options = dict(task="transcribe", **options)
# transcript = model.transcribe(audio_data, **transcribe_options)
# assert "text" in transcript, "Transcript should contain 'text' key"
# assert transcript["text"] != "", "Transcription text should not be empty"