Skip to content

test: add unit tests for functions in agents.py#179

Open
abuzarmahmood wants to merge 5 commits intomainfrom
171-add-tests-for-agentspy
Open

test: add unit tests for functions in agents.py#179
abuzarmahmood wants to merge 5 commits intomainfrom
171-add-tests-for-agentspy

Conversation

@abuzarmahmood
Copy link
Copy Markdown
Owner

No description provided.

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

Generated edit command:

Summary of User's Issues and Requests

The issue involves adding tests for the agents.py file in the abuzarmahmood/blech_github_bot repository. The agents.py file is responsible for agent creation and configuration for the GitHub bot, including functions for registering tool functions with agents, creating user and assistant agents, and generating prompts for agents. The last comment suggests developing tests for this file.

Overview of Plan to Address the Issues

  1. File Identification:

    • File: /home/exouser/Desktop/blech_github_bot/src/repos/abuzarmahmood/blech_github_bot/src/agents.py
    • Description: This file handles agent creation and configuration for the GitHub bot.
  2. Create a New Test File:

    • File: test_agents.py (to be created in the tests directory)
    • Description: This file will contain unit tests for the functions in agents.py.
  3. Testing Framework:

    • Use a testing framework like unittest or pytest to structure the tests and cover various scenarios and edge cases for each function.

Specific Details of Changes to be Made

  1. Create Test File for agents.py:
    • File: /home/exouser/Desktop/blech_github_bot/src/repos/abuzarmahmood/blech_github_bot/tests/test_agents.py
    • Description: This file will contain unit tests for the functions in agents.py.
    • Code snippet of edits:
      import unittest
      from src.agents import register_functions, create_user_agent, create_agent, parse_comments, generate_prompt
      from autogen import ConversableAgent, AssistantAgent, UserProxyAgent
      from github.Issue import Issue
      from unittest.mock import MagicMock
      
      class TestAgents(unittest.TestCase):
      
          def test_register_functions(self):
              agent = MagicMock(spec=ConversableAgent)
              registered_agent = register_functions(agent)
              self.assertIsInstance(registered_agent, ConversableAgent)
      
          def test_create_user_agent(self):
              user_agent = create_user_agent()
              self.assertIsInstance(user_agent, UserProxyAgent)
      
          def test_create_agent(self):
              agent_name = "edit_assistant"
              llm_config = {"key": "value"}
              agent = create_agent(agent_name, llm_config)
              self.assertIsInstance(agent, AssistantAgent)
      
          def test_parse_comments(self):
              repo_name = "test_repo"
              repo_path = "/path/to/repo"
              details = {}
              issue = MagicMock(spec=Issue)
              last_comment_str, comments_str, all_comments = parse_comments(repo_name, repo_path, details, issue)
              self.assertIsInstance(last_comment_str, str)
              self.assertIsInstance(comments_str, str)
              self.assertIsInstance(all_comments, list)
      
          def test_generate_prompt(self):
              agent_name = "edit_assistant"
              repo_name = "test_repo"
              repo_path = "/path/to/repo"
              details = {"title": "Test Title", "body": "Test Body"}
              issue = MagicMock(spec=Issue)
              prompt = generate_prompt(agent_name, repo_name, repo_path, details, issue)
              self.assertIsInstance(prompt, str)
      
      if __name__ == '__main__':
          unittest.main()

This test file will ensure that the functions in agents.py are working as expected and will help in maintaining the integrity of the codebase as changes are made in the future.


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/agents.py
Here is the SEARCH/REPLACE block to create the new test file test_agents.py:                      

tests/test_agents.py                                                                              

                                                                                                  
<<<<<<< SEARCH                                                                                    
=======                                                                                           
import unittest                                                                                   
from src.agents import register_functions, create_user_agent, create_agent, parse_comments,       
generate_prompt                                                                                   
from autogen import ConversableAgent, AssistantAgent, UserProxyAgent                              
from github.Issue import Issue                                                                    
from unittest.mock import MagicMock                                                               
                                                                                                  
class TestAgents(unittest.TestCase):                                                              
                                                                                                  
    def test_register_functions(self):                                                            
        agent = MagicMock(spec=ConversableAgent)                                                  
        registered_agent = register_functions(agent)                                              
        self.assertIsInstance(registered_agent, ConversableAgent)                                 
                                                                                                  
    def test_create_user_agent(self):                                                             
        user_agent = create_user_agent()                                                          
        self.assertIsInstance(user_agent, UserProxyAgent)                                         
                                                                                                  
    def test_create_agent(self):                                                                  
        agent_name = "edit_assistant"                                                             
        llm_config = {"key": "value"}                                                             
        agent = create_agent(agent_name, llm_config)                                              
        self.assertIsInstance(agent, AssistantAgent)                                              
                                                                                                  
    def test_parse_comments(self):                                                                
        repo_name = "test_repo"                                                                   
        repo_path = "/path/to/repo"                                                               
        details = {}                                                                              
        issue = MagicMock(spec=Issue)                                                             
        last_comment_str, comments_str, all_comments = parse_comments(repo_name, repo_path,       
details, issue)                                                                                   
        self.assertIsInstance(last_comment_str, str)                                              
        self.assertIsInstance(comments_str, str)                                                  
        self.assertIsInstance(all_comments, list)                                                 
                                                                                                  
    def test_generate_prompt(self):                                                               
        agent_name = "edit_assistant"                                                             
        repo_name = "test_repo"                                                                   
        repo_path = "/path/to/repo"                                                               
        details = {"title": "Test Title", "body": "Test Body"}                                    
        issue = MagicMock(spec=Issue)                                                             
        prompt = generate_prompt(agent_name, repo_name, repo_path, details, issue)                
        self.assertIsInstance(prompt, str)                                                        
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
>>>>>>> REPLACE                                                                                   
                                                                                                  

To run the tests, you can use the following shell command:                                        

                                                                                                  
python -m unittest tests/test_agents.py                                                           
                                                                                                  

Tokens: 8.4k sent, 415 received. Cost: $0.03 message, $0.03 session.

tests/test_agents.py
Applied edit to tests/test_agents.py
Commit 89bfc65 test: add unit tests for functions in agents.py

python -m unittest tests/test_agents.py

@codecov
Copy link
Copy Markdown

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 (dc53b93).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #179   +/-   ##
=======================================
  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
Copy Markdown
Owner Author

Refactor to use pytest

@abuzarmahmood
Copy link
Copy Markdown
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/agents.py

tests/test_agents.py
Here are the changes to refactor the test file to use pytest instead of unittest:                 

tests/test_agents.py                                                                              

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

tests/test_agents.py                                                                              

                                                                                                  
<<<<<<< SEARCH                                                                                    
class TestAgents(unittest.TestCase):                                                              
                                                                                                  
    def test_register_functions(self):                                                            
        agent = MagicMock(spec=ConversableAgent)                                                  
        registered_agent = register_functions(agent)                                              
        self.assertIsInstance(registered_agent, ConversableAgent)                                 
                                                                                                  
    def test_create_user_agent(self):                                                             
        user_agent = create_user_agent()                                                          
        self.assertIsInstance(user_agent, UserProxyAgent)                                         
                                                                                                  
    def test_create_agent(self):                                                                  
        agent_name = "edit_assistant"                                                             
        llm_config = {"key": "value"}                                                             
        agent = create_agent(agent_name, llm_config)                                              
        self.assertIsInstance(agent, AssistantAgent)                                              
                                                                                                  
    def test_parse_comments(self):                                                                
        repo_name = "test_repo"                                                                   
        repo_path = "/path/to/repo"                                                               
        details = {}                                                                              
        issue = MagicMock(spec=Issue)                                                             
        last_comment_str, comments_str, all_comments = parse_comments(                            
            repo_name, repo_path, details, issue)                                                 
        self.assertIsInstance(last_comment_str, str)                                              
        self.assertIsInstance(comments_str, str)                                                  
        self.assertIsInstance(all_comments, list)                                                 
                                                                                                  
    def test_generate_prompt(self):                                                               
        agent_name = "edit_assistant"                                                             
        repo_name = "test_repo"                                                                   
        repo_path = "/path/to/repo"                                                               
        details = {"title": "Test Title", "body": "Test Body"}                                    
        issue = MagicMock(spec=Issue)                                                             
        prompt = generate_prompt(                                                                 
            agent_name, repo_name, repo_path, details, issue)                                     
        self.assertIsInstance(prompt, str)                                                        
                                                                                                  
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
=======                                                                                           
def test_register_functions():                                                                    
    agent = MagicMock(spec=ConversableAgent)                                                      
    registered_agent = register_functions(agent)                                                  
    assert isinstance(registered_agent, ConversableAgent)                                         
                                                                                                  
def test_create_user_agent():                                                                     
    user_agent = create_user_agent()                                                              
    assert isinstance(user_agent, UserProxyAgent)                                                 
                                                                                                  
def test_create_agent():                                                                          
    agent_name = "edit_assistant"                                                                 
    llm_config = {"key": "value"}                                                                 
    agent = create_agent(agent_name, llm_config)                                                  
    assert isinstance(agent, AssistantAgent)                                                      
                                                                                                  
def test_parse_comments():                                                                        
    repo_name = "test_repo"                                                                       
    repo_path = "/path/to/repo"                                                                   
    details = {}                                                                                  
    issue = MagicMock(spec=Issue)                                                                 
    last_comment_str, comments_str, all_comments = parse_comments(                                
        repo_name, repo_path, details, issue)                                                     
    assert isinstance(last_comment_str, str)                                                      
    assert isinstance(comments_str, str)                                                          
    assert isinstance(all_comments, list)                                                         
                                                                                                  
def test_generate_prompt():                                                                       
    agent_name = "edit_assistant"                                                                 
    repo_name = "test_repo"                                                                       
    repo_path = "/path/to/repo"                                                                   
    details = {"title": "Test Title", "body": "Test Body"}                                        
    issue = MagicMock(spec=Issue)                                                                 
    prompt = generate_prompt(                                                                     
        agent_name, repo_name, repo_path, details, issue)                                         
    assert isinstance(prompt, str)                                                                
>>>>>>> REPLACE                                                                                   
                                                                                                  

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

                                                                                                  
pytest tests/test_agents.py                                                                       
                                                                                                  

Tokens: 8.5k sent, 660 received. Cost: $0.03 message, $0.03 session.
Applied edit to tests/test_agents.py
Commit f188990 refactor: convert tests in test_agents.py from unittest to pytest

pytest tests/test_agents.py


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

@abuzarmahmood
Copy link
Copy Markdown
Owner Author

Update GitHub workflow yml to include new tests

@abuzarmahmood
Copy link
Copy Markdown
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/agents.py

tests/test_agents.py

tests/test_triggers.py
.github/workflows/test.yml                                                                        

                                                                                                  
<<<<<<< SEARCH                                                                                    
        pytest tests/test_triggers.py -v --cov --cov-branch --cov-report=xml                      
=======                                                                                           
        pytest tests/test_triggers.py tests/test_agents.py -v --cov --cov-branch --cov-report=xml 
>>>>>>> REPLACE                                                                                   
                                                                                                  

Tokens: 10k sent, 63 received. Cost: $0.03 message, $0.03 session.
Applied edit to .github/workflows/test.yml
Commit 2d85b25 ci: update workflow to include new tests for agents.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 agents.py

1 participant