Skip to content

Add tests for blech_data_transfer#15

Merged
abuzarmahmood merged 7 commits intomasterfrom
04-28-add_tests
Apr 28, 2025
Merged

Add tests for blech_data_transfer#15
abuzarmahmood merged 7 commits intomasterfrom
04-28-add_tests

Conversation

@abuzarmahmood
Copy link
Copy Markdown
Member

@abuzarmahmood abuzarmahmood commented Apr 28, 2025

Using pytest framework

Copy link
Copy Markdown
Member Author

abuzarmahmood commented Apr 28, 2025

@abuzarmahmood abuzarmahmood changed the title refactor(utils): move mount_katz_drive.sh to utils directory Add tests for blech_data_transfer Apr 28, 2025
@abuzarmahmood abuzarmahmood marked this pull request as ready for review April 28, 2025 15:29
@abuzarmahmood
Copy link
Copy Markdown
Member Author

Generated edit command:

  • Summary of User's Issues and Requests:
    The user has requested to add tests for the blech_data_transfer repository using the pytest framework. The focus is on ensuring comprehensive test coverage for the existing functionality.

  • Overview of Plan to Address the Issues:
    The plan involves reviewing the existing test files and ensuring that all functions in the blech_data_transfer module are adequately tested. This includes checking for any missing test cases and adding them where necessary. The existing test file test_blech_data_sentry.py already covers a wide range of functions, but we need to ensure completeness and correctness.

  • Specific Details of Changes to be Made:

    1. Add Missing Test Cases:

      • File: /home/exouser/Desktop/blech_github_bot/src/repos/katzlabbrandeis/blech_data_transfer/tests/test_blech_data_sentry.py
      • Description of Change: Review the test file to identify any functions from the blech_data_sentry module that are not covered by existing tests. Add test cases for any uncovered functions.
      • Code Snippet of Edits:
        def test_new_functionality():
            """Test description for new functionality"""
            # Setup test environment
            # Call the function
            # Assert expected outcomes
    2. Ensure Comprehensive Testing:

      • File: /home/exouser/Desktop/blech_github_bot/src/repos/katzlabbrandeis/blech_data_transfer/tests/test_blech_data_sentry.py
      • Description of Change: Ensure that all edge cases and potential failure points are tested. This includes testing with different input parameters and ensuring that exceptions are handled correctly.
      • Code Snippet of Edits:
        def test_edge_case_scenario():
            """Test edge case scenario"""
            # Setup edge case
            # Call the function
            # Assert expected outcomes
    3. Refactor Existing Tests for Clarity and Efficiency:

      • File: /home/exouser/Desktop/blech_github_bot/src/repos/katzlabbrandeis/blech_data_transfer/tests/test_blech_data_sentry.py
      • Description of Change: Refactor existing test cases to improve readability and efficiency. This may involve using fixtures more effectively or simplifying complex test logic.
      • Code Snippet of Edits:
        @pytest.fixture
        def setup_environment():
            """Setup common test environment"""
            # Setup code
            yield
            # Teardown code

By implementing these changes, the test suite will be more robust, ensuring that the blech_data_transfer module functions as expected under various conditions.


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

Aider output:

View Aider Output
The --sonnet flag is deprecated and will be removed in a future version. Please use --model sonnet instead.
You can skip this check with --no-gitignore
Added .aider* to .gitignore
Aider v0.82.2
Main model: anthropic/claude-3-7-sonnet-20250219 with diff edit format, infinite output
Weak model: anthropic/claude-3-5-haiku-20241022
Git repo: .git with 12 files
Repo-map: using 4096 tokens, auto refresh


tests/test_blech_data_sentry.py
I understand you want to add tests for the blech_data_transfer.py module. Looking at the existing test file for                    
blech_data_sentry.py, I'll create a new test file for blech_data_transfer.py following a similar pattern.                          

Let's create a new test file for the blech_data_transfer.py module:                                                                

tests/test_blech_data_transfer.py                                                                                                  

                                                                                                                                   
<<<<<<< SEARCH                                                                                                                     
=======                                                                                                                            
import pytest                                                                                                                      
import os                                                                                                                          
import pandas as pd                                                                                                                
import tempfile                                                                                                                    
import shutil                                                                                                                      
from unittest.mock import patch, MagicMock                                                                                         
from io import StringIO                                                                                                            
import sys                                                                                                                         
                                                                                                                                   
# Import the functions to test                                                                                                     
from src.blech_data_transfer import (                                                                                              
    parse_arguments,                                                                                                               
    get_data_folder,                                                                                                               
    initialize_dataset_handler,                                                                                                    
    check_experiment_existence,                                                                                                    
    validate_data_folder,                                                                                                          
    check_info_file,                                                                                                               
    select_user,                                                                                                                   
    select_subfolder,                                                                                                              
    prepare_file_transfer,                                                                                                         
    transfer_data,                                                                                                                 
    add_log_entry                                                                                                                  
)                                                                                                                                  
                                                                                                                                   
@pytest.fixture                                                                                                                    
def temp_dir():                                                                                                                    
    """Create a temporary directory for testing"""                                                                                 
    temp_dir = tempfile.mkdtemp()                                                                                                  
    yield temp_dir                                                                                                                 
    shutil.rmtree(temp_dir)                                                                                                        
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_data_folder(temp_dir):                                                                                                    
    """Create a mock data folder with necessary structure"""                                                                       
    # Create data folder structure                                                                                                 
    data_folder = os.path.join(temp_dir, 'test_data')                                                                              
    session_dir = os.path.join(data_folder, 'session1')                                                                            
    os.makedirs(session_dir, exist_ok=True)                                                                                        
                                                                                                                                   
    # Create info.rhd file                                                                                                         
    with open(os.path.join(session_dir, 'info.rhd'), 'w') as f:                                                                    
        f.write('test info file')                                                                                                  
                                                                                                                                   
    return data_folder                                                                                                             
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_server_path(temp_dir):                                                                                                    
    """Create a mock server path structure"""                                                                                      
    # Create local_only_files directory with server path file                                                                      
    local_only_dir = os.path.join(temp_dir, 'local_only_files')                                                                    
    os.makedirs(local_only_dir, exist_ok=True)                                                                                     
                                                                                                                                   
    # Create server path file                                                                                                      
    server_path = os.path.join(temp_dir, 'server')                                                                                 
    os.makedirs(server_path, exist_ok=True)                                                                                        
                                                                                                                                   
    with open(os.path.join(local_only_dir, 'blech_server_path.txt'), 'w') as f:                                                    
        f.write(server_path)                                                                                                       
                                                                                                                                   
    # Create data_management directory                                                                                             
    data_mgmt_dir = os.path.join(server_path, 'data_management')                                                                   
    os.makedirs(data_mgmt_dir, exist_ok=True)                                                                                      
                                                                                                                                   
    # Create users_list.txt                                                                                                        
    with open(os.path.join(server_path, 'users_list.txt'), 'w') as f:                                                              
        f.write('user1\nuser2\nuser3')                                                                                             
                                                                                                                                   
    # Create user directories                                                                                                      
    for user in ['user1', 'user2', 'user3']:                                                                                       
        user_dir = os.path.join(server_path, user)                                                                                 
        os.makedirs(user_dir, exist_ok=True)                                                                                       
        # Create some subfolders for user1                                                                                         
        if user == 'user1':                                                                                                        
            os.makedirs(os.path.join(user_dir, 'subfolder1'), exist_ok=True)                                                       
            os.makedirs(os.path.join(user_dir, 'subfolder2'), exist_ok=True)                                                       
                                                                                                                                   
    return temp_dir, server_path                                                                                                   
                                                                                                                                   
def test_parse_arguments():                                                                                                        
    """Test argument parsing with default values"""                                                                                
    with patch('sys.argv', ['blech_data_transfer.py']):                                                                            
        args = parse_arguments()                                                                                                   
        assert args.data_folder is None                                                                                            
                                                                                                                                   
    """Test argument parsing with data folder specified"""                                                                         
    with patch('sys.argv', ['blech_data_transfer.py', '--data_folder', '/test/path']):                                             
        args = parse_arguments()                                                                                                   
        assert args.data_folder == '/test/path'                                                                                    
                                                                                                                                   
def test_get_data_folder():                                                                                                        
    """Test getting data folder from arguments"""                                                                                  
    # Test with data folder in args                                                                                                
    args = MagicMock()                                                                                                             
    args.data_folder = '/test/path'                                                                                                
                                                                                                                                   
    with patch('builtins.input', return_value='/input/path'), \                                                                    
         patch('os.path.exists', return_value=True):                                                                               
        result = get_data_folder(args)                                                                                             
        assert result == '/test/path'                                                                                              
                                                                                                                                   
    # Test with no data folder in args, using input                                                                                
    args.data_folder = None                                                                                                        
                                                                                                                                   
    with patch('builtins.input', return_value='/input/path'), \                                                                    
         patch('os.path.exists', return_value=True):                                                                               
        result = get_data_folder(args)                                                                                             
        assert result == '/input/path'                                                                                             
                                                                                                                                   
    # Test with invalid path                                                                                                       
    with patch('builtins.input', return_value='/invalid/path'), \                                                                  
         patch('os.path.exists', return_value=False), \                                                                            
         patch('sys.exit') as mock_exit:                                                                                           
        get_data_folder(args)                                                                                                      
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_initialize_dataset_handler(mock_server_path):                                                                             
    """Test initializing dataset handler"""                                                                                        
    dir_path, _ = mock_server_path                                                                                                 
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        handler = initialize_dataset_handler(dir_path)                                                                             
                                                                                                                                   
    assert handler is not None                                                                                                     
    assert hasattr(handler, 'server_path')                                                                                         
    assert hasattr(handler, 'server_home_dir')                                                                                     
                                                                                                                                   
def test_check_experiment_existence(mock_server_path, mock_data_folder):                                                           
    """Test checking if experiment exists in dataset frame"""                                                                      
    dir_path, _ = mock_server_path                                                                                                 
                                                                                                                                   
    # Mock dataset handler                                                                                                         
    handler = MagicMock()                                                                                                          
    handler.check_experiment_exists.return_value = False                                                                           
                                                                                                                                   
    # Test when experiment doesn't exist                                                                                           
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = check_experiment_existence(handler, mock_data_folder)                                                             
                                                                                                                                   
    assert result is False                                                                                                         
    handler.check_experiment_exists.assert_called_once_with(mock_data_folder)                                                      
                                                                                                                                   
    # Test when experiment exists                                                                                                  
    handler.check_experiment_exists.return_value = True                                                                            
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', return_value='y'):                                                                                
        result = check_experiment_existence(handler, mock_data_folder)                                                             
                                                                                                                                   
    assert result is True                                                                                                          
                                                                                                                                   
def test_validate_data_folder(mock_data_folder):                                                                                   
    """Test validating data folder structure"""                                                                                    
    # Test with valid data folder                                                                                                  
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = validate_data_folder(mock_data_folder)                                                                            
                                                                                                                                   
    assert result is True                                                                                                          
                                                                                                                                   
    # Test with invalid data folder                                                                                                
    invalid_folder = os.path.dirname(mock_data_folder)                                                                             
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('sys.exit') as mock_exit:                                                                                           
        validate_data_folder(invalid_folder)                                                                                       
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_check_info_file(mock_data_folder):                                                                                        
    """Test checking for info.rhd file"""                                                                                          
    # Test with valid info file                                                                                                    
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = check_info_file(mock_data_folder)                                                                                 
                                                                                                                                   
    assert result == os.path.join(mock_data_folder, 'session1', 'info.rhd')                                                        
                                                                                                                                   
    # Test with no info file                                                                                                       
    no_info_folder = os.path.dirname(mock_data_folder)                                                                             
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('sys.exit') as mock_exit:                                                                                           
        check_info_file(no_info_folder)                                                                                            
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_select_user(mock_server_path):                                                                                            
    """Test selecting a user from users list"""                                                                                    
    _, server_path = mock_server_path                                                                                              
    users_list = ['user1', 'user2', 'user3']                                                                                       
                                                                                                                                   
    # Test with valid selection                                                                                                    
    with patch('builtins.input', return_value='1'):                                                                                
        result = select_user(users_list, server_path)                                                                              
                                                                                                                                   
    assert result == os.path.join(server_path, 'user1')                                                                            
                                                                                                                                   
    # Test with invalid selection then valid                                                                                       
    with patch('builtins.input', side_effect=['5', '2']):                                                                          
        result = select_user(users_list, server_path)                                                                              
                                                                                                                                   
    assert result == os.path.join(server_path, 'user2')                                                                            
                                                                                                                                   
def test_select_subfolder(mock_server_path):                                                                                       
    """Test selecting a subfolder from user directory"""                                                                           
    _, server_path = mock_server_path                                                                                              
    user_path = os.path.join(server_path, 'user1')                                                                                 
                                                                                                                                   
    # Test with valid selection                                                                                                    
    with patch('builtins.input', return_value='1'):                                                                                
        result = select_subfolder(user_path)                                                                                       
                                                                                                                                   
    assert result == os.path.join(user_path, 'subfolder1')                                                                         
                                                                                                                                   
    # Test with invalid selection then valid                                                                                       
    with patch('builtins.input', side_effect=['5', '2']):                                                                          
        result = select_subfolder(user_path)                                                                                       
                                                                                                                                   
    assert result == os.path.join(user_path, 'subfolder2')                                                                         
                                                                                                                                   
    # Test with creating new subfolder                                                                                             
    with patch('builtins.input', side_effect=['n', 'new_folder']):                                                                 
        result = select_subfolder(user_path)                                                                                       
                                                                                                                                   
    assert result == os.path.join(user_path, 'new_folder')                                                                         
    assert os.path.exists(os.path.join(user_path, 'new_folder'))                                                                   
                                                                                                                                   
def test_prepare_file_transfer(mock_data_folder, mock_server_path):                                                                
    """Test preparing file transfer lists"""                                                                                       
    _, server_path = mock_server_path                                                                                              
    copy_dir = os.path.join(server_path, 'user1', 'subfolder1')                                                                    
                                                                                                                                   
    # Ensure copy_dir exists                                                                                                       
    os.makedirs(copy_dir, exist_ok=True)                                                                                           
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        dir_list, rel_file_list = prepare_file_transfer(mock_data_folder, copy_dir)                                                
                                                                                                                                   
    assert isinstance(dir_list, list)                                                                                              
    assert isinstance(rel_file_list, list)                                                                                         
    assert len(dir_list) > 0                                                                                                       
    assert len(rel_file_list) > 0                                                                                                  
                                                                                                                                   
def test_transfer_data(mock_data_folder, mock_server_path):                                                                        
    """Test transferring data files"""                                                                                             
    _, server_path = mock_server_path                                                                                              
    server_data_folder = os.path.join(server_path, 'user1', 'subfolder1', os.path.basename(mock_data_folder))                      
                                                                                                                                   
    # Create directory lists for transfer                                                                                          
    dir_list = ['session1']                                                                                                        
    rel_file_list = ['session1/info.rhd']                                                                                          
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('shutil.copy2') as mock_copy:                                                                                       
        transfer_data(mock_data_folder, server_data_folder, dir_list, rel_file_list)                                               
                                                                                                                                   
    # Check if directories were created                                                                                            
    assert os.path.exists(os.path.join(server_data_folder, 'session1'))                                                            
                                                                                                                                   
    # Check if copy was called                                                                                                     
    mock_copy.assert_called()                                                                                                      
                                                                                                                                   
def test_add_log_entry(mock_server_path, mock_data_folder):                                                                        
    """Test adding log entry to dataset frame"""                                                                                   
    _, server_path = mock_server_path                                                                                              
                                                                                                                                   
    # Mock dataset handler                                                                                                         
    handler = MagicMock()                                                                                                          
    handler.add_entry = MagicMock()                                                                                                
    handler.sync_logs = MagicMock()                                                                                                
                                                                                                                                   
    users_list = ['user1', 'user2', 'user3']                                                                                       
    user = 'user1'                                                                                                                 
    server_data_folder = os.path.join(server_path, user, 'subfolder1', os.path.basename(mock_data_folder))                         
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        add_log_entry(handler, users_list, user, mock_data_folder, server_data_folder)                                             
                                                                                                                                   
    # Check if add_entry was called                                                                                                
    handler.add_entry.assert_called_once()                                                                                         
                                                                                                                                   
    # Check if sync_logs was called                                                                                                
    handler.sync_logs.assert_called_once()                                                                                         
>>>>>>> REPLACE                                                                                                                    
                                                                                                                                   

Now let's run the tests to verify our implementation:                                                                              

                                                                                                                                   
cd /path/to/your/project && python -m pytest tests/test_blech_data_transfer.py -v                                                  
                                                                                                                                   

Tokens: 6.9k sent, 2.3k received. Cost: $0.05 message, $0.05 session.

server_only_files/users_list.txt

src/blech_data_sentry.py

src/blech_data_transfer.py
I'll create a test file for blech_data_transfer.py based on the files you've provided. Looking at the code, I need to make some    
adjustments to my previous approach to match the actual implementation.                                                            

tests/test_blech_data_transfer.py                                                                                                  

                                                                                                                                   
<<<<<<< SEARCH                                                                                                                     
=======                                                                                                                            
import pytest                                                                                                                      
import os                                                                                                                          
import pandas as pd                                                                                                                
import tempfile                                                                                                                    
import shutil                                                                                                                      
from unittest.mock import patch, MagicMock                                                                                         
from io import StringIO                                                                                                            
import sys                                                                                                                         
                                                                                                                                   
# Import the dataset_handler module                                                                                                
from src.dataset_handler import DatasetFrameHandler, DatasetFrameLogger                                                            
                                                                                                                                   
@pytest.fixture                                                                                                                    
def temp_dir():                                                                                                                    
    """Create a temporary directory for testing"""                                                                                 
    temp_dir = tempfile.mkdtemp()                                                                                                  
    yield temp_dir                                                                                                                 
    shutil.rmtree(temp_dir)                                                                                                        
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_server_path(temp_dir):                                                                                                    
    """Create a mock server path structure"""                                                                                      
    # Create local_only_files directory with server path file                                                                      
    local_only_dir = os.path.join(temp_dir, 'local_only_files')                                                                    
    os.makedirs(local_only_dir, exist_ok=True)                                                                                     
                                                                                                                                   
    # Create server path file                                                                                                      
    server_path = os.path.join(temp_dir, 'server')                                                                                 
    os.makedirs(server_path, exist_ok=True)                                                                                        
                                                                                                                                   
    with open(os.path.join(local_only_dir, 'blech_server_path.txt'), 'w') as f:                                                    
        f.write(server_path)                                                                                                       
                                                                                                                                   
    # Create data_management directory                                                                                             
    data_mgmt_dir = os.path.join(server_path, 'data_management')                                                                   
    os.makedirs(data_mgmt_dir, exist_ok=True)                                                                                      
                                                                                                                                   
    # Create users_list.txt in data_management                                                                                     
    users_list_content = """Username,Directory,Email                                                                               
user1,user1_dir,user1@example.com                                                                                                  
user2,user2_dir,user2@example.com                                                                                                  
user3,user3_dir,user3@example.com"""                                                                                               
                                                                                                                                   
    with open(os.path.join(data_mgmt_dir, 'users_list.txt'), 'w') as f:                                                            
        f.write(users_list_content)                                                                                                
                                                                                                                                   
    # Create user directories                                                                                                      
    for user_dir in ['user1_dir', 'user2_dir', 'user3_dir']:                                                                       
        user_path = os.path.join(server_path, user_dir)                                                                            
        os.makedirs(user_path, exist_ok=True)                                                                                      
        # Create some subfolders for user1                                                                                         
        if user_dir == 'user1_dir':                                                                                                
            os.makedirs(os.path.join(user_path, 'subfolder1'), exist_ok=True)                                                      
            os.makedirs(os.path.join(user_path, 'subfolder2'), exist_ok=True)                                                      
                                                                                                                                   
    return temp_dir, server_path, data_mgmt_dir                                                                                    
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_data_folder(temp_dir):                                                                                                    
    """Create a mock data folder with necessary structure"""                                                                       
    # Create data folder structure                                                                                                 
    data_folder = os.path.join(temp_dir, 'test_data')                                                                              
    session_dir = os.path.join(data_folder, 'session1')                                                                            
    os.makedirs(session_dir, exist_ok=True)                                                                                        
                                                                                                                                   
    # Create info.rhd file                                                                                                         
    with open(os.path.join(session_dir, 'info.rhd'), 'w') as f:                                                                    
        f.write('test info file')                                                                                                  
                                                                                                                                   
    # Create metadata.info file                                                                                                    
    with open(os.path.join(data_folder, 'metadata.info'), 'w') as f:                                                               
        f.write('test metadata file')                                                                                              
                                                                                                                                   
    return data_folder                                                                                                             
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_dataset_handler(mock_server_path):                                                                                        
    """Create a mock dataset handler"""                                                                                            
    dir_path, server_path, _ = mock_server_path                                                                                    
                                                                                                                                   
    # Create a real DatasetFrameHandler instance                                                                                   
    handler = DatasetFrameHandler(dir_path)                                                                                        
                                                                                                                                   
    # Override server_path and server_home_dir                                                                                     
    handler.server_path = server_path                                                                                              
    handler.server_home_dir = os.path.join(server_path, 'data_management')                                                         
                                                                                                                                   
    # Create empty dataset_frame.csv                                                                                               
    dataset_frame = pd.DataFrame(columns=['date', 'time', 'user', 'email', 'recording', 'recording_path', 'info_file_exists'])     
    dataset_frame.to_csv(os.path.join(handler.server_home_dir, 'dataset_frame.csv'), index=False)                                  
                                                                                                                                   
    return handler                                                                                                                 
                                                                                                                                   
def test_parse_arguments():                                                                                                        
    """Test argument parsing with data folder specified"""                                                                         
    with patch('sys.argv', ['blech_data_transfer.py', '/test/path']):                                                              
        from src.blech_data_transfer import parse_arguments                                                                        
        args = parse_arguments()                                                                                                   
        assert args.data_folder == '/test/path'                                                                                    
                                                                                                                                   
def test_get_data_folder():                                                                                                        
    """Test getting data folder from arguments"""                                                                                  
    from src.blech_data_transfer import get_data_folder                                                                            
                                                                                                                                   
    # Test with data folder in args                                                                                                
    args = MagicMock()                                                                                                             
    args.data_folder = '/test/path'                                                                                                
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/test/path'                                                                                              
                                                                                                                                   
    # Test with no data folder in args, using GUI                                                                                  
    args.data_folder = None                                                                                                        
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/gui/path'                                                                                               
                                                                                                                                   
    # Test with trailing slash                                                                                                     
    args.data_folder = '/test/path/'                                                                                               
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/test/path'                                                                                              
                                                                                                                                   
def test_initialize_dataset_handler(mock_server_path):                                                                             
    """Test initializing dataset handler"""                                                                                        
    from src.blech_data_transfer import initialize_dataset_handler                                                                 
                                                                                                                                   
    dir_path, _, _ = mock_server_path                                                                                              
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        handler = initialize_dataset_handler(dir_path)                                                                             
                                                                                                                                   
    assert handler is not None                                                                                                     
    assert hasattr(handler, 'check_dataset_frame')                                                                                 
    assert hasattr(handler, 'sync_logs')                                                                                           
                                                                                                                                   
def test_check_experiment_existence(mock_dataset_handler, mock_data_folder):                                                       
    """Test checking if experiment exists"""                                                                                       
    from src.blech_data_transfer import check_experiment_existence                                                                 
                                                                                                                                   
    # Mock check_experiment_exists to return False                                                                                 
    mock_dataset_handler.check_experiment_exists = MagicMock(return_value=False)                                                   
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        # Should not raise any exceptions                                                                                          
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
                                                                                                                                   
    # Mock check_experiment_exists to return True                                                                                  
    mock_dataset_handler.check_experiment_exists = MagicMock(return_value=True)                                                    
                                                                                                                                   
    # Test with user choosing to continue                                                                                          
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['y']):                                                                               
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
                                                                                                                                   
    # Test with user choosing to exit                                                                                              
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['n']), \                                                                             
         patch('sys.exit') as mock_exit:                                                                                           
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_validate_data_folder(mock_data_folder):                                                                                   
    """Test validating data folder"""                                                                                              
    from src.blech_data_transfer import validate_data_folder                                                                       
                                                                                                                                   
    # Test with valid folder                                                                                                       
    with patch('sys.stdout', new=StringIO()):                                                                                      
        validate_data_folder(mock_data_folder)                                                                                     
                                                                                                                                   
    # Test with invalid folder                                                                                                     
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('sys.exit') as mock_exit:                                                                                           
        validate_data_folder('/nonexistent/path')                                                                                  
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_check_info_file(mock_data_folder):                                                                                        
    """Test checking for info file"""                                                                                              
    from src.blech_data_transfer import check_info_file                                                                            
                                                                                                                                   
    # Create a test info file                                                                                                      
    info_file_path = os.path.join(mock_data_folder, 'test.info')                                                                   
    with open(info_file_path, 'w') as f:                                                                                           
        f.write('test info content')                                                                                               
                                                                                                                                   
    # Test with existing info file                                                                                                 
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = check_info_file(mock_data_folder)                                                                                 
        assert result == info_file_path                                                                                            
                                                                                                                                   
    # Remove the info file                                                                                                         
    os.remove(info_file_path)                                                                                                      
                                                                                                                                   
    # Test with no info file, user chooses to exit                                                                                 
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['e']), \                                                                             
         patch('sys.exit') as mock_exit:                                                                                           
        check_info_file(mock_data_folder)                                                                                          
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
    # Test with no info file, user chooses to wait then finds file                                                                 
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['w', '']), \                                                                         
         patch('glob', side_effect=[[], [info_file_path]]):                                                                        
        with open(info_file_path, 'w') as f:                                                                                       
            f.write('test info content')                                                                                           
        result = check_info_file(mock_data_folder)                                                                                 
        assert result == info_file_path                                                                                            
                                                                                                                                   
def test_select_user(mock_server_path):                                                                                            
    """Test selecting a user from the list"""                                                                                      
    from src.blech_data_transfer import select_user                                                                                
                                                                                                                                   
    _, _, data_mgmt_dir = mock_server_path                                                                                         
                                                                                                                                   
    # Read users list                                                                                                              
    users_list = pd.read_csv(os.path.join(data_mgmt_dir, 'users_list.txt'))                                                        
                                                                                                                                   
    # Test with valid selection                                                                                                    
    with patch('builtins.input', return_value='1'), \                                                                              
         patch('sys.stdout', new=StringIO()):                                                                                      
        user, user_path = select_user(users_list, os.path.dirname(data_mgmt_dir))                                                  
        assert user == 'user1'                                                                                                     
        assert 'user1_dir' in user_path                                                                                            
                                                                                                                                   
    # Test with invalid then valid selection                                                                                       
    with patch('builtins.input', side_effect=['invalid', '2']), \                                                                  
         patch('sys.stdout', new=StringIO()):                                                                                      
        user, user_path = select_user(users_list, os.path.dirname(data_mgmt_dir))                                                  
        assert user == 'user2'                                                                                                     
        assert 'user2_dir' in user_path                                                                                            
                                                                                                                                   
def test_select_subfolder(mock_server_path):                                                                                       
    """Test selecting a subfolder"""                                                                                               
    from src.blech_data_transfer import select_subfolder                                                                           
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    user_path = os.path.join(server_path, 'user1_dir')                                                                             
                                                                                                                                   
    # Test selecting existing subfolder                                                                                            
    with patch('builtins.input', return_value='1'), \                                                                              
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'subfolder1')                                                                   
                                                                                                                                   
    # Test creating new subfolder                                                                                                  
    with patch('builtins.input', side_effect=['-1', 'new_subfolder']), \                                                           
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'new_subfolder')                                                                
        assert os.path.exists(os.path.join(user_path, 'new_subfolder'))                                                            
                                                                                                                                   
    # Test with invalid then valid selection                                                                                       
    with patch('builtins.input', side_effect=['invalid', '2']), \                                                                  
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'subfolder2')                                                                   
                                                                                                                                   
def test_prepare_file_transfer(mock_data_folder, mock_server_path):                                                                
    """Test preparing file transfer"""                                                                                             
    from src.blech_data_transfer import prepare_file_transfer                                                                      
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    copy_dir = os.path.join(server_path, 'user1_dir', 'subfolder1')                                                                
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        dir_list, file_list, rel_file_list, server_data_folder = prepare_file_transfer(mock_data_folder, copy_dir)                 
                                                                                                                                   
    assert isinstance(dir_list, list)                                                                                              
    assert isinstance(file_list, list)                                                                                             
    assert isinstance(rel_file_list, list)                                                                                         
    assert os.path.exists(server_data_folder)                                                                                      
    assert os.path.basename(mock_data_folder) in server_data_folder                                                                
                                                                                                                                   
def test_transfer_data(mock_data_folder, mock_server_path):                                                                        
    """Test transferring data"""                                                                                                   
    from src.blech_data_transfer import transfer_data                                                                              
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    copy_dir = os.path.join(server_path, 'user1_dir', 'subfolder1')                                                                
    server_data_folder = os.path.join(copy_dir, os.path.basename(mock_data_folder))                                                
    os.makedirs(server_data_folder, exist_ok=True)                                                                                 
                                                                                                                                   
    # Get file lists                                                                                                               
    dir_list = [os.path.join(mock_data_folder, 'session1')]                                                                        
    file_list = [                                                                                                                  
        os.path.join(mock_data_folder, 'metadata.info'),                                                                           
        os.path.join(mock_data_folder, 'session1', 'info.rhd')                                                                     
    ]                                                                                                                              
    rel_file_list = [                                                                                                              
        'metadata.info',                                                                                                           
        os.path.join('session1', 'info.rhd')                                                                                       
    ]                                                                                                                              
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('tqdm', lambda x: x):                                                                                               
        transfer_data(mock_data_folder, server_data_folder, dir_list, rel_file_list)                                               
                                                                                                                                   
    # Check if files were copied                                                                                                   
    assert os.path.exists(os.path.join(server_data_folder, 'metadata.info'))                                                       
    assert os.path.exists(os.path.join(server_data_folder, 'session1', 'info.rhd'))                                                
                                                                                                                                   
def test_add_log_entry(mock_dataset_handler, mock_server_path, mock_data_folder):                                                  
    """Test adding log entry"""                                                                                                    
    from src.blech_data_transfer import add_log_entry                                                                              
                                                                                                                                   
    _, server_path, data_mgmt_dir = mock_server_path                                                                               
                                                                                                                                   
    # Create users list                                                                                                            
    users_list = pd.read_csv(os.path.join(data_mgmt_dir, 'users_list.txt'))                                                        
                                                                                                                                   
    user = 'user1'                                                                                                                 
    server_data_folder = os.path.join(server_path, 'user1_dir', 'subfolder1', os.path.basename(mock_data_folder))                  
                                                                                                                                   
    # Mock add_entry method                                                                                                        
    mock_dataset_handler.add_entry = MagicMock()                                                                                   
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        add_log_entry(mock_dataset_handler, users_list, user, mock_data_folder, server_data_folder)                                
                                                                                                                                   
    # Check if add_entry was called with correct arguments                                                                         
    mock_dataset_handler.add_entry.assert_called_once()                                                                            
    args = mock_dataset_handler.add_entry.call_args[0][0]                                                                          
    assert args['user'] == user                                                                                                    
    assert args['recording'] == os.path.basename(mock_data_folder)                                                                 
    assert args['recording_path'] == server_data_folder                                                                            
    assert args['info_file_exists'] == True                                                                                        
>>>>>>> REPLACE                                                                                                                    
                                                                                                                                   

Now let's run the tests to verify our implementation:                                                                              

                                                                                                                                   
python -m pytest tests/test_blech_data_transfer.py -v                                                                              
                                                                                                                                   

Note that these tests are designed to work with the actual implementation of blech_data_transfer.py. Some functions in the tests   
may need adjustments based on how the actual code behaves during testing.                                                          

Tokens: 13k sent, 3.0k received. Cost: $0.08 message, $0.14 session.

dataset_frame.csv
I'll create a test file for blech_data_transfer.py based on the files you've provided. Let me refine the test file to better match 
the actual implementation.                                                                                                         

tests/test_blech_data_transfer.py                                                                                                  

                                                                                                                                   
<<<<<<< SEARCH                                                                                                                     
=======                                                                                                                            
import pytest                                                                                                                      
import os                                                                                                                          
import pandas as pd                                                                                                                
import tempfile                                                                                                                    
import shutil                                                                                                                      
from unittest.mock import patch, MagicMock                                                                                         
from io import StringIO                                                                                                            
import sys                                                                                                                         
                                                                                                                                   
# Import the dataset_handler module                                                                                                
from src.dataset_handler import DatasetFrameHandler, DatasetFrameLogger                                                            
                                                                                                                                   
@pytest.fixture                                                                                                                    
def temp_dir():                                                                                                                    
    """Create a temporary directory for testing"""                                                                                 
    temp_dir = tempfile.mkdtemp()                                                                                                  
    yield temp_dir                                                                                                                 
    shutil.rmtree(temp_dir)                                                                                                        
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_server_path(temp_dir):                                                                                                    
    """Create a mock server path structure"""                                                                                      
    # Create local_only_files directory with server path file                                                                      
    local_only_dir = os.path.join(temp_dir, 'local_only_files')                                                                    
    os.makedirs(local_only_dir, exist_ok=True)                                                                                     
                                                                                                                                   
    # Create server path file                                                                                                      
    server_path = os.path.join(temp_dir, 'server')                                                                                 
    os.makedirs(server_path, exist_ok=True)                                                                                        
                                                                                                                                   
    with open(os.path.join(local_only_dir, 'blech_server_path.txt'), 'w') as f:                                                    
        f.write(server_path)                                                                                                       
                                                                                                                                   
    # Create data_management directory                                                                                             
    data_mgmt_dir = os.path.join(server_path, 'data_management')                                                                   
    os.makedirs(data_mgmt_dir, exist_ok=True)                                                                                      
                                                                                                                                   
    # Create users_list.txt in data_management                                                                                     
    users_list_content = """Username,Directory,Email                                                                               
user1,user1_dir,user1@example.com                                                                                                  
user2,user2_dir,user2@example.com                                                                                                  
user3,user3_dir,user3@example.com"""                                                                                               
                                                                                                                                   
    with open(os.path.join(data_mgmt_dir, 'users_list.txt'), 'w') as f:                                                            
        f.write(users_list_content)                                                                                                
                                                                                                                                   
    # Create user directories                                                                                                      
    for user_dir in ['user1_dir', 'user2_dir', 'user3_dir']:                                                                       
        user_path = os.path.join(server_path, user_dir)                                                                            
        os.makedirs(user_path, exist_ok=True)                                                                                      
        # Create some subfolders for user1                                                                                         
        if user_dir == 'user1_dir':                                                                                                
            os.makedirs(os.path.join(user_path, 'subfolder1'), exist_ok=True)                                                      
            os.makedirs(os.path.join(user_path, 'subfolder2'), exist_ok=True)                                                      
                                                                                                                                   
    return temp_dir, server_path, data_mgmt_dir                                                                                    
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_data_folder(temp_dir):                                                                                                    
    """Create a mock data folder with necessary structure"""                                                                       
    # Create data folder structure                                                                                                 
    data_folder = os.path.join(temp_dir, 'test_data')                                                                              
    session_dir = os.path.join(data_folder, 'session1')                                                                            
    os.makedirs(session_dir, exist_ok=True)                                                                                        
                                                                                                                                   
    # Create info file                                                                                                             
    with open(os.path.join(data_folder, 'test.info'), 'w') as f:                                                                   
        f.write('test info content')                                                                                               
                                                                                                                                   
    # Create some test files                                                                                                       
    with open(os.path.join(session_dir, 'test_file.txt'), 'w') as f:                                                               
        f.write('test file content')                                                                                               
                                                                                                                                   
    return data_folder                                                                                                             
                                                                                                                                   
@pytest.fixture                                                                                                                    
def mock_dataset_handler(mock_server_path):                                                                                        
    """Create a mock dataset handler"""                                                                                            
    dir_path, server_path, _ = mock_server_path                                                                                    
                                                                                                                                   
    # Create a real DatasetFrameHandler instance                                                                                   
    handler = DatasetFrameHandler(dir_path)                                                                                        
                                                                                                                                   
    # Override server_path and server_home_dir                                                                                     
    handler.server_path = server_path                                                                                              
    handler.server_home_dir = os.path.join(server_path, 'data_management')                                                         
                                                                                                                                   
    # Create empty dataset_frame.csv                                                                                               
    dataset_frame = pd.DataFrame(columns=['date', 'time', 'user', 'email', 'recording', 'recording_path', 'info_file_exists'])     
    dataset_frame.to_csv(os.path.join(handler.server_home_dir, 'dataset_frame.csv'), index=False)                                  
                                                                                                                                   
    return handler                                                                                                                 
                                                                                                                                   
def test_parse_arguments():                                                                                                        
    """Test argument parsing with data folder specified"""                                                                         
    with patch('sys.argv', ['blech_data_transfer.py', '/test/path']):                                                              
        from src.blech_data_transfer import parse_arguments                                                                        
        args = parse_arguments()                                                                                                   
        assert args.data_folder == '/test/path'                                                                                    
                                                                                                                                   
def test_get_data_folder():                                                                                                        
    """Test getting data folder from arguments"""                                                                                  
    from src.blech_data_transfer import get_data_folder                                                                            
                                                                                                                                   
    # Test with data folder in args                                                                                                
    args = MagicMock()                                                                                                             
    args.data_folder = '/test/path'                                                                                                
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/test/path'                                                                                              
                                                                                                                                   
    # Test with no data folder in args, using GUI                                                                                  
    args.data_folder = None                                                                                                        
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/gui/path'                                                                                               
                                                                                                                                   
    # Test with trailing slash                                                                                                     
    args.data_folder = '/test/path/'                                                                                               
                                                                                                                                   
    with patch('easygui.diropenbox', return_value='/gui/path'):                                                                    
        result = get_data_folder(args)                                                                                             
        assert result == '/test/path'                                                                                              
                                                                                                                                   
def test_initialize_dataset_handler(mock_server_path):                                                                             
    """Test initializing dataset handler"""                                                                                        
    from src.blech_data_transfer import initialize_dataset_handler                                                                 
                                                                                                                                   
    dir_path, _, _ = mock_server_path                                                                                              
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        handler = initialize_dataset_handler(dir_path)                                                                             
                                                                                                                                   
    assert handler is not None                                                                                                     
    assert hasattr(handler, 'check_dataset_frame')                                                                                 
    assert hasattr(handler, 'sync_logs')                                                                                           
                                                                                                                                   
def test_check_experiment_existence(mock_dataset_handler, mock_data_folder):                                                       
    """Test checking if experiment exists"""                                                                                       
    from src.blech_data_transfer import check_experiment_existence                                                                 
                                                                                                                                   
    # Mock check_experiment_exists to return False                                                                                 
    mock_dataset_handler.check_experiment_exists = MagicMock(return_value=False)                                                   
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        # Should not raise any exceptions                                                                                          
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
                                                                                                                                   
    # Mock check_experiment_exists to return True                                                                                  
    mock_dataset_handler.check_experiment_exists = MagicMock(return_value=True)                                                    
                                                                                                                                   
    # Test with user choosing to continue                                                                                          
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['y']):                                                                               
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
                                                                                                                                   
    # Test with user choosing to exit                                                                                              
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['n']), \                                                                             
         patch('sys.exit') as mock_exit:                                                                                           
        check_experiment_existence(mock_dataset_handler, mock_data_folder)                                                         
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_validate_data_folder(mock_data_folder):                                                                                   
    """Test validating data folder"""                                                                                              
    from src.blech_data_transfer import validate_data_folder                                                                       
                                                                                                                                   
    # Test with valid folder                                                                                                       
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = validate_data_folder(mock_data_folder)                                                                            
        assert result is None  # Function doesn't return anything                                                                  
                                                                                                                                   
    # Test with invalid folder                                                                                                     
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('sys.exit') as mock_exit:                                                                                           
        validate_data_folder('/nonexistent/path')                                                                                  
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
def test_check_info_file(mock_data_folder):                                                                                        
    """Test checking for info file"""                                                                                              
    from src.blech_data_transfer import check_info_file                                                                            
                                                                                                                                   
    # Test with existing info file                                                                                                 
    with patch('sys.stdout', new=StringIO()):                                                                                      
        result = check_info_file(mock_data_folder)                                                                                 
        assert result == os.path.join(mock_data_folder, 'test.info')                                                               
                                                                                                                                   
    # Remove the info file                                                                                                         
    os.remove(os.path.join(mock_data_folder, 'test.info'))                                                                         
                                                                                                                                   
    # Test with no info file, user chooses to exit                                                                                 
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['e']), \                                                                             
         patch('sys.exit') as mock_exit:                                                                                           
        check_info_file(mock_data_folder)                                                                                          
        mock_exit.assert_called_once()                                                                                             
                                                                                                                                   
    # Test with no info file, user chooses to wait then finds file                                                                 
    info_file_path = os.path.join(mock_data_folder, 'new.info')                                                                    
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('builtins.input', side_effect=['w', '']), \                                                                         
         patch('glob', side_effect=[[], [info_file_path]]):                                                                        
        # Create the info file after the first glob call                                                                           
        with open(info_file_path, 'w') as f:                                                                                       
            f.write('test info content')                                                                                           
        result = check_info_file(mock_data_folder)                                                                                 
        assert result == info_file_path                                                                                            
                                                                                                                                   
def test_select_user(mock_server_path):                                                                                            
    """Test selecting a user from the list"""                                                                                      
    from src.blech_data_transfer import select_user                                                                                
                                                                                                                                   
    _, server_path, data_mgmt_dir = mock_server_path                                                                               
                                                                                                                                   
    # Read users list                                                                                                              
    users_list = pd.read_csv(os.path.join(data_mgmt_dir, 'users_list.txt'))                                                        
                                                                                                                                   
    # Test with valid selection                                                                                                    
    with patch('builtins.input', return_value='1'), \                                                                              
         patch('sys.stdout', new=StringIO()):                                                                                      
        user, user_path = select_user(users_list, server_path)                                                                     
        assert user == 'user1'                                                                                                     
        assert 'user1_dir' in user_path                                                                                            
                                                                                                                                   
    # Test with invalid then valid selection                                                                                       
    with patch('builtins.input', side_effect=['invalid', '2']), \                                                                  
         patch('sys.stdout', new=StringIO()):                                                                                      
        user, user_path = select_user(users_list, server_path)                                                                     
        assert user == 'user2'                                                                                                     
        assert 'user2_dir' in user_path                                                                                            
                                                                                                                                   
def test_select_subfolder(mock_server_path):                                                                                       
    """Test selecting a subfolder"""                                                                                               
    from src.blech_data_transfer import select_subfolder                                                                           
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    user_path = os.path.join(server_path, 'user1_dir')                                                                             
                                                                                                                                   
    # Test selecting existing subfolder                                                                                            
    with patch('builtins.input', return_value='1'), \                                                                              
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'subfolder1')                                                                   
                                                                                                                                   
    # Test creating new subfolder                                                                                                  
    with patch('builtins.input', side_effect=['-1', 'new_subfolder']), \                                                           
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'new_subfolder')                                                                
        assert os.path.exists(os.path.join(user_path, 'new_subfolder'))                                                            
                                                                                                                                   
    # Test with invalid then valid selection                                                                                       
    with patch('builtins.input', side_effect=['invalid', '2']), \                                                                  
         patch('sys.stdout', new=StringIO()):                                                                                      
        copy_dir = select_subfolder(user_path)                                                                                     
        assert copy_dir == os.path.join(user_path, 'subfolder2')                                                                   
                                                                                                                                   
def test_prepare_file_transfer(mock_data_folder, mock_server_path):                                                                
    """Test preparing file transfer"""                                                                                             
    from src.blech_data_transfer import prepare_file_transfer                                                                      
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    copy_dir = os.path.join(server_path, 'user1_dir', 'subfolder1')                                                                
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        dir_list, file_list, rel_file_list, server_data_folder = prepare_file_transfer(mock_data_folder, copy_dir)                 
                                                                                                                                   
    assert isinstance(dir_list, list)                                                                                              
    assert isinstance(file_list, list)                                                                                             
    assert isinstance(rel_file_list, list)                                                                                         
    assert os.path.exists(server_data_folder)                                                                                      
    assert os.path.basename(mock_data_folder) in server_data_folder                                                                
                                                                                                                                   
    # Check if session1 is in dir_list                                                                                             
    assert any('session1' in d for d in dir_list)                                                                                  
                                                                                                                                   
    # Check if test_file.txt is in rel_file_list                                                                                   
    assert any('test_file.txt' in f for f in rel_file_list)                                                                        
                                                                                                                                   
def test_transfer_data(mock_data_folder, mock_server_path):                                                                        
    """Test transferring data"""                                                                                                   
    from src.blech_data_transfer import transfer_data                                                                              
                                                                                                                                   
    _, server_path, _ = mock_server_path                                                                                           
    copy_dir = os.path.join(server_path, 'user1_dir', 'subfolder1')                                                                
    server_data_folder = os.path.join(copy_dir, os.path.basename(mock_data_folder))                                                
    os.makedirs(server_data_folder, exist_ok=True)                                                                                 
                                                                                                                                   
    # Get file lists                                                                                                               
    dir_list = [os.path.join(mock_data_folder, 'session1')]                                                                        
    file_list = [                                                                                                                  
        os.path.join(mock_data_folder, 'test.info'),                                                                               
        os.path.join(mock_data_folder, 'session1', 'test_file.txt')                                                                
    ]                                                                                                                              
    rel_file_list = [                                                                                                              
        'test.info',                                                                                                               
        os.path.join('session1', 'test_file.txt')                                                                                  
    ]                                                                                                                              
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()), \                                                                                    
         patch('tqdm', lambda x: x):                                                                                               
        transfer_data(mock_data_folder, server_data_folder, dir_list, rel_file_list)                                               
                                                                                                                                   
    # Check if directories were created                                                                                            
    assert os.path.exists(os.path.join(server_data_folder, 'session1'))                                                            
                                                                                                                                   
    # Check if files were copied                                                                                                   
    assert os.path.exists(os.path.join(server_data_folder, 'test.info'))                                                           
    assert os.path.exists(os.path.join(server_data_folder, 'session1', 'test_file.txt'))                                           
                                                                                                                                   
def test_add_log_entry(mock_dataset_handler, mock_server_path, mock_data_folder):                                                  
    """Test adding log entry"""                                                                                                    
    from src.blech_data_transfer import add_log_entry                                                                              
                                                                                                                                   
    _, server_path, data_mgmt_dir = mock_server_path                                                                               
                                                                                                                                   
    # Create users list                                                                                                            
    users_list = pd.read_csv(os.path.join(data_mgmt_dir, 'users_list.txt'))                                                        
                                                                                                                                   
    user = 'user1'                                                                                                                 
    server_data_folder = os.path.join(server_path, 'user1_dir', 'subfolder1', os.path.basename(mock_data_folder))                  
                                                                                                                                   
    # Mock add_entry method                                                                                                        
    mock_dataset_handler.add_entry = MagicMock()                                                                                   
                                                                                                                                   
    with patch('sys.stdout', new=StringIO()):                                                                                      
        add_log_entry(mock_dataset_handler, users_list, user, mock_data_folder, server_data_folder)                                
                                                                                                                                   
    # Check if add_entry was called with correct arguments                                                                         
    mock_dataset_handler.add_entry.assert_called_once()                                                                            
    args = mock_dataset_handler.add_entry.call_args[0][0]                                                                          
    assert args['user'] == user                                                                                                    
    assert args['recording'] == os.path.basename(mock_data_folder)                                                                 
    assert args['recording_path'] == server_data_folder                                                                            
    assert args['info_file_exists'] == True                                                                                        
>>>>>>> REPLACE                                                                                                                    
                                                                                                                                   

Now let's run the tests to verify our implementation:                                                                              

                                                                                                                                   
python -m pytest tests/test_blech_data_transfer.py -v                                                                              
                                                                                                                                   

These tests are designed to work with the actual implementation of blech_data_transfer.py. The tests cover all the main functions  
in the script, including argument parsing, data folder validation, user selection, file transfer, and log entry creation.          

Tokens: 17k sent, 3.1k received. Cost: $0.10 message, $0.23 session.

tests/test_blech_data_transfer.py
Applied edit to tests/test_blech_data_transfer.py
Commit 1bf4671 Based on the files you've added, I'll generate a concise commit message that captures the essence of adding tests 
for the `blech_data_transfer` module:

test: add comprehensive test suite for blech_data_transfer module

python -m pytest tests/test_blech_data_transfer.py -v
Summarization failed for model anthropic/claude-3-5-haiku-20241022: cannot schedule new futures after shutdown
Summarization failed for model anthropic/claude-3-7-sonnet-20250219: cannot schedule new futures after shutdown
summarizer unexpectedly failed for all models

Copy link
Copy Markdown
Member Author

abuzarmahmood commented Apr 28, 2025

Merge activity

  • Apr 28, 11:44 AM EDT: A user started a stack merge that includes this pull request via Graphite.
  • Apr 28, 11:46 AM EDT: Graphite rebased this pull request as part of a merge.
  • Apr 28, 11:47 AM EDT: @abuzarmahmood merged this pull request with Graphite.

@abuzarmahmood abuzarmahmood changed the base branch from 9-refactor-blech_data_transfer-into-functions-and-function-calls to graphite-base/15 April 28, 2025 15:44
@abuzarmahmood abuzarmahmood changed the base branch from graphite-base/15 to master April 28, 2025 15:45
- Moved the `mount_katz_drive.sh` script to the `utils` directory for better organization of utility scripts.
- Moved `blech_data_sentry.py` to `src/blech_data_sentry.py` for better project organization.
- Moved `blech_data_transfer.py` to `src/blech_data_transfer.py` to maintain consistent structure.
- Moved `dataset_handler.py` to `src/dataset_handler.py` to align with new project structure.
…ge that captures the essence of adding tests for the `blech_data_transfer` module:

test: add comprehensive test suite for blech_data_transfer module
- Change import statement to use an absolute import path for `dataset_handler` to ensure module is correctly resolved.
- Addresses potential issues with relative imports that could cause import errors in certain environments.
- Create an `__init__.py` file for the `tests` directory to ensure it is recognized as a package.
@abuzarmahmood abuzarmahmood merged commit 90e212f into master Apr 28, 2025
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant