-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_transcribe.py
83 lines (69 loc) · 3.57 KB
/
test_transcribe.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
# Copyright 2024 roperi. All Rights Reserved.
import unittest
from unittest.mock import patch
import os
import json
from transcribe import main, process_transcript
class TestTranscribeProgram(unittest.TestCase):
@patch('transcribe.DeepgramClient')
def test_main_with_local_file(self, mock_client):
# Mock DeepgramClient instance and its methods
mock_deepgram_instance = mock_client.return_value
mock_deepgram_instance.listen.prerecorded.v.return_value.transcribe_file.return_value.to_dict.return_value = {
"results": {
"summary": {"short": "Test summary"},
"channels": [{"alternatives": [{"paragraphs": {"transcript": "Test transcript"}}]}],
"topics": {"segments": [{"topics": [{"topic": "Test topic"}]}]}
}
}
# Call main function with a local file
input_path = 'test_audio.wav'
project_name = 'test_project'
main(input_path, False, project_name)
# Assert that transcription JSON and related files are created
self.assertTrue(os.path.exists(f'output/{project_name}__transcription.json'))
self.assertTrue(os.path.exists(f'output/{project_name}__summary.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__paragraphs.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__topics.txt'))
@patch('transcribe.DeepgramClient')
def test_main_with_url(self, mock_client):
# Mock DeepgramClient instance and its methods
mock_deepgram_instance = mock_client.return_value
mock_deepgram_instance.listen.prerecorded.v.return_value.transcribe_url.return_value.to_dict.return_value = {
"results": {
"summary": {"short": "Test summary"},
"channels": [{"alternatives": [{"paragraphs": {"transcript": "Test transcript"}}]}],
"topics": {"segments": [{"topics": [{"topic": "Test topic"}]}]}
}
}
# Call main function with a URL
input_url = 'http://example.com/audio.mp3'
project_name = 'test_project'
main(input_url, True, project_name)
# Assert that transcription JSON and related files are created
self.assertTrue(os.path.exists(f'output/{project_name}__transcription.json'))
self.assertTrue(os.path.exists(f'output/{project_name}__summary.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__paragraphs.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__topics.txt'))
def test_process_transcript(self):
# Create a temporary JSON file with some content
test_data = {
"results": {
"summary": {"short": "Test summary"},
"channels": [{"alternatives": [{"paragraphs": {"transcript": "Test transcript"}}]}],
"topics": {"segments": [{"topics": [{"topic": "Test topic"}]}]}
}
}
with open('test_transcription.json', 'w') as f:
json.dump(test_data, f)
# Call process_transcript function with the temporary JSON file
project_name = 'test_project'
process_transcript('test_transcription.json', project_name)
# Assert that output files are created
self.assertTrue(os.path.exists(f'output/{project_name}__summary.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__paragraphs.txt'))
self.assertTrue(os.path.exists(f'output/{project_name}__topics.txt'))
# Clean up temporary file
os.unlink('test_transcription.json')
if __name__ == '__main__':
unittest.main()