Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions tests/steps/test_ExtractCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ def test_extract_code_init(extract_code_instance):

def test_extract_code_run(extract_code_instance, tmp_path):
# Run the extract code step
"""Test the extract_code method of an ExtractCode instance.

This method runs the extract_code step and verifies the expected output structure and content.

Args:
extract_code_instance (ExtractCode): An instance of the ExtractCode class to be tested.
tmp_path (pathlib.Path): A temporary directory path provided by the pytest fixture.

Returns:
None

Raises:
AssertionError: If any of the assertions fail, indicating that the extract_code output
does not match the expected structure or content.
"""
result = extract_code_instance.run()

assert result.keys() == {"files_to_patch"}
Expand All @@ -69,6 +84,18 @@ def test_extract_code_run(extract_code_instance, tmp_path):

@pytest.fixture
def extract_code_instance_with_fix(tmp_path):
"""Extract code instance with fix from a given test file.

Args:
tmp_path (Path): A temporary directory path for creating and manipulating files.

Returns:
Generator[ExtractCode]: A generator that yields an ExtractCode object containing
the extracted code instance with fix information.

Raises:
OSError: If there are issues with file operations or directory changes.
"""
original_dir = Path.cwd()

os.chdir(tmp_path)
Expand Down Expand Up @@ -109,6 +136,18 @@ def extract_code_instance_with_fix(tmp_path):

def test_extract_code_run_with_fix(extract_code_instance_with_fix, tmp_path):
# Run the extract code step
"""Test the extraction of code with fix functionality.

Args:
extract_code_instance_with_fix (ExtractCode): An instance of ExtractCode class with fix functionality.
tmp_path (pathlib.Path): A temporary directory path for the test.

Returns:
None: This method doesn't return anything explicitly.

Raises:
AssertionError: If any of the assertions fail during the test.
"""
result = extract_code_instance_with_fix.run()

assert result.keys() == {"files_to_patch"}
Expand Down
21 changes: 21 additions & 0 deletions tests/steps/test_ExtractModelResponse.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

@pytest.fixture
def sample_inputs():
"""Generates sample inputs for testing or demonstration purposes.

Returns:
dict: A dictionary containing two keys:
- 'openai_responses' (list): A list of strings representing sample responses,
where each response may contain multiple partitions.
- 'response_partitions' (dict): A dictionary where keys are identifiers and
values are lists of partition strings to be used for matching.

"""
return {
"openai_responses": ["partition1response1partition2", "response2partition3"],
"response_partitions": {"key1": ["partition1", "partition2"], "key2": ["partition3"]},
Expand All @@ -25,6 +35,17 @@ def test_init_missing_required_keys():


def test_run_no_partitions(sample_inputs):
"""Test the ExtractModelResponse step with no partitions in the input.

Args:
sample_inputs (dict): A dictionary containing sample inputs for the ExtractModelResponse step.

Returns:
None

Raises:
AssertionError: If the output does not match the expected format or values.
"""
step = ExtractModelResponse({**sample_inputs, "response_partitions": {}})
output = step.run()
assert len(output["extracted_responses"]) == 2
Expand Down
40 changes: 40 additions & 0 deletions tests/steps/test_ModifyCode.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ def test_handle_indent(src, target, expected):


def test_replace_code_in_file(tmp_path):
"""Test the replace_code_in_file function.

Args:
tmp_path (pathlib.Path): A temporary directory path fixture provided by pytest.

Returns:
None: This function doesn't return anything explicitly.

Raises:
AssertionError: If the file content after replacement doesn't match the expected output.
"""
file_path = tmp_path / "test.txt"
file_path.write_text("line 1\nline 2\nline 3")
start_line = 1
Expand All @@ -56,6 +67,20 @@ def test_modify_code_init():


def test_modify_code_run(tmp_path):
"""Test the modify_code_run functionality

This function tests the ModifyCode class's run method by creating a temporary file,
modifying its content, and verifying the result.

Args:
tmp_path (pathlib.Path): A pytest fixture that provides a temporary directory unique to the test invocation.

Returns:
None

Raises:
AssertionError: If the result doesn't contain the expected 'modified_code_files' key or if the number of modified files is not 1.
"""
file_path = tmp_path / "test.txt"
file_path.write_text("line 1\nline 2\nline 3")

Expand All @@ -74,6 +99,21 @@ def test_modify_code_run(tmp_path):


def test_modify_code_none_edit(tmp_path):
"""Test the modify_code method with no edits

This function tests the behavior of the ModifyCode class when no edits are provided.
It creates a test file, sets up input data with no patch, and verifies that the
ModifyCode.run() method returns an empty list of modified code files.

Args:
tmp_path (pathlib.Path): A temporary directory path provided by pytest for testing

Returns:
None: This test function doesn't return anything, it uses assertions to verify behavior

Raises:
AssertionError: If the assertions in the test fail
"""
file_path = tmp_path / "test.txt"
file_path.write_text("line 1\nline 2\nline 3")

Expand Down
10 changes: 10 additions & 0 deletions tests/steps/test_PreparePR.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,16 @@

@pytest.fixture
def prepare_pr_instance():
"""Prepares a PreparePR instance with modified code file information.

Args:
None

Returns:
PreparePR: An instance of PreparePR initialized with a dictionary containing
information about modified code files.

"""
inputs = {
"modified_code_files": [
{"path": "file1", "start_line": 1, "end_line": 2, "commit_message": "commit msg"},
Expand Down
71 changes: 71 additions & 0 deletions tests/steps/test_PreparePrompt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,24 @@

@pytest.fixture
def valid_prompt_file():
"""A context manager that creates a temporary file containing a valid prompt dictionary.

Args:
None

Returns:
str: The path to the temporary file containing the serialized prompt dictionary.

Yields:
str: The path to the temporary file containing the serialized prompt dictionary.

Notes:
- This method uses a context manager to ensure proper cleanup of the temporary file.
- The temporary file is created using tempfile.NamedTemporaryFile with write mode.
- The _PROMPT_FILE_DICT is assumed to be a predefined dictionary containing prompt data.
- The temporary file is not automatically deleted when closed, allowing for its use after the context manager exits.
- The file is manually closed in the 'finally' block to ensure proper resource management.
"""
fp = tempfile.NamedTemporaryFile("w", delete=False)
try:
json.dump(_PROMPT_FILE_DICT, fp)
Expand All @@ -32,6 +50,22 @@ def valid_prompt_file():

@pytest.fixture
def valid_prompt_values_file():
"""Creates a temporary file with valid prompt values and yields the file path.

This method creates a temporary file, writes the contents of _PROMPT_VALUES (assumed to be a
dictionary or list) as JSON to the file, and yields the file path. The file is automatically
closed and deleted after use.

Returns:
str: The path to the temporary file containing the valid prompt values in JSON format.

Yields:
str: The path to the temporary file.

Raises:
IOError: If there's an error writing to the temporary file.
JSONDecodeError: If there's an error encoding _PROMPT_VALUES to JSON.
"""
fp = tempfile.NamedTemporaryFile("w", delete=False)
try:
json.dump(_PROMPT_VALUES, fp)
Expand Down Expand Up @@ -61,6 +95,19 @@ def valid_prompt_values_file():
],
)
def test_prepare_prompt_required_keys(valid_prompt_file, valid_prompt_values_file, keys):
"""Test the PreparePrompt class with missing required keys.

Args:
valid_prompt_file (str): Path to a valid prompt template file.
valid_prompt_values_file (str): Path to a valid prompt values file.
keys (list): List of keys to exclude from the input dictionary.

Returns:
None

Raises:
ValueError: When the PreparePrompt class is instantiated with missing required keys.
"""
inputs = {
"prompt_template_file": valid_prompt_file,
"prompt_id": _PROMPT_ID,
Expand Down Expand Up @@ -88,6 +135,19 @@ def test_prepare_prompt_non_existent_files(valid_prompt_file, valid_prompt_value

@pytest.mark.parametrize("key", ["prompt_values", "prompt_value_file"])
def test_prepare_prompt_prompt_values(valid_prompt_file, valid_prompt_values_file, key):
"""Test the PreparePrompt class with different input configurations.

Args:
valid_prompt_file (str): Path to a valid prompt template file.
valid_prompt_values_file (str): Path to a valid prompt values file.
key (str): The key to remove from the inputs dictionary.

Returns:
None: This method doesn't return anything explicitly.

Raises:
AssertionError: If the assertions for prompt_template or prompt_values fail.
"""
inputs = {
"prompt_template_file": valid_prompt_file,
"prompt_id": _PROMPT_ID,
Expand All @@ -101,6 +161,17 @@ def test_prepare_prompt_prompt_values(valid_prompt_file, valid_prompt_values_fil


def test_prepare_prompt_prompts(valid_prompt_file):
"""Tests the preparation of prompts using the PreparePrompt class.

Args:
valid_prompt_file (str): Path to a valid prompt template file.

Returns:
None

Raises:
AssertionError: If the prompts are not generated correctly or if the number of prompts is unexpected.
"""
inputs = {
"prompt_template_file": valid_prompt_file,
"prompt_id": _PROMPT_ID,
Expand Down
53 changes: 53 additions & 0 deletions tests/steps/test_QueryEmbeddings.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@

@pytest.fixture
def setup_collection():
"""Sets up a test collection in a ChromaDB persistent client for testing purposes.

Args:
None

Returns:
chromadb.api.models.Collection.Collection: A ChromaDB collection object for testing.

Yields:
chromadb.api.models.Collection.Collection: A ChromaDB collection object for testing.

Raises:
chromadb.errors.ChromaError: If there's an issue with creating or accessing the ChromaDB client or collection.

Notes:
- This function is a generator that yields a test collection.
- The collection is created with a fixed name "test".
- Three documents are inserted into the collection with UUIDs as ids.
- After yielding the collection, it deletes the test collection from the client.
"""
_TEST_COLLECTION = "test"

client = chromadb.PersistentClient(path=get_vector_db_path())
Expand All @@ -28,6 +48,23 @@ def setup_collection():

def test_required_keys(setup_collection):
# Test that the required keys are checked
"""Test the required keys validation for QueryEmbeddings.

This method tests whether the QueryEmbeddings class correctly checks for the
presence of required keys in the input dictionary.

Args:
setup_collection (object): A fixture providing a pre-configured collection
for testing purposes.

Returns:
None: This test method doesn't return a value, but raises an assertion
if the test fails.

Raises:
AssertionError: If the required keys are not properly validated by the
QueryEmbeddings class.
"""
inputs = {"embedding_name": setup_collection.name, "texts": ["text1", "text2"]}
query_embeddings = QueryEmbeddings(inputs)

Expand All @@ -52,6 +89,22 @@ def test_query_results(setup_collection):
@pytest.mark.parametrize("top_k", [1, 2, 3])
def test_top_k(setup_collection, top_k):
# Test that the token limit is enforced
"""Test the top-k functionality of QueryEmbeddings

This method tests whether the token limit (top_k) is correctly enforced in the QueryEmbeddings class.
It creates a QueryEmbeddings instance with a single input text and a specified top_k value,
then verifies that the number of embedding results matches the top_k parameter.

Args:
setup_collection: The collection object used for setting up the test environment.
top_k (int): The maximum number of results to return.

Returns:
None: This method doesn't return a value, but uses assertions to validate the behavior.

Raises:
AssertionError: If the number of embedding results does not match the specified top_k value.
"""
inputs = {"embedding_name": setup_collection.name, "texts": ["text1"], "top_k": top_k}
query_embeddings = QueryEmbeddings(inputs)
results = query_embeddings.run()
Expand Down
14 changes: 14 additions & 0 deletions tests/steps/test_ReadIssues.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@
)
def test_read_issues(mocker, inputs_extra, method_path, issue_texts):
# Set up
"""Test the read_issues method of the ReadIssues class.

Args:
mocker (pytest.MockFixture): A pytest fixture for mocking.
inputs_extra (dict): Additional input parameters to be merged with base inputs.
method_path (str): The path to the method to be mocked.
issue_texts (dict): A dictionary containing issue texts.

Returns:
None: This test method doesn't return anything, but uses assertions to verify the behavior.

Raises:
AssertionError: If the results from read_issues.run() do not match the expected output.
"""
base_inputs = {"issue_url": "https://example.com/issue"}
inputs = {**base_inputs, **inputs_extra}

Expand Down
15 changes: 15 additions & 0 deletions tests/steps/test_ScanDepscan.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,21 @@

@pytest.mark.skip(reason="Seeing F in CI but not locally")
def test_run():
"""Test the ScanDepscan step functionality

This method creates a temporary directory, writes a package-lock.json file with predefined content,
changes the current working directory to the temporary directory, instantiates and runs the
ScanDepscan step, and verifies the result.

Args:
None

Returns:
None

Raises:
AssertionError: If the sbom_vdr_values is None or empty, or if any other assertion fails
"""
inputs = {}
# String content to be written to the package.lock file
package_lock_content = """{
Expand Down
Loading
Loading