|  | 
|  | 1 | +from unittest.mock import Mock, patch | 
|  | 2 | + | 
|  | 3 | +from django.core.management import call_command | 
|  | 4 | + | 
|  | 5 | +import pytest | 
|  | 6 | +from gitlab import GitlabGetError | 
|  | 7 | + | 
|  | 8 | +from codebase.clients import RepoClient | 
|  | 9 | +from codebase.indexes import CodebaseIndex | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +@pytest.fixture | 
|  | 13 | +def mock_repo_client(): | 
|  | 14 | +    with patch.object(RepoClient, "create_instance") as mock: | 
|  | 15 | +        client = Mock(spec=RepoClient) | 
|  | 16 | +        mock.return_value = client | 
|  | 17 | +        yield client | 
|  | 18 | + | 
|  | 19 | + | 
|  | 20 | +@pytest.fixture | 
|  | 21 | +def mock_indexer(): | 
|  | 22 | +    with patch("codebase.management.commands.update_index.CodebaseIndex") as mock: | 
|  | 23 | +        indexer = Mock(spec=CodebaseIndex) | 
|  | 24 | +        mock.return_value = indexer | 
|  | 25 | +        yield indexer | 
|  | 26 | + | 
|  | 27 | + | 
|  | 28 | +def test_update_index_all_repositories(mock_repo_client, mock_indexer): | 
|  | 29 | +    """Test updating index for all repositories.""" | 
|  | 30 | +    # Mock repositories | 
|  | 31 | +    mock_repo1 = Mock(slug="repo1") | 
|  | 32 | +    mock_repo2 = Mock(slug="repo2") | 
|  | 33 | +    mock_repo_client.list_repositories.return_value = [mock_repo1, mock_repo2] | 
|  | 34 | + | 
|  | 35 | +    # Call the command | 
|  | 36 | +    call_command("update_index") | 
|  | 37 | + | 
|  | 38 | +    # Verify repositories were listed with correct parameters | 
|  | 39 | +    mock_repo_client.list_repositories.assert_called_once_with(topics=None, load_all=True) | 
|  | 40 | + | 
|  | 41 | +    # Verify index was updated for each repository | 
|  | 42 | +    assert mock_indexer.update.call_count == 2 | 
|  | 43 | +    mock_indexer.update.assert_any_call(repo_id="repo1", ref=None) | 
|  | 44 | +    mock_indexer.update.assert_any_call(repo_id="repo2", ref=None) | 
|  | 45 | + | 
|  | 46 | + | 
|  | 47 | +def test_update_index_specific_repository(mock_repo_client, mock_indexer): | 
|  | 48 | +    """Test updating index for a specific repository.""" | 
|  | 49 | +    # Mock repository | 
|  | 50 | +    mock_repo = Mock(slug="specific-repo") | 
|  | 51 | +    mock_repo_client.get_repository.return_value = mock_repo | 
|  | 52 | + | 
|  | 53 | +    # Call the command | 
|  | 54 | +    call_command("update_index", repo_id="specific-repo") | 
|  | 55 | + | 
|  | 56 | +    # Verify correct repository was fetched | 
|  | 57 | +    mock_repo_client.get_repository.assert_called_once_with("specific-repo") | 
|  | 58 | + | 
|  | 59 | +    # Verify index was updated only for the specific repository | 
|  | 60 | +    mock_indexer.update.assert_called_once_with(repo_id="specific-repo", ref=None) | 
|  | 61 | +    mock_repo_client.list_repositories.assert_not_called() | 
|  | 62 | + | 
|  | 63 | + | 
|  | 64 | +def test_update_index_with_topics(mock_repo_client, mock_indexer): | 
|  | 65 | +    """Test updating index for repositories with specific topics.""" | 
|  | 66 | +    # Mock repositories | 
|  | 67 | +    mock_repo1 = Mock(slug="repo1") | 
|  | 68 | +    mock_repo2 = Mock(slug="repo2") | 
|  | 69 | +    mock_repo_client.list_repositories.return_value = [mock_repo1, mock_repo2] | 
|  | 70 | + | 
|  | 71 | +    # Call the command with topics | 
|  | 72 | +    call_command("update_index", topics=["python", "django"]) | 
|  | 73 | + | 
|  | 74 | +    # Verify repositories were listed with correct topics | 
|  | 75 | +    mock_repo_client.list_repositories.assert_called_once_with(topics=["python", "django"], load_all=True) | 
|  | 76 | + | 
|  | 77 | +    # Verify index was updated for each repository | 
|  | 78 | +    assert mock_indexer.update.call_count == 2 | 
|  | 79 | +    mock_indexer.update.assert_any_call(repo_id="repo1", ref=None) | 
|  | 80 | +    mock_indexer.update.assert_any_call(repo_id="repo2", ref=None) | 
|  | 81 | + | 
|  | 82 | + | 
|  | 83 | +def test_update_index_with_ref(mock_repo_client, mock_indexer): | 
|  | 84 | +    """Test updating index for a specific reference.""" | 
|  | 85 | +    # Mock repository | 
|  | 86 | +    mock_repo = Mock(slug="repo1") | 
|  | 87 | +    mock_repo_client.list_repositories.return_value = [mock_repo] | 
|  | 88 | + | 
|  | 89 | +    # Call the command with ref | 
|  | 90 | +    call_command("update_index", ref="feature-branch") | 
|  | 91 | + | 
|  | 92 | +    # Verify index was updated with correct reference | 
|  | 93 | +    mock_indexer.update.assert_called_once_with(repo_id="repo1", ref="feature-branch") | 
|  | 94 | + | 
|  | 95 | + | 
|  | 96 | +def test_update_index_with_reset(mock_repo_client, mock_indexer): | 
|  | 97 | +    """Test updating index with reset option.""" | 
|  | 98 | +    # Mock repository | 
|  | 99 | +    mock_repo = Mock(slug="repo1") | 
|  | 100 | +    mock_repo_client.list_repositories.return_value = [mock_repo] | 
|  | 101 | + | 
|  | 102 | +    # Call the command with reset | 
|  | 103 | +    call_command("update_index", reset=True) | 
|  | 104 | + | 
|  | 105 | +    # Verify index was reset and then updated | 
|  | 106 | +    mock_indexer.delete.assert_called_once_with(repo_id="repo1", ref=None) | 
|  | 107 | +    mock_indexer.update.assert_called_once_with(repo_id="repo1", ref=None) | 
|  | 108 | + | 
|  | 109 | + | 
|  | 110 | +def test_update_index_repository_not_found(mock_repo_client, mock_indexer): | 
|  | 111 | +    """Test handling of non-existent repository.""" | 
|  | 112 | +    # Mock GitlabGetError for non-existent repository | 
|  | 113 | +    mock_repo_client.get_repository.side_effect = GitlabGetError() | 
|  | 114 | + | 
|  | 115 | +    # Call the command with non-existent repository | 
|  | 116 | +    call_command("update_index", repo_id="non-existent-repo") | 
|  | 117 | + | 
|  | 118 | +    # Verify error was handled gracefully | 
|  | 119 | +    mock_repo_client.get_repository.assert_called_once_with("non-existent-repo") | 
|  | 120 | +    mock_indexer.update.assert_not_called() | 
|  | 121 | +    mock_indexer.delete.assert_not_called() | 
|  | 122 | + | 
|  | 123 | + | 
|  | 124 | +def test_update_index_no_repositories(mock_repo_client, mock_indexer): | 
|  | 125 | +    """Test behavior when no repositories are found.""" | 
|  | 126 | +    # Mock empty repository list | 
|  | 127 | +    mock_repo_client.list_repositories.return_value = [] | 
|  | 128 | + | 
|  | 129 | +    # Call the command | 
|  | 130 | +    call_command("update_index") | 
|  | 131 | + | 
|  | 132 | +    # Verify no updates were performed | 
|  | 133 | +    mock_indexer.update.assert_not_called() | 
|  | 134 | +    mock_indexer.delete.assert_not_called() | 
0 commit comments