aboutsummaryrefslogtreecommitdiff
path: root/autogpts/autogpt/tests/unit/test_utils.py
diff options
context:
space:
mode:
Diffstat (limited to 'autogpts/autogpt/tests/unit/test_utils.py')
-rw-r--r--autogpts/autogpt/tests/unit/test_utils.py66
1 files changed, 47 insertions, 19 deletions
diff --git a/autogpts/autogpt/tests/unit/test_utils.py b/autogpts/autogpt/tests/unit/test_utils.py
index 2cea8dce9..afe2497dc 100644
--- a/autogpts/autogpt/tests/unit/test_utils.py
+++ b/autogpts/autogpt/tests/unit/test_utils.py
@@ -3,6 +3,7 @@ from unittest.mock import patch
import pytest
import requests
+from git import InvalidGitRepositoryError
from autogpt.app.utils import (
get_bulletin_from_web,
@@ -18,11 +19,15 @@ from tests.utils import skip_in_ci
def valid_json_response() -> dict:
return {
"thoughts": {
- "text": "My task is complete. I will use the 'task_complete' command to shut down.",
- "reasoning": "I will use the 'task_complete' command because it allows me to shut down and signal that my task is complete.",
- "plan": "I will use the 'task_complete' command with the reason 'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
- "criticism": "I need to ensure that I have completed all necessary tasks before shutting down.",
- "speak": "",
+ "text": "My task is complete. I will use the 'task_complete' command "
+ "to shut down.",
+ "reasoning": "I will use the 'task_complete' command because it allows me "
+ "to shut down and signal that my task is complete.",
+ "plan": "I will use the 'task_complete' command with the reason "
+ "'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
+ "criticism": "I need to ensure that I have completed all necessary tasks "
+ "before shutting down.",
+ "speak": "All done!",
},
"command": {
"name": "task_complete",
@@ -35,10 +40,14 @@ def valid_json_response() -> dict:
def invalid_json_response() -> dict:
return {
"thoughts": {
- "text": "My task is complete. I will use the 'task_complete' command to shut down.",
- "reasoning": "I will use the 'task_complete' command because it allows me to shut down and signal that my task is complete.",
- "plan": "I will use the 'task_complete' command with the reason 'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
- "criticism": "I need to ensure that I have completed all necessary tasks before shutting down.",
+ "text": "My task is complete. I will use the 'task_complete' command "
+ "to shut down.",
+ "reasoning": "I will use the 'task_complete' command because it allows me "
+ "to shut down and signal that my task is complete.",
+ "plan": "I will use the 'task_complete' command with the reason "
+ "'Task complete: retrieved Tesla's revenue in 2022.' to shut down.",
+ "criticism": "I need to ensure that I have completed all necessary tasks "
+ "before shutting down.",
"speak": "",
},
"command": {"name": "", "args": {}},
@@ -51,27 +60,32 @@ def test_validate_yaml_file_valid():
result, message = validate_yaml_file("valid_test_file.yaml")
os.remove("valid_test_file.yaml")
- assert result == True
+ assert result is True
assert "Successfully validated" in message
def test_validate_yaml_file_not_found():
result, message = validate_yaml_file("non_existent_file.yaml")
- assert result == False
+ assert result is False
assert "wasn't found" in message
def test_validate_yaml_file_invalid():
with open("invalid_test_file.yaml", "w") as f:
f.write(
- "settings:\n first_setting: value\n second_setting: value\n nested_setting: value\n third_setting: value\nunindented_setting: value"
+ "settings:\n"
+ " first_setting: value\n"
+ " second_setting: value\n"
+ " nested_setting: value\n"
+ " third_setting: value\n"
+ "unindented_setting: value"
)
result, message = validate_yaml_file("invalid_test_file.yaml")
os.remove("invalid_test_file.yaml")
print(result)
print(message)
- assert result == False
+ assert result is False
assert "There was an issue while trying to read" in message
@@ -85,7 +99,7 @@ def test_get_bulletin_from_web_success(mock_get):
assert expected_content in bulletin
mock_get.assert_called_with(
- "https://raw.githubusercontent.com/Significant-Gravitas/AutoGPT/master/autogpts/autogpt/BULLETIN.md"
+ "https://raw.githubusercontent.com/Significant-Gravitas/AutoGPT/master/autogpts/autogpt/BULLETIN.md" # noqa: E501
)
@@ -121,7 +135,7 @@ def test_get_latest_bulletin_with_file():
with patch("autogpt.app.utils.get_bulletin_from_web", return_value=""):
bulletin, is_new = get_latest_bulletin()
assert expected_content in bulletin
- assert is_new == False
+ assert is_new is False
os.remove("data/CURRENT_BULLETIN.md")
@@ -152,7 +166,7 @@ def test_get_latest_bulletin_new_bulletin_same_as_old_bulletin():
):
bulletin, is_new = get_latest_bulletin()
assert expected_content in bulletin
- assert is_new == False
+ assert is_new is False
os.remove("data/CURRENT_BULLETIN.md")
@@ -160,8 +174,6 @@ def test_get_latest_bulletin_new_bulletin_same_as_old_bulletin():
@skip_in_ci
def test_get_current_git_branch():
branch_name = get_current_git_branch()
-
- # Assuming that the branch name will be non-empty if the function is working correctly.
assert branch_name != ""
@@ -175,7 +187,7 @@ def test_get_current_git_branch_success(mock_repo):
@patch("autogpt.app.utils.Repo")
def test_get_current_git_branch_failure(mock_repo):
- mock_repo.side_effect = Exception()
+ mock_repo.side_effect = InvalidGitRepositoryError()
branch_name = get_current_git_branch()
assert branch_name == ""
@@ -193,3 +205,19 @@ def test_extract_json_from_response_wrapped_in_code_block(valid_json_response: d
assert (
extract_dict_from_response(emulated_response_from_openai) == valid_json_response
)
+
+
+def test_extract_json_from_response_wrapped_in_code_block_with_language(
+ valid_json_response: dict,
+):
+ emulated_response_from_openai = "```json" + str(valid_json_response) + "```"
+ assert (
+ extract_dict_from_response(emulated_response_from_openai) == valid_json_response
+ )
+
+
+def test_extract_json_from_response_json_contained_in_string(valid_json_response: dict):
+ emulated_response_from_openai = "sentence1" + str(valid_json_response) + "sentence2"
+ assert (
+ extract_dict_from_response(emulated_response_from_openai) == valid_json_response
+ )