Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

Added unittest cases for commands/analyze_code.py file (#4212)

GoCodeof13aba0958b9
diff --git a/tests/test_analyze_code.py b/tests/test_analyze_code.py
new file mode 100644
index 0000000000..48bded032f
--- /dev/null
+++ b/tests/test_analyze_code.py
@@ -0,0 +1,60 @@
+# Date: 2023-5-13
+# Author: Generated by GoCodeo.
+import pytest
+
+from autogpt.commands.analyze_code import analyze_code
+
+
+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.",
+ )