-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from hrshdhgd/tester
Tester
- Loading branch information
Showing
14 changed files
with
347 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,7 +11,7 @@ jobs: | |
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: [ "3.8", "3.11" ] | ||
python-version: [ "3.9", "3.11" ] | ||
|
||
steps: | ||
- uses: actions/[email protected] | ||
|
@@ -33,4 +33,5 @@ jobs: | |
run: poetry run tox -e lint | ||
|
||
- name: Test with pytest and generate coverage file | ||
run: poetry run tox -e py | ||
run: poetry run pytest --ignore=tests/test_tester.py | ||
# Changed from tox to pytest to make --ignore work. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
.. py:module:: codergpt | ||
Test writing module | ||
=================== | ||
|
||
.. py:class:: CodeTester(chain) | ||
The CodeTester class is responsible for generating testing code from a given source file. It utilizes a llm chain to produce tests for specific functions or classes within the source file. | ||
|
||
.. py:method:: __init__(chain) | ||
Initializes the CodeTester instance with a provided llm chain. | ||
|
||
:param chain: A RunnableSerializable object capable of executing tasks. | ||
:type chain: RunnableSerializable[Dict, Any] | ||
|
||
.. py:method:: write_tests(filename, function=None, classname=None, outfile=None) | ||
Generates test cases for the specified code by invoking the llm chain. If a function or class name is provided, it will generate tests specifically for that function or class. Otherwise, it will attempt to create tests for the entire code. | ||
|
||
:param filename: The path to the code file for which tests are to be written. | ||
:type filename: Union[str, Path] | ||
:param function: The name of the function for which tests should be generated. Defaults to None, indicating that no specific function is targeted. | ||
:type function: Optional[str] | ||
:param classname: The name of the class for which tests should be generated. Defaults to None, indicating that no specific class is targeted. | ||
:type classname: Optional[str] | ||
:param outfile: The path where the generated test file should be saved. If not provided, a default path within the TEST_DIR will be used. | ||
:type outfile: Optional[str] | ||
|
||
The method reads the source code from the provided filename and uses the llm chain to generate appropriate test cases. The resulting test code is then written to either the specified outfile or a new file within the TEST_DIR directory. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
"""Test writing module.""" | ||
|
||
import os | ||
from pathlib import Path | ||
from typing import Any, Dict, Optional, Union | ||
|
||
from langchain_core.runnables.base import RunnableSerializable | ||
|
||
from codergpt.constants import TEST_DIR | ||
|
||
|
||
class CodeTester: | ||
"""Code tester class writes testing code from a given file.""" | ||
|
||
def __init__(self, chain: RunnableSerializable[Dict, Any]): | ||
""" | ||
Initialize the CodeTester class with a runnable chain. | ||
:param chain: A RunnableSerializable object capable of executing tasks. | ||
""" | ||
self.chain = chain | ||
|
||
def write_tests( | ||
self, | ||
filename: Union[str, Path], | ||
function: Optional[str] = None, | ||
classname: Optional[str] = None, | ||
outfile: Optional[str] = None, | ||
): | ||
""" | ||
Write tests for the code by invoking the runnable chain. | ||
:param path: The path to the code file to be explained. | ||
:param function: The name of the function to explain. Default is None. | ||
:param classname: The name of the class to explain. Default is None. | ||
""" | ||
with open(filename, "r") as source_file: | ||
source_code = source_file.read() | ||
if function or classname: | ||
if function: | ||
response = self.chain.invoke( | ||
{ | ||
"input": f"Write tests for the function '{function}' in \n\n```\n{source_code}\n```" | ||
"Return just the code block. Also explain the tests in a systematic way as a comment." | ||
} | ||
) | ||
if classname: | ||
response = self.chain.invoke( | ||
{ | ||
"input": f"Write tests for the class '{classname}' in \n\n```\n{source_code}\n```" | ||
"Also explain the tests in a systematic way." | ||
} | ||
) | ||
else: | ||
# Write tests for full code | ||
response = self.chain.invoke( | ||
{ | ||
"input": f"Write tests for the following code: \n\n```\n{source_code}\n```" | ||
"Also explain the tests in a systematic way." | ||
} | ||
) | ||
test_code = response.content | ||
base_filename = os.path.basename(filename) | ||
if outfile: | ||
new_filepath = outfile | ||
else: | ||
new_filepath = f"{TEST_DIR}/test_{base_filename}" | ||
# Write the test to the new file | ||
with open(new_filepath, "w") as updated_file: | ||
updated_file.write(test_code) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
"""Test python code.""" | ||
|
||
def calculate_sum(numbers): | ||
""" | ||
Calculate the sum of a list of numbers. | ||
:param numbers: A list of numbers. | ||
:type numbers: list[int] | ||
:return: The sum of the numbers. | ||
:rtype: int | ||
""" | ||
result = 0 | ||
for number in numbers: | ||
result += number | ||
return result | ||
|
||
|
||
class MathOperations: | ||
"""Class to perform mathematical operations.""" | ||
|
||
def multiply(self, a, b): | ||
""" | ||
Multiply two numbers. | ||
:param a: The first number. | ||
:type a: int | ||
:param b: The second number. | ||
:type b: int | ||
:return: The product of the two numbers. | ||
:rtype: int | ||
""" | ||
answer = 0 | ||
for i in range(b): | ||
answer += a | ||
return answer |
Oops, something went wrong.