|
| 1 | +import unittest |
| 2 | +from unittest import mock |
| 3 | + |
| 4 | +from interpreter.core.computer.files.files import Files |
| 5 | + |
| 6 | + |
| 7 | +class TestFiles(unittest.TestCase): |
| 8 | + def setUp(self): |
| 9 | + self.files = Files(mock.Mock()) |
| 10 | + |
| 11 | + @mock.patch("interpreter.core.computer.files.files.aifs") |
| 12 | + def test_search(self, mock_aifs): |
| 13 | + # Arrange |
| 14 | + mock_args = ["foo", "bar"] |
| 15 | + mock_kwargs = {"foo": "bar"} |
| 16 | + |
| 17 | + # Act |
| 18 | + self.files.search(mock_args, mock_kwargs) |
| 19 | + |
| 20 | + # Assert |
| 21 | + mock_aifs.search.assert_called_once_with(mock_args, mock_kwargs) |
| 22 | + |
| 23 | + def test_edit_original_text_in_filedata(self): |
| 24 | + # Arrange |
| 25 | + mock_open = mock.mock_open(read_data="foobar") |
| 26 | + mock_write = mock_open.return_value.write |
| 27 | + |
| 28 | + # Act |
| 29 | + with mock.patch("interpreter.core.computer.files.files.open", mock_open): |
| 30 | + self.files.edit("example/filepath/file", "foobar", "foobarbaz") |
| 31 | + |
| 32 | + # Assert |
| 33 | + mock_open.assert_any_call("example/filepath/file", "r") |
| 34 | + mock_open.assert_any_call("example/filepath/file", "w") |
| 35 | + mock_write.assert_called_once_with("foobarbaz") |
| 36 | + |
| 37 | + def test_edit_original_text_not_in_filedata(self): |
| 38 | + # Arrange |
| 39 | + mock_open = mock.mock_open(read_data="foobar") |
| 40 | + |
| 41 | + # Act |
| 42 | + with self.assertRaises(ValueError) as context_manager: |
| 43 | + with mock.patch("interpreter.core.computer.files.files.open", mock_open): |
| 44 | + self.files.edit("example/filepath/file", "barbaz", "foobarbaz") |
| 45 | + |
| 46 | + # Assert |
| 47 | + mock_open.assert_any_call("example/filepath/file", "r") |
| 48 | + self.assertEqual( |
| 49 | + str(context_manager.exception), |
| 50 | + "Original text not found. Did you mean one of these? foobar", |
| 51 | + ) |
0 commit comments