Skip to content

test: add unit tests for branch_handler functions in test_branch_handler.py#176

Open
abuzarmahmood wants to merge 5 commits intomainfrom
173-add-tests-for-branch_handlerpy
Open

test: add unit tests for branch_handler functions in test_branch_handler.py#176
abuzarmahmood wants to merge 5 commits intomainfrom
173-add-tests-for-branch_handlerpy

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 issue is to add tests for the branch_handler.py file in the abuzarmahmood/blech_github_bot repository. The file contains utility functions for handling git branches related to issues. The last comment [ develop_issue ] suggests that the issue is still in the development phase.

  • Overview of Plan to Address the Issues:
    To address this issue, a new test file named test_branch_handler.py will be created. Unit tests for each function in the branch_handler.py file will be written using the unittest framework. The tests will cover the following functions: get_issue_related_branches, get_current_branch, checkout_branch, delete_branch, back_to_master_branch, and push_changes.

  • Specific Details of Changes to be Made:

    1. Create Test File

      • File: test_branch_handler.py
      • Description: Create a new test file to contain all the test cases for the functions in branch_handler.py.
    2. Test for get_issue_related_branches

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function correctly retrieves branches related to a specific GitHub issue.
      • Code snippet:
        import unittest
        from unittest.mock import patch, MagicMock
        from branch_handler import get_issue_related_branches
        
        class TestBranchHandler(unittest.TestCase):
            @patch('branch_handler.git.Repo')
            @patch('branch_handler.os.popen')
            def test_get_issue_related_branches(self, mock_popen, mock_repo):
                mock_issue = MagicMock()
                mock_issue.number = 123
                mock_issue.title = "Test Issue"
                mock_popen.return_value.read.return_value = "branch1\\turl1\\nbranch2\\turl2"
                
                branches = get_issue_related_branches('/path/to/repo', mock_issue)
                expected_branches = [('branch1', 'url1'), ('branch2', 'url2')]
                self.assertEqual(branches, expected_branches)
    3. Test for get_current_branch

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function returns the name of the current git branch.
      • Code snippet:
        @patch('branch_handler.git.Repo')
        def test_get_current_branch(self, mock_repo):
            mock_repo.return_value.active_branch.name = 'main'
            branch_name = get_current_branch('/path/to/repo')
            self.assertEqual(branch_name, 'main')
    4. Test for checkout_branch

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function checks out a git branch, optionally creating it.
      • Code snippet:
        @patch('branch_handler.git.Repo')
        def test_checkout_branch(self, mock_repo):
            mock_repo.return_value.heads = ['main', 'dev']
            checkout_branch('/path/to/repo', 'dev')
            mock_repo.return_value.git.checkout.assert_called_with('dev')
    5. Test for delete_branch

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function deletes a git branch.
      • Code snippet:
        @patch('branch_handler.git.Repo')
        def test_delete_branch(self, mock_repo):
            mock_repo.return_value.heads = ['main', 'dev']
            delete_branch('/path/to/repo', 'dev')
            mock_repo.return_value.delete_head.assert_called_with('dev', force=False)
    6. Test for back_to_master_branch

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function switches back to the master/main branch.
      • Code snippet:
        @patch('branch_handler.git.Repo')
        def test_back_to_master_branch(self, mock_repo):
            mock_repo.return_value.heads = ['main', 'dev']
            back_to_master_branch('/path/to/repo')
            mock_repo.return_value.git.checkout.assert_called_with('main')
    7. Test for push_changes

      • File: test_branch_handler.py
      • Description: Write a test case to verify that the function pushes local changes to the remote repository.
      • Code snippet:
        @patch('branch_handler.git.Repo')
        def test_push_changes(self, mock_repo):
            mock_repo.return_value.active_branch.name = 'dev'
            push_changes('/path/to/repo')
            mock_repo.return_value.git.push.assert_called_with('origin', 'dev')

By implementing these tests, the functions in branch_handler.py will be verified for correctness, helping maintain code quality and catch future regressions.


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/branch_handler.py
To implement the tests for the branch_handler.py file, we will create a new test file             
test_branch_handler.py and add unit tests for each function using the unittest framework. Here are
the changes:                                                                                      

tests/test_branch_handler.py                                                                      

                                                                                                  
<<<<<<< SEARCH                                                                                    
=======                                                                                           
import unittest                                                                                   
from unittest.mock import patch, MagicMock                                                        
from src.branch_handler import (                                                                  
    get_issue_related_branches,                                                                   
    get_current_branch,                                                                           
    checkout_branch,                                                                              
    delete_branch,                                                                                
    back_to_master_branch,                                                                        
    push_changes                                                                                  
)                                                                                                 
                                                                                                  
class TestBranchHandler(unittest.TestCase):                                                       
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    @patch('src.branch_handler.os.popen')                                                         
    def test_get_issue_related_branches(self, mock_popen, mock_repo):                             
        mock_issue = MagicMock()                                                                  
        mock_issue.number = 123                                                                   
        mock_issue.title = "Test Issue"                                                           
        mock_popen.return_value.read.return_value = "branch1\turl1\nbranch2\turl2"                
                                                                                                  
        branches = get_issue_related_branches('/path/to/repo', mock_issue)                        
        expected_branches = [('branch1', 'url1'), ('branch2', 'url2')]                            
        self.assertEqual(branches, expected_branches)                                             
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_get_current_branch(self, mock_repo):                                                 
        mock_repo.return_value.active_branch.name = 'main'                                        
        branch_name = get_current_branch('/path/to/repo')                                         
        self.assertEqual(branch_name, 'main')                                                     
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_checkout_branch(self, mock_repo):                                                    
        mock_repo.return_value.heads = ['main', 'dev']                                            
        checkout_branch('/path/to/repo', 'dev')                                                   
        mock_repo.return_value.git.checkout.assert_called_with('dev')                             
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_delete_branch(self, mock_repo):                                                      
        mock_repo.return_value.heads = ['main', 'dev']                                            
        delete_branch('/path/to/repo', 'dev')                                                     
        mock_repo.return_value.delete_head.assert_called_with('dev', force=False)                 
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_back_to_master_branch(self, mock_repo):                                              
        mock_repo.return_value.heads = ['main', 'dev']                                            
        back_to_master_branch('/path/to/repo')                                                    
        mock_repo.return_value.git.checkout.assert_called_with('main')                            
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_push_changes(self, mock_repo):                                                       
        mock_repo.return_value.active_branch.name = 'dev'                                         
        push_changes('/path/to/repo')                                                             
        mock_repo.return_value.git.push.assert_called_with('origin', 'dev')                       
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
>>>>>>> REPLACE                                                                                   
                                                                                                  

You can run the tests using the following command:                                                

                                                                                                  
python -m unittest discover -s tests -p "test_branch_handler.py"                                  
                                                                                                  

Tokens: 7.3k sent, 594 received. Cost: $0.02 message, $0.02 session.

tests/test_branch_handler.py
Applied edit to tests/test_branch_handler.py
Commit 9ef9b2a test: add unit tests for branch_handler functions in test_branch_handler.py

python -m unittest discover -s tests -p "test_branch_handler.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 (1062f9b).
Report is 4 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #176   +/-   ##
=======================================
  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/branch_handler.py

tests/test_branch_handler.py
Here are the changes to refactor the test suite from unittest to pytest:                          

tests/test_branch_handler.py                                                                      

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

tests/test_branch_handler.py                                                                      

                                                                                                  
<<<<<<< SEARCH                                                                                    
class TestBranchHandler(unittest.TestCase):                                                       
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    @patch('src.branch_handler.os.popen')                                                         
    def test_get_issue_related_branches(self, mock_popen, mock_repo):                             
        mock_issue = MagicMock()                                                                  
        mock_issue.number = 123                                                                   
        mock_issue.title = "Test Issue"                                                           
        mock_popen.return_value.read.return_value = "branch1\turl1\nbranch2\turl2"                
                                                                                                  
        branches = get_issue_related_branches('/path/to/repo', mock_issue)                        
        expected_branches = [('branch1', 'url1'), ('branch2', 'url2')]                            
        self.assertEqual(branches, expected_branches)                                             
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_get_current_branch(self, mock_repo):                                                 
        mock_repo.return_value.active_branch.name = 'main'                                        
        branch_name = get_current_branch('/path/to/repo')                                         
        self.assertEqual(branch_name, 'main')                                                     
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_checkout_branch(self, mock_repo):                                                    
        mock_repo.return_value.heads = ['main', 'dev']                                            
        checkout_branch('/path/to/repo', 'dev')                                                   
        mock_repo.return_value.git.checkout.assert_called_with('dev')                             
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_delete_branch(self, mock_repo):                                                      
        mock_repo.return_value.heads = ['main', 'dev']                                            
        delete_branch('/path/to/repo', 'dev')                                                     
        mock_repo.return_value.delete_head.assert_called_with(                                    
            'dev', force=False)                                                                   
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_back_to_master_branch(self, mock_repo):                                              
        mock_repo.return_value.heads = ['main', 'dev']                                            
        back_to_master_branch('/path/to/repo')                                                    
        mock_repo.return_value.git.checkout.assert_called_with('main')                            
                                                                                                  
    @patch('src.branch_handler.git.Repo')                                                         
    def test_push_changes(self, mock_repo):                                                       
        mock_repo.return_value.active_branch.name = 'dev'                                         
        push_changes('/path/to/repo')                                                             
        mock_repo.return_value.git.push.assert_called_with('origin', 'dev')                       
                                                                                                  
                                                                                                  
if __name__ == '__main__':                                                                        
    unittest.main()                                                                               
=======                                                                                           
@patch('src.branch_handler.git.Repo')                                                             
@patch('src.branch_handler.os.popen')                                                             
def test_get_issue_related_branches(mock_popen, mock_repo):                                       
    mock_issue = MagicMock()                                                                      
    mock_issue.number = 123                                                                       
    mock_issue.title = "Test Issue"                                                               
    mock_popen.return_value.read.return_value = "branch1\turl1\nbranch2\turl2"                    
                                                                                                  
    branches = get_issue_related_branches('/path/to/repo', mock_issue)                            
    expected_branches = [('branch1', 'url1'), ('branch2', 'url2')]                                
    assert branches == expected_branches                                                          
                                                                                                  
@patch('src.branch_handler.git.Repo')                                                             
def test_get_current_branch(mock_repo):                                                           
    mock_repo.return_value.active_branch.name = 'main'                                            
    branch_name = get_current_branch('/path/to/repo')                                             
    assert branch_name == 'main'                                                                  
                                                                                                  
@patch('src.branch_handler.git.Repo')                                                             
def test_checkout_branch(mock_repo):                                                              
    mock_repo.return_value.heads = ['main', 'dev']                                                
    checkout_branch('/path/to/repo', 'dev')                                                       
    mock_repo.return_value.git.checkout.assert_called_with('dev')                                 
                                                                                                  
@patch('src.branch_handler.git.Repo')                                                             
def test_delete_branch(mock_repo):                                                                
    mock_repo.return_value.heads = ['main', 'dev']                                                
    delete_branch('/path/to/repo', 'dev')                                                         
    mock_repo.return_value.delete_head.assert_called_with('dev', force=False)                     
                                                                                                  
@patch('src.branch_handler.git.Repo')                                                             
def test_back_to_master_branch(mock_repo):                                                        
    mock_repo.return_value.heads = ['main', 'dev']                                                
    back_to_master_branch('/path/to/repo')                                                        
    mock_repo.return_value.git.checkout.assert_called_with('main')                                
                                                                                                  
@patch('src.branch_handler.git.Repo')                                                             
def test_push_changes(mock_repo):                                                                 
    mock_repo.return_value.active_branch.name = 'dev'                                             
    push_changes('/path/to/repo')                                                                 
    mock_repo.return_value.git.push.assert_called_with('origin', 'dev')                           
>>>>>>> REPLACE                                                                                   
                                                                                                  

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

                                                                                                  
pytest tests/test_branch_handler.py                                                               
                                                                                                  

Tokens: 7.5k sent, 944 received. Cost: $0.03 message, $0.03 session.
Applied edit to tests/test_branch_handler.py
Commit d98288f refactor: convert test suite from unittest to pytest framework

pytest tests/test_branch_handler.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


requirements.txt

src/branch_handler.py

tests/test_branch_handler.py
To update the GitHub workflow to include the new tests for branch_handler.py, we need to modify   
the .github/workflows/test.yml file. Please add this file to the chat so I can provide the        
necessary SEARCH/REPLACE blocks.                                                                  

Tokens: 7.2k sent, 51 received. Cost: $0.02 message, $0.02 session.

.github/workflows/test.yml
To include the new tests for branch_handler.py in the GitHub workflow, we need to update the      
.github/workflows/test.yml file to run the tests/test_branch_handler.py file. Here is the         
necessary change:                                                                                 

.github/workflows/test.yml                                                                        

                                                                                                  
<<<<<<< SEARCH                                                                                    
    - name: Run tests with coverage                                                               
      run: |                                                                                      
        pytest tests/test_triggers.py -v --cov --cov-branch --cov-report=xml                      
=======                                                                                           
    - name: Run tests with coverage                                                               
      run: |                                                                                      
        pytest tests/test_triggers.py tests/test_branch_handler.py -v --cov --cov-branch          
--cov-report=xml                                                                                  
>>>>>>> REPLACE                                                                                   
                                                                                                  

This change will ensure that the tests in tests/test_branch_handler.py are executed as part of the
GitHub Actions workflow.                                                                          

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

tests/test_triggers.py
To ensure the tests for branch_handler.py are included in the GitHub Actions workflow, we need to 
update the .github/workflows/test.yml file. Here is the SEARCH/REPLACE block for the change:      

.github/workflows/test.yml                                                                        

                                                                                                  
<<<<<<< SEARCH                                                                                    
    - name: Run tests with coverage                                                               
      run: |                                                                                      
        pytest tests/test_triggers.py -v --cov --cov-branch --cov-report=xml                      
=======                                                                                           
    - name: Run tests with coverage                                                               
      run: |                                                                                      
        pytest tests/test_triggers.py tests/test_branch_handler.py -v --cov --cov-branch          
--cov-report=xml                                                                                  
>>>>>>> REPLACE                                                                                   
                                                                                                  

This update will ensure that both test_triggers.py and test_branch_handler.py are executed during 
the CI process.                                                                                   

Tokens: 9.2k sent, 163 received. Cost: $0.02 message, $0.06 session.
Applied edit to .github/workflows/test.yml
Commit 9dc1d1d ci: update workflow to include tests for branch_handler.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 branch_handler.py

1 participant