Skip to content

Commit

Permalink
Added tests for commenter
Browse files Browse the repository at this point in the history
  • Loading branch information
hrshdhgd committed Feb 9, 2024
1 parent 5998dcc commit 6d191d5
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/codergpt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@

coder = CoderGPT()


@click.group()
@click.option("-v", "--verbose", count=True)
@click.option("-q", "--quiet")
Expand All @@ -62,6 +63,7 @@ def main(verbose: int, quiet: bool):
if quiet:
logger.setLevel(level=logging.ERROR)


@main.command()
@path_argument
def inspect(path: Union[str, Path, TextIO]):
Expand All @@ -72,6 +74,7 @@ def inspect(path: Union[str, Path, TextIO]):
"""
coder.inspect_package(path=path)


@main.command()
@path_argument
@click.option("-f", "--function", help="Function name to explain.")
Expand All @@ -94,6 +97,7 @@ def explain(path: Union[str, Path], function: str, classname: str):
else:
raise ValueError("The path provided is not a file.")


@main.command("comment")
@path_argument
@overwrite_option
Expand All @@ -114,5 +118,6 @@ def add_comments(path: Union[str, Path], overwrite: bool = False):
else:
raise ValueError("The path provided is not a file.")


if __name__ == "__main__":
main()
4 changes: 4 additions & 0 deletions src/codergpt/commenter/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
"""Commenter module for the package."""

from .commenter import CodeCommenter

__all__ = ["CodeCommenter"]
66 changes: 66 additions & 0 deletions tests/test_commenter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
"""Test commenter."""

import os
import unittest
from unittest.mock import MagicMock

from codergpt.commenter import CodeCommenter

from tests.test_constants import TEST_INPUT_DIR


class TestCodeCommenter(unittest.TestCase):
"""Class for testing the CodeCommenter class."""

def setUp(self):
"""Set up the test case by creating a mock chain object and initializing the CodeCommenter instance."""
self.mock_chain = MagicMock()
self.commenter = CodeCommenter(chain=self.mock_chain)

def test_comment_with_overwrite(self):
"""Test the comment method with overwrite set to True."""
# Setup
code = "print('Hello, World!')"
filename = TEST_INPUT_DIR / "test.py"
expected_commented_code = "# This prints a message to the console\nprint('Hello, World!')"

# Configure the mock to return the expected commented code
self.mock_chain.invoke.return_value.content = expected_commented_code

# Act
self.commenter.comment(code=code, filename=filename, overwrite=True)

# Assert
self.mock_chain.invoke.assert_called_once()
with open(filename, "r") as f:
content = f.read()
self.assertEqual(content, expected_commented_code)

def test_comment_without_overwrite(self):
"""Test the comment method without overwrite set."""
# Setup
code = "print('Goodbye, World!')"
filename = TEST_INPUT_DIR / "test.py"
updated_filename = TEST_INPUT_DIR / "test_updated.py"
expected_commented_code = "# This prints a farewell message to the console\nprint('Goodbye, World!')"

# Configure the mock to return the expected commented code
self.mock_chain.invoke.return_value.content = expected_commented_code

# Act
self.commenter.comment(code=code, filename=filename)

# Assert
self.mock_chain.invoke.assert_called_once()
with open(updated_filename, "r") as f:
content = f.read()
self.assertEqual(content, expected_commented_code)

def tearDown(self):
"""Clean up created files after each test case."""
os.remove(TEST_INPUT_DIR / "test.py") if os.path.exists(TEST_INPUT_DIR / "test.py") else None
os.remove(TEST_INPUT_DIR / "test_updated.py") if os.path.exists(TEST_INPUT_DIR / "test_updated.py") else None


if __name__ == "__main__":
unittest.main()
7 changes: 7 additions & 0 deletions tests/test_constants.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
"""Test constants."""

from pathlib import Path

TEST_DIR = Path(__file__).resolve().parent
TEST_INPUT_DIR = TEST_DIR / "input"
TEST_OUTPUT_DIR = TEST_DIR / "output"

0 comments on commit 6d191d5

Please sign in to comment.