Skip to content

Commit 2e6d2dd

Browse files
committed
Added unittests to commands.
1 parent 71d41bd commit 2e6d2dd

File tree

4 files changed

+234
-0
lines changed

4 files changed

+234
-0
lines changed

tests/codebase/management/__init__.py

Whitespace-only changes.

tests/codebase/management/commands/__init__.py

Whitespace-only changes.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
from unittest.mock import Mock, call, patch
2+
3+
from django.core.management import call_command
4+
5+
import pytest
6+
7+
from codebase.clients import RepoClient
8+
9+
10+
@pytest.fixture
11+
def mock_repo_client():
12+
with patch.object(RepoClient, "create_instance") as mock:
13+
client = Mock(spec=RepoClient)
14+
mock.return_value = client
15+
yield client
16+
17+
18+
def test_setup_webhooks_success(mock_repo_client):
19+
"""Test successful webhook setup for repositories."""
20+
# Mock repositories
21+
mock_repo1 = Mock(slug="repo1")
22+
mock_repo2 = Mock(slug="repo2")
23+
mock_repo_client.list_repositories.return_value = [mock_repo1, mock_repo2]
24+
mock_repo_client.set_repository_webhooks.return_value = True
25+
26+
# Call the command
27+
call_command("setup_webhooks", base_url="https://test.com")
28+
29+
# Verify list_repositories was called with load_all=True
30+
mock_repo_client.list_repositories.assert_called_once_with(load_all=True)
31+
32+
# Verify set_repository_webhooks was called for each repo
33+
expected_calls = [
34+
call(
35+
"repo1",
36+
"https://test.com/api/codebase/callbacks/gitlab/",
37+
["push_events", "issues_events", "note_events", "pipeline_events"],
38+
enable_ssl_verification=True,
39+
),
40+
call(
41+
"repo2",
42+
"https://test.com/api/codebase/callbacks/gitlab/",
43+
["push_events", "issues_events", "note_events", "pipeline_events"],
44+
enable_ssl_verification=True,
45+
),
46+
]
47+
assert mock_repo_client.set_repository_webhooks.call_count == 2
48+
mock_repo_client.set_repository_webhooks.assert_has_calls(expected_calls)
49+
50+
51+
def test_setup_webhooks_with_ssl_disabled(mock_repo_client):
52+
"""Test webhook setup with SSL verification disabled."""
53+
# Mock single repository
54+
mock_repo = Mock(slug="repo1")
55+
mock_repo_client.list_repositories.return_value = [mock_repo]
56+
mock_repo_client.set_repository_webhooks.return_value = True
57+
58+
# Call the command with SSL verification disabled
59+
call_command("setup_webhooks", base_url="https://test.com", disable_ssl_verification=True)
60+
61+
# Verify set_repository_webhooks was called with SSL verification disabled
62+
mock_repo_client.set_repository_webhooks.assert_called_once_with(
63+
"repo1",
64+
"https://test.com/api/codebase/callbacks/gitlab/",
65+
["push_events", "issues_events", "note_events", "pipeline_events"],
66+
enable_ssl_verification=False,
67+
)
68+
69+
70+
def test_setup_webhooks_update_existing(mock_repo_client):
71+
"""Test updating existing webhooks."""
72+
# Mock repository
73+
mock_repo = Mock(slug="repo1")
74+
mock_repo_client.list_repositories.return_value = [mock_repo]
75+
# Return False to simulate updating existing webhook
76+
mock_repo_client.set_repository_webhooks.return_value = False
77+
78+
# Call the command
79+
call_command("setup_webhooks", base_url="https://test.com")
80+
81+
# Verify webhook was updated
82+
mock_repo_client.set_repository_webhooks.assert_called_once_with(
83+
"repo1",
84+
"https://test.com/api/codebase/callbacks/gitlab/",
85+
["push_events", "issues_events", "note_events", "pipeline_events"],
86+
enable_ssl_verification=True,
87+
)
88+
89+
90+
def test_setup_webhooks_no_repositories(mock_repo_client):
91+
"""Test behavior when no repositories are found."""
92+
# Mock empty repository list
93+
mock_repo_client.list_repositories.return_value = []
94+
95+
# Call the command
96+
call_command("setup_webhooks", base_url="https://test.com")
97+
98+
# Verify list_repositories was called but set_repository_webhooks was not
99+
mock_repo_client.list_repositories.assert_called_once_with(load_all=True)
100+
mock_repo_client.set_repository_webhooks.assert_not_called()
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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

Comments
 (0)