Skip to content

test: add unit tests for functions in bot_tools.py#178

Open
abuzarmahmood wants to merge 5 commits intomainfrom
172-add-tests-for-bot_toolspy
Open

test: add unit tests for functions in bot_tools.py#178
abuzarmahmood wants to merge 5 commits intomainfrom
172-add-tests-for-bot_toolspy

Conversation

@abuzarmahmood
Copy link
Owner

No description provided.

@abuzarmahmood abuzarmahmood linked an issue May 6, 2025 that may be closed by this pull request
@abuzarmahmood
Copy link
Owner Author

Generated edit command:

  • Summary of User's Issues and Requests:
    The user has requested the addition of tests for the bot_tools.py file in the repository abuzarmahmood/blech_github_bot. The last comment [ develop_issue ] suggests that the issue is still in the development phase, but no specific details are provided in the comment itself.

  • Overview of Plan to Address the Issues:

    1. Identify the bot_tools.py file and its functions that require testing.
    2. Create a new test file named test_bot_tools.py in the tests directory.
    3. Implement unit tests for each function in bot_tools.py using a testing framework like unittest or pytest.
    4. Ensure that the tests cover various scenarios, including edge cases and error handling.
  • Specific Details of Changes to be Made:

    1. Create Test File:

      • File: tests/test_bot_tools.py
      • Description: Create a new test file to house the unit tests for bot_tools.py.
    2. Add Unit Tests for get_local_repo_path:

      • File: tests/test_bot_tools.py
      • Description: Test the function's ability to correctly construct the path to a local repository and handle cases where the repository does not exist.
      • Code snippet:
        import unittest
        from src.bot_tools import get_local_repo_path
        
        class TestBotTools(unittest.TestCase):
            def test_get_local_repo_path_exists(self):
                # Assuming a known repo path for testing
                repo_name = "known_owner/known_repo"
                expected_path = "/home/exouser/Desktop/blech_github_bot/src/repos/known_owner/known_repo"
                self.assertEqual(get_local_repo_path(repo_name), expected_path)
        
            def test_get_local_repo_path_not_exists(self):
                repo_name = "unknown_owner/unknown_repo"
                expected_message = "Repository unknown_owner/unknown_repo not found @ /home/exouser/Desktop/blech_github_bot/src/repos/unknown_owner/unknown_repo"
                self.assertEqual(get_local_repo_path(repo_name), expected_message)
    3. Add Unit Tests for search_for_pattern:

      • File: tests/test_bot_tools.py
      • Description: Test the function's ability to search for a pattern in a directory and return the correct file paths.
      • Code snippet:
        def test_search_for_pattern(self):
            search_dir = "/home/exouser/Desktop/blech_github_bot/src"
            pattern = "def "
            result = search_for_pattern(search_dir, pattern)
            self.assertIn("bot_tools.py", result)
    4. Add Unit Tests for search_for_file:

      • File: tests/test_bot_tools.py
      • Description: Test the function's ability to find a file in a directory and handle cases where the file is not found.
      • Code snippet:
        def test_search_for_file_exists(self):
            directory = "/home/exouser/Desktop/blech_github_bot/src"
            filename = "bot_tools.py"
            result = search_for_file(directory, filename)
            self.assertIn("bot_tools.py", result)
        
        def test_search_for_file_not_exists(self):
            directory = "/home/exouser/Desktop/blech_github_bot/src"
            filename = "non_existent_file.py"
            result = search_for_file(directory, filename)
            self.assertEqual(result, "File not found")
    5. Add Unit Tests for estimate_tokens:

      • File: tests/test_bot_tools.py
      • Description: Test the function's ability to estimate the number of tokens in a given text.
      • Code snippet:
        def test_estimate_tokens(self):
            text = "This is a test sentence."
            self.assertEqual(estimate_tokens(text), 5)
        
            empty_text = ""
            self.assertEqual(estimate_tokens(empty_text), 0)
    6. Add Unit Tests for readfile and readlines:

      • File: tests/test_bot_tools.py
      • Description: Test the functions' ability to read files and handle token thresholds.
      • Code snippet:
        def test_readfile(self):
            filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"
            content = readfile(filepath)
            self.assertIn("def get_local_repo_path", content)
        
        def test_readlines(self):
            filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"
            content = readlines(filepath, 0, 10)
            self.assertIn("def get_local_repo_path", content)

By implementing these tests, the functions in bot_tools.py will be verified for correct functionality and robustness against various scenarios, thereby improving the reliability and maintainability of the codebase.


This response was automatically generated by blech_bot using model gpt-4o

Aider output:

View Aider Output
Aider v0.82.3
Main model: gpt-4o with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 19 files
Repo-map: using 4096 tokens, auto refresh


src/bot_tools.py
Here is the implementation of the test file test_bot_tools.py with unit tests for the functions in
bot_tools.py:                                                                                     

tests/test_bot_tools.py                                                                           

                                                                                                  
<<<<<<< SEARCH                                                                                    
=======                                                                                           
import unittest                                                                                   
from src.bot_tools import (                                                                       
    get_local_repo_path,                                                                          
    search_for_pattern,                                                                           
    search_for_file,                                                                              
    estimate_tokens,                                                                              
    readfile,                                                                                     
    readlines                                                                                     
)                                                                                                 
                                                                                                  
class TestBotTools(unittest.TestCase):                                                            
                                                                                                  
    def test_get_local_repo_path_exists(self):                                                    
        # Assuming a known repo path for testing                                                  
        repo_name = "known_owner/known_repo"                                                      
        expected_path = "/home/exouser/Desktop/blech_github_bot/src/repos/known_owner/known_repo" 
        self.assertEqual(get_local_repo_path(repo_name), expected_path)                           
                                                                                                  
    def test_get_local_repo_path_not_exists(self):                                                
        repo_name = "unknown_owner/unknown_repo"                                                  
        expected_message = "Repository unknown_owner/unknown_repo not found @                     
/home/exouser/Desktop/blech_github_bot/src/repos/unknown_owner/unknown_repo"                      
        self.assertEqual(get_local_repo_path(repo_name), expected_message)                        
                                                                                                  
    def test_search_for_pattern(self):                                                            
        search_dir = "/home/exouser/Desktop/blech_github_bot/src"                                 
        pattern = "def "                                                                          
        result = search_for_pattern(search_dir, pattern)                                          
        self.assertIn("bot_tools.py", result)                                                     
                                                                                                  
    def test_search_for_file_exists(self):                                                        
        directory = "/home/exouser/Desktop/blech_github_bot/src"                                  
        filename = "bot_tools.py"                                                                 
        result = search_for_file(directory, filename)                                             
        self.assertIn("bot_tools.py", result)                                                     
                                                                                                  
    def test_search_for_file_not_exists(self):                                                    
        directory = "/home/exouser/Desktop/blech_github_bot/src"                                  
        filename = "non_existent_file.py"                                                         
        result = search_for_file(directory, filename)                                             
        self.assertEqual(result, "File not found")                                                
                                                                                                  
    def test_estimate_tokens(self):                                                               
        text = "This is a test sentence."                                                         
        self.assertEqual(estimate_tokens(text), 5)                                                
                                                                                                  
        empty_text = ""                                                                           
        self.assertEqual(estimate_tokens(empty_text), 0)                                          
                                                                                                  
    def test_readfile(self):                                                                      
        filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                      
        content = readfile(filepath)                                                              
        self.assertIn("def get_local_repo_path", content)                                         
                                                                                                  
    def test_readlines(self):                                                                     
        filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                      
        content = readlines(filepath, 0, 10)                                                      
        self.assertIn("def get_local_repo_path", content)                                         
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
>>>>>>> REPLACE                                                                                   
                                                                                                  

You can run the tests using the following command:                                                

                                                                                                  
python -m unittest tests/test_bot_tools.py                                                        
                                                                                                  

Tokens: 7.4k sent, 567 received. Cost: $0.02 message, $0.02 session.

tests/test_bot_tools.py
Applied edit to tests/test_bot_tools.py
Commit 788277c test: add unit tests for functions in bot_tools.py

python -m unittest tests/test_bot_tools.py

@codecov
Copy link

codecov bot commented May 6, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 41.79%. Comparing base (7344865) to head (64762f4).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #178   +/-   ##
=======================================
  Coverage   41.79%   41.79%           
=======================================
  Files           4        4           
  Lines         579      579           
  Branches       66       66           
=======================================
  Hits          242      242           
  Misses        335      335           
  Partials        2        2           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abuzarmahmood
Copy link
Owner Author

Refactor to use pytest

@abuzarmahmood
Copy link
Owner Author

Applied changes based on comment:

View Aider Output
──────────────────────────────────────────────────────────────────────────────────────────────────
Aider v0.82.3
Main model: gpt-4o with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 20 files
Repo-map: using 4096 tokens, auto refresh


src/bot_tools.py

tests/test_bot_tools.py
To refactor the tests to use pytest, we will convert the unittest test cases into pytest          
functions. Here are the necessary changes:                                                        

tests/test_bot_tools.py                                                                           

                                                                                                  
<<<<<<< SEARCH                                                                                    
import unittest                                                                                   
=======                                                                                           
import pytest                                                                                     
>>>>>>> REPLACE                                                                                   
                                                                                                  

tests/test_bot_tools.py                                                                           

                                                                                                  
<<<<<<< SEARCH                                                                                    
class TestBotTools(unittest.TestCase):                                                            
                                                                                                  
    def test_get_local_repo_path_exists(self):                                                    
        # Assuming a known repo path for testing                                                  
        repo_name = "known_owner/known_repo"                                                      
        expected_path = "/home/exouser/Desktop/blech_github_bot/src/repos/known_owner/known_repo" 
        self.assertEqual(get_local_repo_path(repo_name), expected_path)                           
                                                                                                  
    def test_get_local_repo_path_not_exists(self):                                                
        repo_name = "unknown_owner/unknown_repo"                                                  
        expected_message = "Repository unknown_owner/unknown_repo not found @                     
/home/exouser/Desktop/blech_github_bot/src/repos/unknown_owner/unknown_repo"                      
        self.assertEqual(get_local_repo_path(repo_name), expected_message)                        
                                                                                                  
    def test_search_for_pattern(self):                                                            
        search_dir = "/home/exouser/Desktop/blech_github_bot/src"                                 
        pattern = "def "                                                                          
        result = search_for_pattern(search_dir, pattern)                                          
        self.assertIn("bot_tools.py", result)                                                     
                                                                                                  
    def test_search_for_file_exists(self):                                                        
        directory = "/home/exouser/Desktop/blech_github_bot/src"                                  
        filename = "bot_tools.py"                                                                 
        result = search_for_file(directory, filename)                                             
        self.assertIn("bot_tools.py", result)                                                     
                                                                                                  
    def test_search_for_file_not_exists(self):                                                    
        directory = "/home/exouser/Desktop/blech_github_bot/src"                                  
        filename = "non_existent_file.py"                                                         
        result = search_for_file(directory, filename)                                             
        self.assertEqual(result, "File not found")                                                
                                                                                                  
    def test_estimate_tokens(self):                                                               
        text = "This is a test sentence."                                                         
        self.assertEqual(estimate_tokens(text), 5)                                                
                                                                                                  
        empty_text = ""                                                                           
        self.assertEqual(estimate_tokens(empty_text), 0)                                          
                                                                                                  
    def test_readfile(self):                                                                      
        filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                      
        content = readfile(filepath)                                                              
        self.assertIn("def get_local_repo_path", content)                                         
                                                                                                  
    def test_readlines(self):                                                                     
        filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                      
        content = readlines(filepath, 0, 10)                                                      
        self.assertIn("def get_local_repo_path", content)                                         
                                                                                                  
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
=======                                                                                           
def test_get_local_repo_path_exists():                                                            
    # Assuming a known repo path for testing                                                      
    repo_name = "known_owner/known_repo"                                                          
    expected_path = "/home/exouser/Desktop/blech_github_bot/src/repos/known_owner/known_repo"     
    assert get_local_repo_path(repo_name) == expected_path                                        
                                                                                                  
def test_get_local_repo_path_not_exists():                                                        
    repo_name = "unknown_owner/unknown_repo"                                                      
    expected_message = "Repository unknown_owner/unknown_repo not found @                         
/home/exouser/Desktop/blech_github_bot/src/repos/unknown_owner/unknown_repo"                      
    assert get_local_repo_path(repo_name) == expected_message                                     
                                                                                                  
def test_search_for_pattern():                                                                    
    search_dir = "/home/exouser/Desktop/blech_github_bot/src"                                     
    pattern = "def "                                                                              
    result = search_for_pattern(search_dir, pattern)                                              
    assert "bot_tools.py" in result                                                               
                                                                                                  
def test_search_for_file_exists():                                                                
    directory = "/home/exouser/Desktop/blech_github_bot/src"                                      
    filename = "bot_tools.py"                                                                     
    result = search_for_file(directory, filename)                                                 
    assert "bot_tools.py" in result                                                               
                                                                                                  
def test_search_for_file_not_exists():                                                            
    directory = "/home/exouser/Desktop/blech_github_bot/src"                                      
    filename = "non_existent_file.py"                                                             
    result = search_for_file(directory, filename)                                                 
    assert result == "File not found"                                                             
                                                                                                  
def test_estimate_tokens():                                                                       
    text = "This is a test sentence."                                                             
    assert estimate_tokens(text) == 5                                                             
                                                                                                  
    empty_text = ""                                                                               
    assert estimate_tokens(empty_text) == 0                                                       
                                                                                                  
def test_readfile():                                                                              
    filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                          
    content = readfile(filepath)                                                                  
    assert "def get_local_repo_path" in content                                                   
                                                                                                  
def test_readlines():                                                                             
    filepath = "/home/exouser/Desktop/blech_github_bot/src/bot_tools.py"                          
    content = readlines(filepath, 0, 10)                                                          
    assert "def get_local_repo_path" in content                                                   
>>>>>>> REPLACE                                                                                   
                                                                                                  

You can now run the tests using pytest with the following command:                                

                                                                                                  
pytest tests/test_bot_tools.py                                                                    
                                                                                                  

Tokens: 7.4k sent, 980 received. Cost: $0.03 message, $0.03 session.
Applied edit to tests/test_bot_tools.py
Commit cb92f51 refactor: convert tests in test_bot_tools.py from unittest to pytest

pytest tests/test_bot_tools.py


This response was automatically generated by blech_bot using model gpt-4o

@abuzarmahmood
Copy link
Owner Author

Update GitHub workflow yml to include new tests

@abuzarmahmood
Copy link
Owner Author

Applied changes based on comment:

View Aider Output
──────────────────────────────────────────────────────────────────────────────────────────────────
Aider v0.82.3
Main model: gpt-4o with diff edit format
Weak model: gpt-4o-mini
Git repo: .git with 20 files
Repo-map: using 4096 tokens, auto refresh


.github/workflows/test.yml

src/bot_tools.py

tests/test_bot_tools.py

tests/test_triggers.py
To include the new tests for bot_tools.py in the GitHub workflow, we need to modify the           
.github/workflows/test.yml file to run the tests/test_bot_tools.py file along with the existing   
tests. Here is the required change:                                                               

.github/workflows/test.yml                                                                        

                                                                                                  
<<<<<<< SEARCH                                                                                    
      run: |                                                                                      
        pytest tests/test_triggers.py -v --cov --cov-branch --cov-report=xml                      
=======                                                                                           
      run: |                                                                                      
        pytest tests/test_triggers.py tests/test_bot_tools.py -v --cov --cov-branch               
--cov-report=xml                                                                                  
>>>>>>> REPLACE                                                                                   
                                                                                                  

This change will ensure that the tests for bot_tools.py are executed as part of the CI/CD process.

Tokens: 8.8k sent, 149 received. Cost: $0.02 message, $0.02 session.
Applied edit to .github/workflows/test.yml
Commit 9982767 ci: update GitHub workflow to include tests for bot_tools.py


This response was automatically generated by blech_bot using model gpt-4o

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add tests for bot_tools.py

1 participant