-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_llm_agent.py
More file actions
62 lines (46 loc) · 2.3 KB
/
test_llm_agent.py
File metadata and controls
62 lines (46 loc) · 2.3 KB
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
import unittest
from unittest.mock import patch, MagicMock
from llm_agent import LLMAgent
class TestLLMAgent(unittest.TestCase):
@patch('llm_agent.genai.GenerativeModel')
def test_generate_patch_success(self, mock_generative_model):
# Arrange
mock_model_instance = MagicMock()
mock_model_instance.generate_content.return_value.text = "This is a test patch."
mock_generative_model.return_value = mock_model_instance
agent = LLMAgent(api_key="fake_api_key")
# Act
response = agent.generate_patch("test prompt")
# Assert
self.assertEqual(response, "This is a test patch.")
mock_generative_model.assert_called_once()
# Get the actual call arguments
actual_call_args = mock_model_instance.generate_content.call_args
# Check if the prompt contains the expected text
self.assertIn("test prompt", actual_call_args[0][0])
@patch('llm_agent.genai.GenerativeModel')
def test_generate_patch_with_file_path(self, mock_generative_model):
# Arrange
mock_model_instance = MagicMock()
mock_model_instance.generate_content.return_value.text = "This is a test patch for a file."
mock_generative_model.return_value = mock_model_instance
agent = LLMAgent(api_key="fake_api_key")
# Act
response = agent.generate_patch("test prompt", file_path="/path/to/file.py")
# Assert
self.assertEqual(response, "This is a test patch for a file.")
mock_model_instance.generate_content.assert_called_once()
self.assertIn("file.py", mock_model_instance.generate_content.call_args[0][0])
@patch('llm_agent.genai.GenerativeModel')
def test_generate_patch_api_error(self, mock_generative_model):
# Arrange
mock_model_instance = MagicMock()
mock_model_instance.generate_content.side_effect = Exception("API Error")
mock_generative_model.return_value = mock_model_instance
agent = LLMAgent(api_key="fake_api_key")
# Act
response = agent.generate_patch("test prompt")
# Assert
self.assertTrue(response.startswith("Error: An error occurred during the API call:"))
if __name__ == '__main__':
unittest.main()