mirror of
https://github.com/aljazceru/Auto-GPT.git
synced 2025-12-23 08:54:24 +01:00
Added unittest cases for commands/analyze_code.py file (#4212)
Co-authored-by: Nicholas Tindle <nick@ntindle.com> Co-authored-by: k-boikov <64261260+k-boikov@users.noreply.github.com>
This commit is contained in:
60
tests/test_analyze_code.py
Normal file
60
tests/test_analyze_code.py
Normal file
@@ -0,0 +1,60 @@
|
|||||||
|
# Date: 2023-5-13
|
||||||
|
# Author: Generated by GoCodeo.
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from autogpt.commands.analyze_code import analyze_code
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_call_ai_function(mocker):
|
||||||
|
return mocker.patch("autogpt.commands.analyze_code.call_ai_function")
|
||||||
|
|
||||||
|
|
||||||
|
class TestAnalyzeCode:
|
||||||
|
def test_positive_analyze_code(self, mock_call_ai_function):
|
||||||
|
# Positive Test
|
||||||
|
mock_call_ai_function.return_value = ["Suggestion 1", "Suggestion 2"]
|
||||||
|
code = "def example_function():\n pass"
|
||||||
|
result = analyze_code(code)
|
||||||
|
assert result == ["Suggestion 1", "Suggestion 2"]
|
||||||
|
mock_call_ai_function.assert_called_once_with(
|
||||||
|
"def analyze_code(code: str) -> list[str]:",
|
||||||
|
[code],
|
||||||
|
"Analyzes the given code and returns a list of suggestions for improvements.",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_negative_analyze_code(self, mock_call_ai_function):
|
||||||
|
# Negative Test
|
||||||
|
mock_call_ai_function.return_value = []
|
||||||
|
code = "def example_function():\n pass"
|
||||||
|
result = analyze_code(code)
|
||||||
|
assert result == []
|
||||||
|
mock_call_ai_function.assert_called_once_with(
|
||||||
|
"def analyze_code(code: str) -> list[str]:",
|
||||||
|
[code],
|
||||||
|
"Analyzes the given code and returns a list of suggestions for improvements.",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_error_analyze_code(self, mock_call_ai_function):
|
||||||
|
# Error Test
|
||||||
|
mock_call_ai_function.side_effect = Exception("Error occurred")
|
||||||
|
code = "def example_function():\n pass"
|
||||||
|
with pytest.raises(Exception):
|
||||||
|
analyze_code(code)
|
||||||
|
mock_call_ai_function.assert_called_once_with(
|
||||||
|
"def analyze_code(code: str) -> list[str]:",
|
||||||
|
[code],
|
||||||
|
"Analyzes the given code and returns a list of suggestions for improvements.",
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_edge_analyze_code_empty_code(self, mock_call_ai_function):
|
||||||
|
# Edge Test
|
||||||
|
mock_call_ai_function.return_value = ["Suggestion 1", "Suggestion 2"]
|
||||||
|
code = ""
|
||||||
|
result = analyze_code(code)
|
||||||
|
assert result == ["Suggestion 1", "Suggestion 2"]
|
||||||
|
mock_call_ai_function.assert_called_once_with(
|
||||||
|
"def analyze_code(code: str) -> list[str]:",
|
||||||
|
[code],
|
||||||
|
"Analyzes the given code and returns a list of suggestions for improvements.",
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user