Skip to content

Commit 8710dc7

Browse files
committed
Fixes
1 parent 574bc91 commit 8710dc7

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

label_studio_ml/examples/deepgram/test_model.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import importlib
12
import os
23
from unittest.mock import MagicMock
34

@@ -7,10 +8,13 @@
78
# Ensure the Label Studio SDK inside the Deepgram example sees harmless defaults.
89
os.environ.setdefault('LABEL_STUDIO_URL', 'http://localhost')
910
os.environ.setdefault('LABEL_STUDIO_API_KEY', 'test-token')
11+
1012
try:
11-
from label_studio_ml.examples.deepgram import model as deepgram_model # noqa: E402
13+
deepgram_module = importlib.import_module('label_studio_ml.examples.deepgram.model')
1214
except ImportError:
13-
from model import DeepgramModel as deepgram_model
15+
deepgram_module = importlib.import_module('model')
16+
17+
DeepgramModelCls = deepgram_module.DeepgramModel
1418

1519

1620
@pytest.fixture
@@ -32,13 +36,13 @@ def patched_clients(monkeypatch):
3236
"""Patch the Deepgram SDK, boto3 client, and Label Studio SDK with mocks."""
3337
mock_deepgram_client = MagicMock(name='DeepgramClientInstance')
3438
mock_deepgram_ctor = MagicMock(return_value=mock_deepgram_client)
35-
monkeypatch.setattr(deepgram_model, 'DeepgramClient', mock_deepgram_ctor)
39+
monkeypatch.setattr(deepgram_module, 'DeepgramClient', mock_deepgram_ctor)
3640

3741
mock_s3_client = MagicMock(name='S3Client')
38-
monkeypatch.setattr(deepgram_model.boto3, 'client', MagicMock(return_value=mock_s3_client))
42+
monkeypatch.setattr(deepgram_module.boto3, 'client', MagicMock(return_value=mock_s3_client))
3943

4044
mock_ls = MagicMock(name='LabelStudio')
41-
monkeypatch.setattr(deepgram_model, 'ls', mock_ls)
45+
monkeypatch.setattr(deepgram_module, 'ls', mock_ls)
4246

4347
return {
4448
'deepgram_client': mock_deepgram_client,
@@ -57,7 +61,7 @@ def test_setup_raises_without_api_key(monkeypatch):
5761
monkeypatch.delenv('DEEPGRAM_API_KEY', raising=False)
5862

5963
with pytest.raises(ValueError, match='DEEPGRAM_API_KEY'):
60-
deepgram_model.DeepgramModel()
64+
DeepgramModelCls()
6165

6266

6367
def test_setup_initializes_clients_with_api_key(env_settings, patched_clients):
@@ -66,7 +70,7 @@ def test_setup_initializes_clients_with_api_key(env_settings, patched_clients):
6670
Steps : call setup after patching external clients.
6771
Checks : ensure Deepgram & S3 clients plus region/bucket/folder are stored.
6872
"""
69-
model = deepgram_model.DeepgramModel()
73+
model = DeepgramModelCls()
7074
model.setup()
7175

7276
assert patched_clients['deepgram_ctor'].called
@@ -87,7 +91,7 @@ def test_setup_falls_back_to_access_token(env_settings, patched_clients):
8791
TypeError('unexpected kwarg'),
8892
patched_clients['deepgram_client'],
8993
]
90-
model = deepgram_model.DeepgramModel()
94+
model = DeepgramModelCls()
9195

9296
assert patched_clients['deepgram_ctor'].call_count == 2
9397
first_call_kwargs = patched_clients['deepgram_ctor'].call_args_list[0].kwargs
@@ -103,7 +107,7 @@ def test_predict_no_context_returns_empty_modelresponse(env_settings, patched_cl
103107
Steps : set up env vars and mocks, then call predict with empty context/result payloads.
104108
Checks : confirm an empty ModelResponse is returned immediately without calling external services.
105109
"""
106-
model = deepgram_model.DeepgramModel()
110+
model = DeepgramModelCls()
107111
tasks = [{'id': 1}]
108112

109113
response = model.predict(tasks=tasks, context=None)
@@ -123,7 +127,7 @@ def test_predict_generates_audio_uploads_to_s3_and_updates_task(env_settings, pa
123127
receives the S3 URL, and the temporary file is deleted.
124128
"""
125129
patched_clients['deepgram_client'].speak.v1.audio.generate.return_value = [b'chunk-a', b'chunk-b']
126-
model = deepgram_model.DeepgramModel()
130+
model = DeepgramModelCls()
127131
model.setup()
128132

129133
tasks = [{'id': 123}]
@@ -164,7 +168,7 @@ def test_predict_s3_failure_raises_and_cleans_up_temp_file(env_settings, patched
164168
"""
165169
patched_clients['deepgram_client'].speak.v1.audio.generate.return_value = [b'chunk']
166170
patched_clients['s3_client'].upload_file.side_effect = RuntimeError('s3 boom')
167-
model = deepgram_model.DeepgramModel()
171+
model = DeepgramModelCls()
168172
model.setup()
169173

170174
tasks = [{'id': 999}]
@@ -189,9 +193,9 @@ def test_setup_in_test_mode_uses_stub_clients(monkeypatch):
189193
"""
190194
monkeypatch.setenv('TEST_ENV', '1')
191195
ctor = MagicMock()
192-
monkeypatch.setattr(deepgram_model, 'DeepgramClient', ctor)
196+
monkeypatch.setattr(deepgram_module, 'DeepgramClient', ctor)
193197

194-
model = deepgram_model.DeepgramModel()
198+
model = DeepgramModelCls()
195199

196200
assert model.test_mode is True
197201
ctor.assert_not_called()
@@ -207,9 +211,9 @@ def test_predict_test_mode_skips_label_studio_update(monkeypatch):
207211
Checks : confirm stub S3 upload runs without raising and Label Studio update is not invoked.
208212
"""
209213
monkeypatch.setenv('TEST_ENV', '1')
210-
model = deepgram_model.DeepgramModel()
214+
model = DeepgramModelCls()
211215
mocked_update = MagicMock()
212-
monkeypatch.setattr(deepgram_model.ls.tasks, 'update', mocked_update)
216+
monkeypatch.setattr(deepgram_module.ls.tasks, 'update', mocked_update)
213217

214218
tasks = [{'id': 321}]
215219
context = {

0 commit comments

Comments
 (0)