Skip to content

Commit

Permalink
feat: final update to RAG generation
Browse files Browse the repository at this point in the history
  • Loading branch information
sauravpanda committed Aug 16, 2024
1 parent d5ebb47 commit ad403d9
Show file tree
Hide file tree
Showing 22 changed files with 1,298 additions and 630 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,5 @@ cython_debug/
node_modules
.next

.cloudcode
.cloudcode
tree_sitter_languages/
76 changes: 53 additions & 23 deletions .kaizen/unit_test/kaizen/helpers/test_create_folder.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,44 @@
# Mock logger
logger = mock.Mock()


@pytest.fixture
def mock_os_path_exists():
with mock.patch('os.path.exists') as mock_exists:
with mock.patch("os.path.exists") as mock_exists:
yield mock_exists


@pytest.fixture
def mock_os_makedirs():
with mock.patch('os.makedirs') as mock_makedirs:
with mock.patch("os.makedirs") as mock_makedirs:
yield mock_makedirs


@pytest.fixture
def mock_logger_debug():
with mock.patch('kaizen.helpers.output.logger.debug') as mock_debug:
with mock.patch("kaizen.helpers.output.logger.debug") as mock_debug:
yield mock_debug

def test_create_new_folder_when_not_exists(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):
folder_path = 'new_folder'

def test_create_new_folder_when_not_exists(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
folder_path = "new_folder"
mock_os_path_exists.return_value = False

create_folder(folder_path)

mock_os_path_exists.assert_called_once_with(folder_path)
mock_os_makedirs.assert_called_once_with(folder_path)
mock_logger_debug.assert_called_once_with(f"Folder '{folder_path}' created successfully.")
mock_logger_debug.assert_called_once_with(
f"Folder '{folder_path}' created successfully."
)


def test_do_nothing_when_folder_already_exists(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):
folder_path = 'existing_folder'
def test_do_nothing_when_folder_already_exists(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
folder_path = "existing_folder"
mock_os_path_exists.return_value = True

create_folder(folder_path)
Expand All @@ -41,52 +52,71 @@ def test_do_nothing_when_folder_already_exists(mock_os_path_exists, mock_os_make
mock_os_makedirs.assert_not_called()
mock_logger_debug.assert_called_once_with(f"Folder '{folder_path}' already exists.")


def test_raise_value_error_when_folder_path_is_empty():
with pytest.raises(ValueError, match="Folder path cannot be empty"):
create_folder('')
create_folder("")


def test_create_deeply_nested_folder(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):
folder_path = 'a/b/c/d/e/f/g'
def test_create_deeply_nested_folder(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
folder_path = "a/b/c/d/e/f/g"
mock_os_path_exists.return_value = False

create_folder(folder_path)

mock_os_path_exists.assert_called_once_with(folder_path)
mock_os_makedirs.assert_called_once_with(folder_path)
mock_logger_debug.assert_called_once_with(f"Folder '{folder_path}' created successfully.")
mock_logger_debug.assert_called_once_with(
f"Folder '{folder_path}' created successfully."
)


def test_create_folder_with_special_characters(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):
folder_path = 'folder_with_special_!@#$%^&*()'
def test_create_folder_with_special_characters(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
folder_path = "folder_with_special_!@#$%^&*()"
mock_os_path_exists.return_value = False

create_folder(folder_path)

mock_os_path_exists.assert_called_once_with(folder_path)
mock_os_makedirs.assert_called_once_with(folder_path)
mock_logger_debug.assert_called_once_with(f"Folder '{folder_path}' created successfully.")
mock_logger_debug.assert_called_once_with(
f"Folder '{folder_path}' created successfully."
)

def test_create_folder_with_max_path_length(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):

def test_create_folder_with_max_path_length(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
# Adjusting the max path length to a more typical value for modern filesystems
max_path_length = os.pathconf('/', 'PC_PATH_MAX')
folder_path = 'a' * max_path_length
max_path_length = os.pathconf("/", "PC_PATH_MAX")
folder_path = "a" * max_path_length
mock_os_path_exists.return_value = False

create_folder(folder_path)

mock_os_path_exists.assert_called_once_with(folder_path)
mock_os_makedirs.assert_called_once_with(folder_path)
mock_logger_debug.assert_called_once_with(f"Folder '{folder_path}' created successfully.")
mock_logger_debug.assert_called_once_with(
f"Folder '{folder_path}' created successfully."
)


def test_create_folder_with_invalid_characters(mock_os_path_exists, mock_os_makedirs, mock_logger_debug):
def test_create_folder_with_invalid_characters(
mock_os_path_exists, mock_os_makedirs, mock_logger_debug
):
# Assuming the filesystem does not allow characters like ':', '*', '?', '<', '>', '|'
invalid_characters = [':', '*', '?', '<', '>', '|']
invalid_characters = [":", "*", "?", "<", ">", "|"]
for char in invalid_characters:
folder_path = f'invalid{char}folder'
folder_path = f"invalid{char}folder"
mock_os_path_exists.return_value = False

with pytest.raises(OSError):
create_folder(folder_path)

mock_os_path_exists.assert_called_once_with(folder_path)
mock_os_makedirs.assert_not_called()
mock_logger_debug.assert_not_called()
mock_logger_debug.assert_not_called()
127 changes: 88 additions & 39 deletions .kaizen/unit_test/kaizen/helpers/test_create_pr_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,91 @@
import time
from kaizen.helpers.output import create_pr_description

DESC_COLLAPSIBLE_TEMPLATE = "<details><summary>Original Description</summary>\n\n{desc}\n\n</details>"
DESC_COLLAPSIBLE_TEMPLATE = (
"<details><summary>Original Description</summary>\n\n{desc}\n\n</details>"
)

@pytest.mark.parametrize("desc, original_desc, expected", [
# Normal Cases
("This is a PR description", "This is the original detailed description",
"This is a PR description\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis is the original detailed description\n\n</details>"),
("Fixes a bug", "This fixes a bug in the system",
"Fixes a bug\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis fixes a bug in the system\n\n</details>"),
# Edge Cases
("", "This is the original detailed description",
"\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis is the original detailed description\n\n</details>"),
("This is a PR description", "",
"This is a PR description\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>"),
("", "",
"\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>"),
("# Heading\n* Bullet", "**Bold**\n_Italic_",
"# Heading\n* Bullet\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n**Bold**\n_Italic_\n\n</details>"),
# Special Characters and HTML Tags
("<h1>Title</h1>", "<p>This is a <strong>bold</strong> statement</p>",
"<h1>Title</h1>\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n<p>This is a <strong>bold</strong> statement</p>\n\n</details>"),
("Special characters: !@#$%^&*()", "More special characters: ~`<>?",
"Special characters: !@#$%^&*()\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nMore special characters: ~`<>?\n\n</details>"),
])

@pytest.mark.parametrize(
"desc, original_desc, expected",
[
# Normal Cases
(
"This is a PR description",
"This is the original detailed description",
"This is a PR description\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis is the original detailed description\n\n</details>",
),
(
"Fixes a bug",
"This fixes a bug in the system",
"Fixes a bug\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis fixes a bug in the system\n\n</details>",
),
# Edge Cases
(
"",
"This is the original detailed description",
"\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nThis is the original detailed description\n\n</details>",
),
(
"This is a PR description",
"",
"This is a PR description\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>",
),
(
"",
"",
"\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>",
),
(
"# Heading\n* Bullet",
"**Bold**\n_Italic_",
"# Heading\n* Bullet\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n**Bold**\n_Italic_\n\n</details>",
),
# Special Characters and HTML Tags
(
"<h1>Title</h1>",
"<p>This is a <strong>bold</strong> statement</p>",
"<h1>Title</h1>\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n<p>This is a <strong>bold</strong> statement</p>\n\n</details>",
),
(
"Special characters: !@#$%^&*()",
"More special characters: ~`<>?",
"Special characters: !@#$%^&*()\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\nMore special characters: ~`<>?\n\n</details>",
),
],
)
def test_create_pr_description_normal_and_edge_cases(desc, original_desc, expected):
assert create_pr_description(desc, original_desc) == expected

@pytest.mark.parametrize("desc, original_desc, expected_error_message", [
# Error Handling
(None, "This is the original detailed description", "desc must be a string"),
(123, "This is the original detailed description", "desc must be a string"),
([], "This is the original detailed description", "desc must be a string"),
("This is a PR description", None, "original_desc must be a string"),
("This is a PR description", 123, "original_desc must be a string"),
("This is a PR description", [], "original_desc must be a string"),
])
def test_create_pr_description_error_handling(desc, original_desc, expected_error_message):

@pytest.mark.parametrize(
"desc, original_desc, expected_error_message",
[
# Error Handling
(None, "This is the original detailed description", "desc must be a string"),
(123, "This is the original detailed description", "desc must be a string"),
([], "This is the original detailed description", "desc must be a string"),
("This is a PR description", None, "original_desc must be a string"),
("This is a PR description", 123, "original_desc must be a string"),
("This is a PR description", [], "original_desc must be a string"),
],
)
def test_create_pr_description_error_handling(
desc, original_desc, expected_error_message
):
with pytest.raises(TypeError) as exc_info:
create_pr_description(desc, original_desc)
assert str(exc_info.value) == expected_error_message

@pytest.mark.parametrize("desc, original_desc", [
# Boundary Conditions
("a" * 10000, "b" * 10000),
("a" * 100000, "b" * 100000),
])

@pytest.mark.parametrize(
"desc, original_desc",
[
# Boundary Conditions
("a" * 10000, "b" * 10000),
("a" * 100000, "b" * 100000),
],
)
def test_create_pr_description_boundary_conditions(desc, original_desc):
start_time = time.time()
result = create_pr_description(desc, original_desc)
Expand All @@ -56,9 +96,18 @@ def test_create_pr_description_boundary_conditions(desc, original_desc):
assert result.startswith(desc)
assert result.endswith(DESC_COLLAPSIBLE_TEMPLATE.format(desc=original_desc))
assert "> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️" in result
assert len(result) == len(desc) + len(original_desc) + len("\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>") - 2
assert (
len(result)
== len(desc)
+ len(original_desc)
+ len(
"\n\n> ✨ Generated with love by [Kaizen](https://cloudcode.ai) ❤️\n\n<details><summary>Original Description</summary>\n\n\n\n</details>"
)
- 2
)
# Removed the arbitrary 1-second boundary condition
print(f"Execution time: {execution_time} seconds")


if __name__ == "__main__":
pytest.main()
pytest.main()
28 changes: 19 additions & 9 deletions .kaizen/unit_test/kaizen/helpers/test_create_pr_review_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
</details>
"""


@pytest.fixture
def setup_single_topic_single_review():
return {
Expand All @@ -32,6 +33,7 @@ def setup_single_topic_single_review():
]
}


@pytest.fixture
def setup_multiple_topics_multiple_reviews():
return {
Expand All @@ -55,7 +57,7 @@ def setup_multiple_topics_multiple_reviews():
"end_line": 40,
"file_name": "file2.py",
"severity_level": 7,
}
},
],
"topic2": [
{
Expand All @@ -68,14 +70,18 @@ def setup_multiple_topics_multiple_reviews():
"file_name": "file3.py",
"severity_level": 5,
}
]
],
}


def test_empty_topics():
topics = {}
expected_output = "## Code Review\n\n✅ **All Clear:** This PR is ready to merge! 👍\n\n"
expected_output = (
"## Code Review\n\n✅ **All Clear:** This PR is ready to merge! 👍\n\n"
)
assert create_pr_review_text(topics) == expected_output


def test_single_topic_single_review(setup_single_topic_single_review):
topics = setup_single_topic_single_review
expected_output = (
Expand All @@ -96,6 +102,7 @@ def test_single_topic_single_review(setup_single_topic_single_review):
)
assert create_pr_review_text(topics) == expected_output


def test_multiple_topics_multiple_reviews(setup_multiple_topics_multiple_reviews):
topics = setup_multiple_topics_multiple_reviews
expected_output = (
Expand Down Expand Up @@ -139,6 +146,7 @@ def test_multiple_topics_multiple_reviews(setup_multiple_topics_multiple_reviews
)
assert create_pr_review_text(topics) == expected_output


def test_reviews_with_missing_fields():
topics = {
"topic1": [
Expand Down Expand Up @@ -181,7 +189,7 @@ def test_reviews_with_missing_fields():
"end_line": 80,
"file_name": "final_test_file.py",
# Missing severity_level
}
},
]
}
expected_output = (
Expand Down Expand Up @@ -235,6 +243,7 @@ def test_reviews_with_missing_fields():
)
assert create_pr_review_text(topics) == expected_output


def test_reviews_with_missing_comment():
topics = {
"topic1": [
Expand Down Expand Up @@ -268,9 +277,10 @@ def test_reviews_with_missing_comment():
)
assert create_pr_review_text(topics) == expected_output


def test_empty_list_in_topics():
topics = {
"topic1": []
}
expected_output = "## Code Review\n\n✅ **All Clear:** This PR is ready to merge! 👍\n\n"
assert create_pr_review_text(topics) == expected_output
topics = {"topic1": []}
expected_output = (
"## Code Review\n\n✅ **All Clear:** This PR is ready to merge! 👍\n\n"
)
assert create_pr_review_text(topics) == expected_output
Loading

0 comments on commit ad403d9

Please sign in to comment.