From a1304d0d54eed7abcdbc76e8fce7020f87d343ae Mon Sep 17 00:00:00 2001 From: Nicholas Albion Date: Wed, 4 Oct 2023 18:15:55 +1100 Subject: [PATCH] restoring other tests --- .github/workflows/ci.yml | 3 +- pilot/helpers/Project.py | 1 - pilot/helpers/agents/test_CodeMonkey.py | 156 ++++++++++++------------ requirements.txt | 1 - 4 files changed, 79 insertions(+), 82 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c3419e9..5a4ba6d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,8 +16,7 @@ jobs: matrix: # 3.10 - 04 Oct 2021 # 3.11 - 24 Oct 2022 - python-version: ['3.9'] -# python-version: ['3.9', '3.10', '3.11', '3.12'] + python-version: ['3.9', '3.10', '3.11', '3.12'] steps: - uses: actions/checkout@v4 diff --git a/pilot/helpers/Project.py b/pilot/helpers/Project.py index f5b5590..e099186 100644 --- a/pilot/helpers/Project.py +++ b/pilot/helpers/Project.py @@ -226,7 +226,6 @@ class Project: # TODO END data['path'], data['full_path'] = self.get_full_file_path(data['path'], data['name']) - update_file(data['full_path'], data['content']) (File.insert(app=self.app, path=data['path'], name=data['name'], full_path=data['full_path']) diff --git a/pilot/helpers/agents/test_CodeMonkey.py b/pilot/helpers/agents/test_CodeMonkey.py index 755d157..d802837 100644 --- a/pilot/helpers/agents/test_CodeMonkey.py +++ b/pilot/helpers/agents/test_CodeMonkey.py @@ -1,6 +1,6 @@ import re import os -from unittest.mock import patch, Mock, MagicMock +from unittest.mock import patch, MagicMock from dotenv import load_dotenv load_dotenv() @@ -40,80 +40,80 @@ class TestCodeMonkey: self.developer = Developer(self.project) self.codeMonkey = CodeMonkey(self.project, developer=self.developer) - # @patch('helpers.AgentConvo.get_saved_development_step', return_value=None) - # @patch('helpers.AgentConvo.save_development_step', new_callable=MagicMock) - # @patch('os.get_terminal_size', mock_terminal_size) - # @patch.object(File, 'insert') - # def test_implement_code_changes(self, mock_get_dev, mock_save_dev, mock_file_insert): - # # Given - # code_changes_description = "Write the word 'Washington' to a .txt file" - # - # if SEND_TO_LLM: - # convo = AgentConvo(self.codeMonkey) - # else: - # convo = MagicMock() - # mock_responses = [ - # # [], - # [{ - # 'content': 'Washington', - # 'description': "A new .txt file with the word 'Washington' in it.", - # 'name': 'washington.txt', - # 'path': 'washington.txt' - # }] - # ] - # convo.send_message.side_effect = mock_responses - # - # if WRITE_TO_FILE: - # self.codeMonkey.implement_code_changes(convo, code_changes_description) - # else: - # # don't write the file, just - # with patch.object(Project, 'save_file') as mock_save_file: - # # When - # self.codeMonkey.implement_code_changes(convo, code_changes_description) - # - # # Then - # mock_save_file.assert_called_once() - # called_data = mock_save_file.call_args[0][0] - # assert re.match(r'\w+\.txt$', called_data['name']) - # assert (called_data['path'] == '/' or called_data['path'] == called_data['name']) - # assert called_data['content'] == 'Washington' - # - # @patch('helpers.AgentConvo.get_saved_development_step', return_value=None) - # @patch('helpers.AgentConvo.save_development_step', new_callable=MagicMock) - # @patch('os.get_terminal_size', mock_terminal_size) - # @patch.object(File, 'insert') - # def test_implement_code_changes_with_read(self, mock_get_dev, mock_save_dev, mock_file_insert): - # # Given - # code_changes_description = "Read the file called file_to_read.txt and write its content to a file called output.txt" - # workspace = self.project.root_path - # update_file(os.path.join(workspace, 'file_to_read.txt'), 'Hello World!\n') - # - # if SEND_TO_LLM: - # convo = AgentConvo(self.codeMonkey) - # else: - # convo = MagicMock() - # mock_responses = [ - # # ['file_to_read.txt', 'output.txt'], - # [{ - # 'content': 'Hello World!\n', - # 'description': 'This file is the output file. The content of file_to_read.txt is copied into this file.', - # 'name': 'output.txt', - # 'path': 'output.txt' - # }] - # ] - # convo.send_message.side_effect = mock_responses - # - # if WRITE_TO_FILE: - # self.codeMonkey.implement_code_changes(convo, code_changes_description) - # else: - # with patch.object(Project, 'save_file') as mock_save_file: - # # When - # self.codeMonkey.implement_code_changes(convo, code_changes_description) - # - # # Then - # clear_directory(workspace) - # mock_save_file.assert_called_once() - # called_data = mock_save_file.call_args[0][0] - # assert called_data['name'] == 'output.txt' - # assert (called_data['path'] == '/' or called_data['path'] == called_data['name']) - # assert called_data['content'] == 'Hello World!\n' + @patch('helpers.AgentConvo.get_saved_development_step', return_value=None) + @patch('helpers.AgentConvo.save_development_step', new_callable=MagicMock) + @patch('os.get_terminal_size', mock_terminal_size) + @patch.object(File, 'insert') + def test_implement_code_changes(self, mock_get_dev, mock_save_dev, mock_file_insert): + # Given + code_changes_description = "Write the word 'Washington' to a .txt file" + + if SEND_TO_LLM: + convo = AgentConvo(self.codeMonkey) + else: + convo = MagicMock() + mock_responses = [ + # [], + [{ + 'content': 'Washington', + 'description': "A new .txt file with the word 'Washington' in it.", + 'name': 'washington.txt', + 'path': 'washington.txt' + }] + ] + convo.send_message.side_effect = mock_responses + + if WRITE_TO_FILE: + self.codeMonkey.implement_code_changes(convo, code_changes_description) + else: + # don't write the file, just + with patch.object(Project, 'save_file') as mock_save_file: + # When + self.codeMonkey.implement_code_changes(convo, code_changes_description) + + # Then + mock_save_file.assert_called_once() + called_data = mock_save_file.call_args[0][0] + assert re.match(r'\w+\.txt$', called_data['name']) + assert (called_data['path'] == '/' or called_data['path'] == called_data['name']) + assert called_data['content'] == 'Washington' + + @patch('helpers.AgentConvo.get_saved_development_step', return_value=None) + @patch('helpers.AgentConvo.save_development_step', new_callable=MagicMock) + @patch('os.get_terminal_size', mock_terminal_size) + @patch.object(File, 'insert') + def test_implement_code_changes_with_read(self, mock_get_dev, mock_save_dev, mock_file_insert): + # Given + code_changes_description = "Read the file called file_to_read.txt and write its content to a file called output.txt" + workspace = self.project.root_path + update_file(os.path.join(workspace, 'file_to_read.txt'), 'Hello World!\n') + + if SEND_TO_LLM: + convo = AgentConvo(self.codeMonkey) + else: + convo = MagicMock() + mock_responses = [ + # ['file_to_read.txt', 'output.txt'], + [{ + 'content': 'Hello World!\n', + 'description': 'This file is the output file. The content of file_to_read.txt is copied into this file.', + 'name': 'output.txt', + 'path': 'output.txt' + }] + ] + convo.send_message.side_effect = mock_responses + + if WRITE_TO_FILE: + self.codeMonkey.implement_code_changes(convo, code_changes_description) + else: + with patch.object(Project, 'save_file') as mock_save_file: + # When + self.codeMonkey.implement_code_changes(convo, code_changes_description) + + # Then + clear_directory(workspace) + mock_save_file.assert_called_once() + called_data = mock_save_file.call_args[0][0] + assert called_data['name'] == 'output.txt' + assert (called_data['path'] == '/' or called_data['path'] == called_data['name']) + assert called_data['content'] == 'Hello World!\n' diff --git a/requirements.txt b/requirements.txt index 5e9ca4e..986e162 100644 --- a/requirements.txt +++ b/requirements.txt @@ -12,7 +12,6 @@ prompt-toolkit==3.0.39 psycopg2-binary==2.9.6 python-dotenv==1.0.0 python-editor==1.0.4 -pytest-mock==3.11.1 questionary==1.10.0 readchar==4.0.5 regex==2023.6.3