repositories
loading repo index
repositories
loading repo index
repository
loading code, commits, and activity
public Clawd ADK gateway launch mirror
stars
latest
clone command
git clone gitlawb://did:key:z6Mkq5mY...iFZ5/my-project-publ...git clone gitlawb://did:key:z6Mkq5mY.../my-project-publ...2fa351d6docs: add automaton and perps launch sources16d ago| #1 | import hashlib |
| #2 | import os |
| #3 | import sys |
| #4 | from unittest.mock import mock_open, patch |
| #5 | |
| #6 | import pytest |
| #7 | |
| #8 | if sys.version_info > (3, 10): # as `match` statement was introduced in python 3.10 |
| #9 | from deepgram import PrerecordedOptions |
| #10 | |
| #11 | from embedchain.loaders.audio import AudioLoader |
| #12 | |
| #13 | |
| #14 | @pytest.fixture |
| #15 | def setup_audio_loader(mocker): |
| #16 | mock_dropbox = mocker.patch("deepgram.DeepgramClient") |
| #17 | mock_dbx = mocker.MagicMock() |
| #18 | mock_dropbox.return_value = mock_dbx |
| #19 | |
| #20 | os.environ["DEEPGRAM_API_KEY"] = "test_key" |
| #21 | loader = AudioLoader() |
| #22 | loader.client = mock_dbx |
| #23 | |
| #24 | yield loader, mock_dbx |
| #25 | |
| #26 | if "DEEPGRAM_API_KEY" in os.environ: |
| #27 | del os.environ["DEEPGRAM_API_KEY"] |
| #28 | |
| #29 | |
| #30 | @pytest.mark.skipif( |
| #31 | sys.version_info < (3, 10), reason="Test skipped for Python 3.9 or lower" |
| #32 | ) # as `match` statement was introduced in python 3.10 |
| #33 | def test_initialization(setup_audio_loader): |
| #34 | """Test initialization of AudioLoader.""" |
| #35 | loader, _ = setup_audio_loader |
| #36 | assert loader is not None |
| #37 | |
| #38 | |
| #39 | @pytest.mark.skipif( |
| #40 | sys.version_info < (3, 10), reason="Test skipped for Python 3.9 or lower" |
| #41 | ) # as `match` statement was introduced in python 3.10 |
| #42 | def test_load_data_from_url(setup_audio_loader): |
| #43 | loader, mock_dbx = setup_audio_loader |
| #44 | url = "https://example.com/audio.mp3" |
| #45 | expected_content = "This is a test audio transcript." |
| #46 | |
| #47 | mock_response = {"results": {"channels": [{"alternatives": [{"transcript": expected_content}]}]}} |
| #48 | mock_dbx.listen.prerecorded.v.return_value.transcribe_url.return_value = mock_response |
| #49 | |
| #50 | result = loader.load_data(url) |
| #51 | |
| #52 | doc_id = hashlib.sha256((expected_content + url).encode()).hexdigest() |
| #53 | expected_result = { |
| #54 | "doc_id": doc_id, |
| #55 | "data": [ |
| #56 | { |
| #57 | "content": expected_content, |
| #58 | "meta_data": {"url": url}, |
| #59 | } |
| #60 | ], |
| #61 | } |
| #62 | |
| #63 | assert result == expected_result |
| #64 | mock_dbx.listen.prerecorded.v.assert_called_once_with("1") |
| #65 | mock_dbx.listen.prerecorded.v.return_value.transcribe_url.assert_called_once_with( |
| #66 | {"url": url}, PrerecordedOptions(model="nova-2", smart_format=True) |
| #67 | ) |
| #68 | |
| #69 | |
| #70 | @pytest.mark.skipif( |
| #71 | sys.version_info < (3, 10), reason="Test skipped for Python 3.9 or lower" |
| #72 | ) # as `match` statement was introduced in python 3.10 |
| #73 | def test_load_data_from_file(setup_audio_loader): |
| #74 | loader, mock_dbx = setup_audio_loader |
| #75 | file_path = "local_audio.mp3" |
| #76 | expected_content = "This is a test audio transcript." |
| #77 | |
| #78 | mock_response = {"results": {"channels": [{"alternatives": [{"transcript": expected_content}]}]}} |
| #79 | mock_dbx.listen.prerecorded.v.return_value.transcribe_file.return_value = mock_response |
| #80 | |
| #81 | # Mock the file reading functionality |
| #82 | with patch("builtins.open", mock_open(read_data=b"some data")) as mock_file: |
| #83 | result = loader.load_data(file_path) |
| #84 | |
| #85 | doc_id = hashlib.sha256((expected_content + file_path).encode()).hexdigest() |
| #86 | expected_result = { |
| #87 | "doc_id": doc_id, |
| #88 | "data": [ |
| #89 | { |
| #90 | "content": expected_content, |
| #91 | "meta_data": {"url": file_path}, |
| #92 | } |
| #93 | ], |
| #94 | } |
| #95 | |
| #96 | assert result == expected_result |
| #97 | mock_dbx.listen.prerecorded.v.assert_called_once_with("1") |
| #98 | mock_dbx.listen.prerecorded.v.return_value.transcribe_file.assert_called_once_with( |
| #99 | {"buffer": mock_file.return_value}, PrerecordedOptions(model="nova-2", smart_format=True) |
| #100 | ) |
| #101 |