fixed test_coder

This commit is contained in:
Paul Gauthier 2024-04-18 15:55:59 -07:00
parent f960774b51
commit 512b991a51

View file

@ -6,21 +6,16 @@ from unittest.mock import MagicMock, patch
import git import git
import openai import openai
from aider import models
from aider.coders import Coder from aider.coders import Coder
from aider.dump import dump # noqa: F401 from aider.dump import dump # noqa: F401
from aider.io import InputOutput from aider.io import InputOutput
from aider.models import Model
from aider.utils import ChdirTemporaryDirectory, GitTemporaryDirectory from aider.utils import ChdirTemporaryDirectory, GitTemporaryDirectory
class TestCoder(unittest.TestCase): class TestCoder(unittest.TestCase):
def setUp(self): def setUp(self):
self.patcher = patch("aider.coders.base_coder.check_model_availability") self.GPT35 = Model("gpt-3.5-turbo")
self.mock_check = self.patcher.start()
self.mock_check.return_value = True
def tearDown(self):
self.patcher.stop()
def test_allowed_to_edit(self): def test_allowed_to_edit(self):
with GitTemporaryDirectory(): with GitTemporaryDirectory():
@ -38,7 +33,7 @@ class TestCoder(unittest.TestCase):
# YES! # YES!
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create(models.GPT4, None, io, fnames=["added.txt"]) coder = Coder.create(self.GPT35, None, io, fnames=["added.txt"])
self.assertTrue(coder.allowed_to_edit("added.txt")) self.assertTrue(coder.allowed_to_edit("added.txt"))
self.assertTrue(coder.allowed_to_edit("repo.txt")) self.assertTrue(coder.allowed_to_edit("repo.txt"))
@ -66,7 +61,7 @@ class TestCoder(unittest.TestCase):
# say NO # say NO
io = InputOutput(yes=False) io = InputOutput(yes=False)
coder = Coder.create(models.GPT4, None, io, fnames=["added.txt"]) coder = Coder.create(self.GPT35, None, io, fnames=["added.txt"])
self.assertTrue(coder.allowed_to_edit("added.txt")) self.assertTrue(coder.allowed_to_edit("added.txt"))
self.assertFalse(coder.allowed_to_edit("repo.txt")) self.assertFalse(coder.allowed_to_edit("repo.txt"))
@ -90,7 +85,7 @@ class TestCoder(unittest.TestCase):
# say NO # say NO
io = InputOutput(yes=False) io = InputOutput(yes=False)
coder = Coder.create(models.GPT4, None, io, fnames=["added.txt"]) coder = Coder.create(self.GPT35, None, io, fnames=["added.txt"])
self.assertTrue(coder.allowed_to_edit("added.txt")) self.assertTrue(coder.allowed_to_edit("added.txt"))
self.assertFalse(coder.need_commit_before_edits) self.assertFalse(coder.need_commit_before_edits)
@ -111,7 +106,7 @@ class TestCoder(unittest.TestCase):
repo.git.commit("-m", "new") repo.git.commit("-m", "new")
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, mock_io) coder = Coder.create(self.GPT35, None, mock_io)
mod = coder.get_last_modified() mod = coder.get_last_modified()
@ -134,7 +129,7 @@ class TestCoder(unittest.TestCase):
files = [file1, file2] files = [file1, file2]
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, io=InputOutput(), fnames=files) coder = Coder.create(self.GPT35, None, io=InputOutput(), fnames=files)
content = coder.get_files_content().splitlines() content = coder.get_files_content().splitlines()
self.assertIn("file1.txt", content) self.assertIn("file1.txt", content)
@ -157,7 +152,7 @@ class TestCoder(unittest.TestCase):
repo.git.commit("-m", "new") repo.git.commit("-m", "new")
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, mock_io) coder = Coder.create(self.GPT35, None, mock_io)
# Call the check_for_file_mentions method # Call the check_for_file_mentions method
coder.check_for_file_mentions("Please check file1.txt and file2.py") coder.check_for_file_mentions("Please check file1.txt and file2.py")
@ -175,7 +170,7 @@ class TestCoder(unittest.TestCase):
def test_check_for_ambiguous_filename_mentions_of_longer_paths(self): def test_check_for_ambiguous_filename_mentions_of_longer_paths(self):
with GitTemporaryDirectory(): with GitTemporaryDirectory():
io = InputOutput(pretty=False, yes=True) io = InputOutput(pretty=False, yes=True)
coder = Coder.create(models.GPT4, None, io) coder = Coder.create(self.GPT35, None, io)
fname = Path("file1.txt") fname = Path("file1.txt")
fname.touch() fname.touch()
@ -196,7 +191,7 @@ class TestCoder(unittest.TestCase):
def test_check_for_subdir_mention(self): def test_check_for_subdir_mention(self):
with GitTemporaryDirectory(): with GitTemporaryDirectory():
io = InputOutput(pretty=False, yes=True) io = InputOutput(pretty=False, yes=True)
coder = Coder.create(models.GPT4, None, io) coder = Coder.create(self.GPT35, None, io)
fname = Path("other") / "file1.txt" fname = Path("other") / "file1.txt"
fname.parent.mkdir(parents=True, exist_ok=True) fname.parent.mkdir(parents=True, exist_ok=True)
@ -225,7 +220,7 @@ class TestCoder(unittest.TestCase):
files = [file1, file2] files = [file1, file2]
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, io=InputOutput(), fnames=files) coder = Coder.create(self.GPT35, None, io=InputOutput(), fnames=files)
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = "ok" coder.partial_response_content = "ok"
@ -251,7 +246,7 @@ class TestCoder(unittest.TestCase):
files = [file1, file2] files = [file1, file2]
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, io=InputOutput(), fnames=files) coder = Coder.create(self.GPT35, None, io=InputOutput(), fnames=files)
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = "ok" coder.partial_response_content = "ok"
@ -281,7 +276,7 @@ class TestCoder(unittest.TestCase):
files = [file1] files = [file1]
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, io=InputOutput(), fnames=files) coder = Coder.create(self.GPT35, None, io=InputOutput(), fnames=files)
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = "ok" coder.partial_response_content = "ok"
@ -306,7 +301,7 @@ class TestCoder(unittest.TestCase):
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create( coder = Coder.create(
models.GPT4, self.GPT35,
None, None,
io=InputOutput(encoding=encoding), io=InputOutput(encoding=encoding),
fnames=files, fnames=files,
@ -336,21 +331,19 @@ class TestCoder(unittest.TestCase):
# Mock the IO object # Mock the IO object
mock_io = MagicMock() mock_io = MagicMock()
mock_client = MagicMock()
# Initialize the Coder object with the mocked IO and mocked repo # Initialize the Coder object with the mocked IO and mocked repo
coder = Coder.create(models.GPT4, None, mock_io, client=mock_client) coder = Coder.create(self.GPT35, None, mock_io)
# Set up the mock to raise
mock_client.chat.completions.create.side_effect = openai.BadRequestError(
message="Invalid request",
response=MagicMock(),
body=None,
)
# Call the run method and assert that InvalidRequestError is raised # Call the run method and assert that InvalidRequestError is raised
with self.assertRaises(openai.BadRequestError): with self.assertRaises(openai.BadRequestError):
coder.run(with_message="hi") with patch("litellm.completion") as Mock:
Mock.side_effect = openai.BadRequestError(
message="Invalid request",
response=MagicMock(),
body=None,
)
coder.run(with_message="hi")
def test_new_file_edit_one_commit(self): def test_new_file_edit_one_commit(self):
"""A new file shouldn't get pre-committed before the GPT edit commit""" """A new file shouldn't get pre-committed before the GPT edit commit"""
@ -360,7 +353,7 @@ class TestCoder(unittest.TestCase):
fname = Path("file.txt") fname = Path("file.txt")
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname)]) coder = Coder.create(self.GPT35, "diff", io=io, fnames=[str(fname)])
self.assertTrue(fname.exists()) self.assertTrue(fname.exists())
@ -416,7 +409,7 @@ new
fname1.write_text("ONE\n") fname1.write_text("ONE\n")
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname1), str(fname2)]) coder = Coder.create(self.GPT35, "diff", io=io, fnames=[str(fname1), str(fname2)])
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = f""" coder.partial_response_content = f"""
@ -468,7 +461,7 @@ TWO
fname2.write_text("OTHER\n") fname2.write_text("OTHER\n")
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname)]) coder = Coder.create(self.GPT35, "diff", io=io, fnames=[str(fname)])
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = f""" coder.partial_response_content = f"""
@ -545,7 +538,7 @@ three
repo.git.commit("-m", "initial") repo.git.commit("-m", "initial")
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create(models.GPT4, "diff", io=io, fnames=[str(fname)]) coder = Coder.create(self.GPT35, "diff", io=io, fnames=[str(fname)])
def mock_send(*args, **kwargs): def mock_send(*args, **kwargs):
coder.partial_response_content = f""" coder.partial_response_content = f"""
@ -595,7 +588,7 @@ two
io = InputOutput(yes=True) io = InputOutput(yes=True)
coder = Coder.create( coder = Coder.create(
models.GPT4, self.GPT35,
None, None,
io, io,
fnames=[fname1, fname2, fname3], fnames=[fname1, fname2, fname3],