Skip to content

Commit

Permalink
unit_test file for read_file
Browse files Browse the repository at this point in the history
  • Loading branch information
AdarshJha619 committed Jul 31, 2023
2 parents 1b3bf07 + a64405c commit 7d20602
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 100 deletions.
98 changes: 2 additions & 96 deletions tests/unit_tests/tools/file/test_read_file.py
Original file line number Diff line number Diff line change
@@ -1,96 +1,3 @@
# import pytest
# from unittest.mock import patch, mock_open, MagicMock

# from superagi.models.agent_execution import AgentExecution
# from superagi.tools.file.read_file import ReadFileTool
# from superagi.models.agent import Agent


# @pytest.fixture
# def read_file_tool():
# read_file_tool = ReadFileTool()
# read_file_tool.agent_id = 1 # Set a dummy agent ID for testing.

# yield read_file_tool


# def test_read_file_success(read_file_tool):
# # Mock the open function, and make it return a file object that has 'Hello, World!' as its contents.
# mock_file = mock_open(read_data='Hello, World!')
# with patch('builtins.open', mock_file), \
# patch('os.path.exists', return_value=True), \
# patch('os.makedirs', return_value=True), \
# patch('superagi.helper.resource_helper.ResourceHelper.get_root_input_dir',
# return_value="/input_dir/{agent_id}/"), \
# patch('superagi.helper.resource_helper.ResourceHelper.get_root_output_dir',
# return_value="/output_dir/{agent_id}/"), \
# patch('superagi.models.agent.Agent.get_agent_from_id', return_value=Agent(id=1, name='TestAgent')), \
# patch('superagi.models.agent_execution.AgentExecution.get_agent_execution_from_id',
# return_value=
# AgentExecution(id=1, name='TestExecution')):
# read_file_tool.toolkit_config.session = MagicMock()
# file_content = read_file_tool._execute('file.txt')

# expected_content = 'Hello, World!\n File file.txt read successfully.'
# assert file_content == expected_content


# def test_read_file_file_not_found(read_file_tool):
# with patch('os.path.exists', return_value=False), \
# patch('superagi.helper.resource_helper.ResourceHelper.get_root_input_dir',
# return_value="/input_dir/{agent_id}/"), \
# patch('superagi.helper.resource_helper.ResourceHelper.get_root_output_dir',
# return_value="/output_dir/{agent_id}/"), \
# patch('superagi.models.agent.Agent.get_agent_from_id', return_value=Agent(id=1, name='TestAgent')), \
# patch('superagi.models.agent_execution.AgentExecution.get_agent_execution_from_id',
# return_value=AgentExecution(id=1, name='TestExecution')):
# read_file_tool.toolkit_config.session = MagicMock()
# with pytest.raises(FileNotFoundError):
# read_file_tool._execute('file.txt')

# import pytest
# from unittest.mock import patch, mock_open, MagicMock
# from superagi.models.agent_execution import AgentExecution
# from superagi.tools.file.read_file import ReadFileTool
# from superagi.models.agent import Agent

# @pytest.fixture
# @pytest.fixture
# def read_file_tool():
# read_file_tool = ReadFileTool()
# read_file_tool.agent_id = 1 # Set a dummy agent ID for testing.
# yield read_file_tool

# def test_read_file_success_pdf(read_file_tool):
# # Path to a test PDF file in your test suite.
# pdf_file_path = "/home/adarsh/Downloads/pdf_file.pdf"
# # Mock the open function, and make it return a file object that has the contents of your PDF file.
# mock_file = mock_open(read_data=b"Expected content of PDF.")
# with patch('builtins.open', mock_file), \
# patch('os.path.exists', return_value=True), \
# patch('os.makedirs', return_value=True), \
# patch('superagi.models.agent.Agent.get_agent_from_id', return_value=Agent(id=1)), \
# patch('superagi.models.agent_execution.AgentExecution.get_agent_execution_from_id',
# return_value=AgentExecution(id=1)):
# read_file_tool.toolkit_config.session = MagicMock()
# file_content = read_file_tool._execute(pdf_file_path)
# assert "LATEX" in file_content # change "expected content" to the content you expect from PDF

# def test_read_file_success_csv(read_file_tool):
# # Path to a test CSV file in your test suite.
# csv_file_path = "home/adarsh/Downloads/SampleData.csv"
# # Mock the open function, and make it return a file object that has the contents of your CSV file.
# mock_file = mock_open(read_data="Expected content of CSV.")
# with patch('builtins.open', mock_file), \
# patch('os.path.exists', return_value=True), \
# patch('os.makedirs', return_value=True), \
# patch('superagi.models.agent.Agent.get_agent_from_id', return_value=Agent(id=1)), \
# patch('superagi.models.agent_execution.AgentExecution.get_agent_execution_from_id',
# return_value=AgentExecution(id=1)):
# read_file_tool.toolkit_config.session = MagicMock()
# file_content = read_file_tool._execute(csv_file_path)
# assert "East" in file_content # change "expected content" to the content you expect from CSV

import pytest
from unittest.mock import MagicMock, patch
from superagi.tools.file.read_file import ReadFileTool # replace with the actual import path for your class
Expand Down Expand Up @@ -158,9 +65,8 @@ def test_read_file_tool_s3(mock_os_path_exists, mock_os_makedirs, mock_get_confi
result = tool._execute(file_name)

assert isinstance(result, str)
# ensure your expected output matches the actual output




def test_read_file_tool_not_found(mock_os_path_exists, mock_os_makedirs, mock_get_config, mock_s3_helper, mock_partition,
mock_get_agent_from_id, mock_get_agent_execution_from_id):
mock_os_path_exists.return_value = False
Expand Down
7 changes: 3 additions & 4 deletions tools.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
"tools": {

}
}
"tools": {
}
}

0 comments on commit 7d20602

Please sign in to comment.