Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Auto-create unit tests for changes in PR #600 #628

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
90 changes: 90 additions & 0 deletions .kaizen/unit_test/.experiments/code_review/test_main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import os
import pytest
from unittest.mock import patch, MagicMock
from datetime import datetime
from tqdm import tqdm

# Assuming the main function is in a file named main.py
from main import main

@pytest.fixture
def mock_process_pr():
with patch('main.process_pr') as mock:
yield mock

@pytest.fixture
def mock_save_review():
with patch('main.save_review') as mock:
yield mock

@pytest.fixture
def mock_logger():
with patch('main.logger') as mock:
yield mock

def test_multiple_pr_urls(mock_process_pr, mock_save_review, mock_logger):
pr_urls = ['https://github.com/org/repo/pull/1', 'https://github.com/org/repo/pull/2']

mock_process_pr.return_value = ('review_desc', 'comments', 'issues', 'combined_diff_data')

with patch('os.makedirs'), \
patch('os.path.join', return_value='/mock/path'), \
patch('datetime.datetime') as mock_datetime:

mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)

main(pr_urls)

assert mock_process_pr.call_count == 2
assert mock_save_review.call_count == 2
mock_logger.info.assert_called_with("All PRs processed successfully")
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment: Potential for sensitive data exposure in test logs.

Solution: Ensure that sensitive data is not logged during tests.
!! Make sure the following suggestion is correct before committing it !!

Suggested change
mock_logger.info.assert_called_with("All PRs processed successfully")
mock_logger.info.assert_called_with("All PRs processed successfully") # Ensure no sensitive data is included


def test_empty_pr_urls(mock_process_pr, mock_save_review, mock_logger):
pr_urls = []

with patch('os.makedirs'), \
patch('os.path.join', return_value='/mock/path'), \
patch('datetime.datetime') as mock_datetime:

mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)

main(pr_urls)

mock_process_pr.assert_not_called()
mock_save_review.assert_not_called()
mock_logger.info.assert_called_with("All PRs processed successfully")

def test_process_pr_exception(mock_process_pr, mock_save_review, mock_logger):
pr_urls = ['https://github.com/org/repo/pull/1']

mock_process_pr.side_effect = Exception("Simulated error")

with patch('os.makedirs'), \
patch('os.path.join', return_value='/mock/path'), \
patch('datetime.datetime') as mock_datetime, \
pytest.raises(Exception):

mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)

main(pr_urls)

mock_process_pr.assert_called_once()
mock_save_review.assert_not_called()
mock_logger.info.assert_not_called_with("All PRs processed successfully")

@pytest.mark.parametrize("reeval_response", [True, False])
def test_reeval_response_flag(mock_process_pr, mock_save_review, mock_logger, reeval_response):
pr_urls = ['https://github.com/org/repo/pull/1']

mock_process_pr.return_value = ('review_desc', 'comments', 'issues', 'combined_diff_data')

with patch('os.makedirs'), \
patch('os.path.join', return_value='/mock/path'), \
patch('datetime.datetime') as mock_datetime:

mock_datetime.now.return_value = datetime(2023, 1, 1, 12, 0, 0)

main(pr_urls)

mock_process_pr.assert_called_once_with(pr_urls[0], reeval_response=False)
mock_save_review.assert_called_once()
103 changes: 103 additions & 0 deletions .kaizen/unit_test/.experiments/code_review/test_process_pr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import pytest
from unittest.mock import patch, MagicMock
from kaizen.reviewer.code_review import CodeReviewer
from kaizen.llms.provider import LLMProvider
from kaizen.formatters.code_review_formatter import create_pr_review_text
from github_app.github_helper.utils import get_diff_text, get_pr_files
from github_app.github_helper.pull_requests import create_review_comments
from .experiments.code_review.main import process_pr

@pytest.fixture
def mock_dependencies():
with patch('kaizen.reviewer.code_review.CodeReviewer') as mock_reviewer, \
patch('kaizen.llms.provider.LLMProvider') as mock_llm_provider, \
patch('github_app.github_helper.utils.get_diff_text') as mock_get_diff_text, \
patch('github_app.github_helper.utils.get_pr_files') as mock_get_pr_files, \
patch('github_app.github_helper.pull_requests.create_review_comments') as mock_create_review_comments, \
patch('kaizen.formatters.code_review_formatter.create_pr_review_text') as mock_create_pr_review_text:

yield {
'reviewer': mock_reviewer,
'llm_provider': mock_llm_provider,
'get_diff_text': mock_get_diff_text,
'get_pr_files': mock_get_pr_files,
'create_review_comments': mock_create_review_comments,
'create_pr_review_text': mock_create_pr_review_text
}

def test_process_pr_normal_case(mock_dependencies):
# Arrange
pr_url = "https://github.com/org/repo/pull/123"
mock_diff_text = "Sample diff text"
mock_pr_files = [{"filename": "file1.py", "patch": "Sample patch"}]
mock_review_data = MagicMock(
topics={"important": ["Topic 1", "Topic 2"]},
code_quality="Good",
model_name="gpt-4",
usage={"prompt_tokens": 100, "completion_tokens": 50},
issues=["Issue 1", "Issue 2"]
)

mock_dependencies['get_diff_text'].return_value = mock_diff_text
mock_dependencies['get_pr_files'].return_value = mock_pr_files
mock_dependencies['reviewer'].return_value.review_pull_request.return_value = mock_review_data
mock_dependencies['create_review_comments'].return_value = (["Comment 1"], ["Topic 1", "Topic 2"])
mock_dependencies['create_pr_review_text'].return_value = "Sample review text"

# Act
review_desc, comments, issues, combined_diff_data = process_pr(pr_url)

# Assert
assert "PR URL: https://github.com/org/repo/pull/123" in review_desc
assert "Sample review text" in review_desc
assert "Cost Usage (gpt-4)" in review_desc
assert comments == ["Comment 1"]
assert issues == ["Issue 1", "Issue 2"]
assert "File Name: file1.py" in combined_diff_data
assert "Patch Details: Sample patch" in combined_diff_data

def test_process_pr_empty_files(mock_dependencies):
# Arrange
pr_url = "https://github.com/org/repo/pull/124"
mock_diff_text = "Sample diff text"
mock_pr_files = []
mock_review_data = MagicMock(
topics={},
code_quality="N/A",
model_name="gpt-3.5-turbo",
usage={"prompt_tokens": 50, "completion_tokens": 25},
issues=[]
)

mock_dependencies['get_diff_text'].return_value = mock_diff_text
mock_dependencies['get_pr_files'].return_value = mock_pr_files
mock_dependencies['reviewer'].return_value.review_pull_request.return_value = mock_review_data
mock_dependencies['create_review_comments'].return_value = ([], [])
mock_dependencies['create_pr_review_text'].return_value = "No changes found"

# Act
review_desc, comments, issues, combined_diff_data = process_pr(pr_url)

# Assert
assert "PR URL: https://github.com/org/repo/pull/124" in review_desc
assert "No changes found" in review_desc
assert "Cost Usage (gpt-3.5-turbo)" in review_desc
assert comments == []
assert issues == []
assert combined_diff_data == ""

@pytest.mark.parametrize("exception_class", [ValueError, ConnectionError, Exception])
def test_process_pr_invalid_url(mock_dependencies, exception_class):
# Arrange
pr_url = "https://invalid-url.com/pr/123"
mock_dependencies['get_diff_text'].side_effect = exception_class("Error fetching diff")

# Act & Assert
with pytest.raises(exception_class):
process_pr(pr_url)

# Verify that the function attempts to get the diff text
mock_dependencies['get_diff_text'].assert_called_once()
# Verify that no further processing occurs after the exception
mock_dependencies['get_pr_files'].assert_not_called()
mock_dependencies['reviewer'].return_value.review_pull_request.assert_not_called()
103 changes: 103 additions & 0 deletions .kaizen/unit_test/.experiments/code_review/test_save_review.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import os
import json
import pytest
from unittest import mock
from unittest.mock import patch

# Assuming the save_review function is imported from the specified path
from .experiments.code_review.main import save_review

@pytest.fixture
def setup_folder(tmp_path):
# Create a temporary directory for testing
return tmp_path

def test_save_review_valid_data(setup_folder):
pr_number = 123
review_desc = "This is a review description."
comments = [{"line": 10, "comment": "Looks good!"}]
issues = [{"issue": "Variable not used", "line": 15}]
folder = setup_folder
combined_diff_data = "diff --git a/file.txt b/file.txt"

save_review(pr_number, review_desc, comments, issues, str(folder), combined_diff_data)

# Verify files were created with correct content
review_file = os.path.join(folder, f"pr_{pr_number}", "review.md")
comments_file = os.path.join(folder, f"pr_{pr_number}", "comments.json")
issues_file = os.path.join(folder, f"pr_{pr_number}", "issues.json")
combined_diff = os.path.join(folder, f"pr_{pr_number}", "combined_diff.txt")

assert os.path.exists(review_file)
assert os.path.exists(comments_file)
assert os.path.exists(issues_file)
assert os.path.exists(combined_diff)

with open(review_file, "r") as f:
assert f.read() == review_desc

with open(comments_file, "r") as f:
assert json.load(f) == comments

with open(issues_file, "r") as f:
assert json.load(f) == issues

with open(combined_diff, "r") as f:
assert f.read() == combined_diff_data

def test_save_review_empty_data(setup_folder):
pr_number = 456
review_desc = ""
comments = []
issues = []
folder = setup_folder
combined_diff_data = ""

save_review(pr_number, review_desc, comments, issues, str(folder), combined_diff_data)

# Verify files were created with correct content
review_file = os.path.join(folder, f"pr_{pr_number}", "review.md")
comments_file = os.path.join(folder, f"pr_{pr_number}", "comments.json")
issues_file = os.path.join(folder, f"pr_{pr_number}", "issues.json")
combined_diff = os.path.join(folder, f"pr_{pr_number}", "combined_diff.txt")

assert os.path.exists(review_file)
assert os.path.exists(comments_file)
assert os.path.exists(issues_file)
assert os.path.exists(combined_diff)

with open(review_file, "r") as f:
assert f.read() == review_desc

with open(comments_file, "r") as f:
assert json.load(f) == comments

with open(issues_file, "r") as f:
assert json.load(f) == issues

with open(combined_diff, "r") as f:
assert f.read() == combined_diff_data

def test_save_review_empty_folder_path():
pr_number = 789
review_desc = "Review description"
comments = [{"line": 20, "comment": "Needs improvement"}]
issues = [{"issue": "Syntax error", "line": 25}]
folder = ""
combined_diff_data = "diff --git a/file2.txt b/file2.txt"

with pytest.raises(FileNotFoundError):
save_review(pr_number, review_desc, comments, issues, folder, combined_diff_data)

@patch("os.makedirs")
def test_save_review_invalid_folder_path(mock_makedirs):
mock_makedirs.side_effect = OSError("Invalid folder path")
pr_number = 101
review_desc = "Another review description"
comments = [{"line": 30, "comment": "Check this"}]
issues = [{"issue": "Deprecated function", "line": 35}]
folder = "/invalid/folder/path"
combined_diff_data = "diff --git a/file3.txt b/file3.txt"

with pytest.raises(OSError, match="Invalid folder path"):
save_review(pr_number, review_desc, comments, issues, folder, combined_diff_data)
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# test_llm_provider.py

import pytest
from unittest.mock import patch, MagicMock
from kaizen.llms.provider import LLMProvider
from kaizen.utils.config import ConfigData
from litellm import Router
import os


@pytest.fixture
def mock_config_data():
return {
"language_model": {
"models": [
{"model_name": "default", "litellm_params": {"model": "gpt-4o-mini"}}
],
"redis_enabled": False,
"enable_observability_logging": False,
}
}


@pytest.fixture
def mock_litellm():
with patch("kaizen.llms.provider.litellm") as mock:
mock.token_counter.return_value = 100
mock.get_max_tokens.return_value = 4000
mock.cost_per_token.return_value = (0.01, 0.02)
yield mock


@pytest.fixture
def llm_provider(mock_config_data, mock_litellm):
with patch.object(ConfigData, "get_config_data", return_value=mock_config_data):
return LLMProvider()


def test_initialization(llm_provider):
assert llm_provider.system_prompt is not None
assert llm_provider.model_config == {"model": "gpt-4o-mini"}
assert llm_provider.default_temperature == 0.3


def test_validate_config_correct_setup(llm_provider):
assert llm_provider.models[0]["model_name"] == "default"


def test_validate_config_missing_language_model():
with patch.object(ConfigData, "get_config_data", return_value={}):
with pytest.raises(
ValueError, match="Missing 'language_model' in configuration"
):
LLMProvider()


def test_token_limit_check_with_valid_prompt(llm_provider, mock_litellm):
assert llm_provider.is_inside_token_limit("Test prompt") is True


def test_available_tokens_calculation(llm_provider, mock_litellm):
assert llm_provider.available_tokens("Test message") == 3200


def test_usage_cost_calculation(llm_provider, mock_litellm):
total_usage = {"prompt_tokens": 100, "completion_tokens": 200}
cost = llm_provider.get_usage_cost(total_usage)
assert cost == (0.01, 0.02)


def test_setup_redis_missing_env_vars():
with patch.dict(os.environ, {}, clear=True):
with patch.object(
ConfigData,
"get_config_data",
return_value={"language_model": {"redis_enabled": True}},
):
with pytest.raises(
ValueError,
match="Redis is enabled but REDIS_HOST or REDIS_PORT environment variables are missing",
):
LLMProvider()


def test_token_limit_check_boundary_condition(llm_provider, mock_litellm):
mock_litellm.token_counter.return_value = 3200
assert (
llm_provider.is_inside_token_limit("Boundary test prompt", percentage=0.8)
is True
)
Loading
Loading