11import os
22import json
33import pytest
4- from unittest .mock import patch , MagicMock
4+ from unittest .mock import patch , MagicMock , Mock
55
66from server .links .analyze_and_label import run , generate_analysis_with_labels , get_analysis_for_type , navigate_dict
77from server .vcon import Vcon
@@ -222,11 +222,13 @@ def test_navigate_dict():
222222 assert navigate_dict (test_dict , "z" ) is None
223223
224224
225+ @patch ('server.links.analyze_and_label.get_openai_client' )
225226@patch ('server.links.analyze_and_label.generate_analysis_with_labels' )
226227@patch ('server.links.analyze_and_label.is_included' , return_value = True )
227228@patch ('server.links.analyze_and_label.randomly_execute_with_sampling' , return_value = True )
228- def test_run_basic (mock_sampling , mock_is_included , mock_generate_analysis , mock_redis_with_vcon , sample_vcon ):
229+ def test_run_basic (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_client , mock_redis_with_vcon , sample_vcon ):
229230 """Test the basic run functionality with mocked analysis generation"""
231+ mock_get_client .return_value = Mock ()
230232 # Set up mock to return analysis JSON
231233 mock_generate_analysis .return_value = json .dumps ({
232234 "labels" : ["customer_service" , "billing_issue" , "refund" ]
@@ -264,12 +266,14 @@ def test_run_basic(mock_sampling, mock_is_included, mock_generate_analysis, mock
264266 assert "refund:refund" in tags_attachment ["body" ]
265267
266268
269+ @patch ('server.links.analyze_and_label.get_openai_client' )
267270@patch ('server.links.analyze_and_label.get_analysis_for_type' )
268271@patch ('server.links.analyze_and_label.generate_analysis_with_labels' )
269272@patch ('server.links.analyze_and_label.is_included' , return_value = True )
270273@patch ('server.links.analyze_and_label.randomly_execute_with_sampling' , return_value = True )
271- def test_run_skip_existing_analysis (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_analysis , mock_redis_with_vcon , sample_vcon_with_analysis ):
274+ def test_run_skip_existing_analysis (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_analysis , mock_get_client , mock_redis_with_vcon , sample_vcon_with_analysis ):
272275 """Test that run skips dialogs with existing labeled analysis"""
276+ mock_get_client .return_value = Mock ()
273277 # Set up mock for generate_analysis_with_labels
274278 mock_generate_analysis .return_value = json .dumps ({
275279 "labels" : ["new_label_that_should_not_be_added" ]
@@ -307,11 +311,13 @@ def test_run_skip_existing_analysis(mock_sampling, mock_is_included, mock_genera
307311 mock_generate_analysis .assert_not_called ()
308312
309313
314+ @patch ('server.links.analyze_and_label.get_openai_client' )
310315@patch ('server.links.analyze_and_label.generate_analysis_with_labels' )
311316@patch ('server.links.analyze_and_label.is_included' , return_value = True )
312317@patch ('server.links.analyze_and_label.randomly_execute_with_sampling' , return_value = True )
313- def test_run_json_parse_error (mock_sampling , mock_is_included , mock_generate_analysis , mock_redis_with_vcon , sample_vcon ):
318+ def test_run_json_parse_error (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_client , mock_redis_with_vcon , sample_vcon ):
314319 """Test handling of JSON parse errors"""
320+ mock_get_client .return_value = Mock ()
315321 # Set up mock to return invalid JSON
316322 mock_generate_analysis .return_value = "This is not valid JSON"
317323
@@ -338,11 +344,13 @@ def test_run_json_parse_error(mock_sampling, mock_is_included, mock_generate_ana
338344 assert tags_attachment is None or len (tags_attachment ["body" ]) == 0
339345
340346
347+ @patch ('server.links.analyze_and_label.get_openai_client' )
341348@patch ('server.links.analyze_and_label.generate_analysis_with_labels' )
342349@patch ('server.links.analyze_and_label.is_included' , return_value = True )
343350@patch ('server.links.analyze_and_label.randomly_execute_with_sampling' , return_value = True )
344- def test_run_analysis_exception (mock_sampling , mock_is_included , mock_generate_analysis , mock_redis_with_vcon , sample_vcon ):
351+ def test_run_analysis_exception (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_client , mock_redis_with_vcon , sample_vcon ):
345352 """Test handling of analysis generation exceptions"""
353+ mock_get_client .return_value = Mock ()
346354 # Make analysis function raise an exception
347355 mock_generate_analysis .side_effect = Exception ("Analysis generation failed" )
348356
@@ -358,11 +366,13 @@ def test_run_analysis_exception(mock_sampling, mock_is_included, mock_generate_a
358366 run ("test-uuid" , "analyze_and_label" , opts )
359367
360368
369+ @patch ('server.links.analyze_and_label.get_openai_client' )
361370@patch ('server.links.analyze_and_label.generate_analysis_with_labels' )
362371@patch ('server.links.analyze_and_label.is_included' , return_value = True )
363372@patch ('server.links.analyze_and_label.randomly_execute_with_sampling' , return_value = True )
364- def test_run_message_format (mock_sampling , mock_is_included , mock_generate_analysis , mock_redis_with_vcon , sample_vcon_message_format ):
373+ def test_run_message_format (mock_sampling , mock_is_included , mock_generate_analysis , mock_get_client , mock_redis_with_vcon , sample_vcon_message_format ):
365374 """Test analyzing a dialog with message format"""
375+ mock_get_client .return_value = Mock ()
366376 # Set up the mock Redis instance to return our sample vCon with message format
367377 mock_instance = mock_redis_with_vcon .return_value
368378 mock_instance .get_vcon .return_value = sample_vcon_message_format
0 commit comments