feature: adds tests

This commit is contained in:
ievgenii.v 2025-10-30 15:03:13 +02:00
parent e6a050d76a
commit f801ea6c71
3 changed files with 92 additions and 4 deletions

View file

@ -0,0 +1,87 @@
import pytest
from mock.mock import patch
from rhodecode.model.db import PullRequestReviewers, User
from rhodecode.tests.routes import route_path
@pytest.mark.usefixtures("autologin_user", "app")
@pytest.mark.backends("git", "hg")
@patch("rhodecode.apps.repository.views.repo_pull_requests.run_task")
class TestAiCodeReview:
def test_pr_closed_review_not_possible(self, backend, pr_util):
pull_request = pr_util.create_pull_request()
pull_request.status = pull_request.STATUS_CLOSED
with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
response = self.app.post(
route_path(
"pullrequest_start_ai_code_review",
repo_name=pull_request.target_repo.repo_name,
pull_request_id=pull_request.pull_request_id
)
)
session = response.get_session_from_response()
err_msg = session.peek_flash('error')
assert response.status_code == 200
assert response.json_body == {'response': True, 'redirect_url': None}
assert len(err_msg) == 1
assert err_msg[0] == "Cannot start review for closed pull requests."
assert run_task_mock.call_count == 0
def test_ai_review_finished(self, backend, pr_util):
pull_request = pr_util.create_pull_request()
pull_request.ai_code_review_state = {
"review_state": "finished"
}
repo_name = pull_request.target_repo.repo_name
pull_request_id = pull_request.pull_request_id
response = self.app.post(
route_path(
"pullrequest_start_ai_code_review",
repo_name=repo_name,
pull_request_id=pull_request_id
)
)
with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
redirect_url = route_path(
"pullrequest_show",
repo_name=repo_name,
pull_request_id=pull_request_id,
)
session = response.get_session_from_response()
err_msg = session.peek_flash('error')
success_message = session.peek_flash()
assert response.status_code == 200
assert response.json_body == {'response': True, 'redirect_url': redirect_url}
assert len(err_msg) == 0
assert len(success_message) == 0
assert run_task_mock.call_count == 0
def test_ai_user_added_to_reviewers_and_review_started(self, backend, pr_util):
pull_request = pr_util.create_pull_request()
pr_id = pull_request.pull_request_id
with patch("rhodecode.apps.repository.views.repo_pull_requests.run_task") as run_task_mock:
response = self.app.post(
route_path(
"pullrequest_start_ai_code_review",
repo_name=pull_request.target_repo.repo_name,
pull_request_id=pr_id
)
)
existing_reviewers = PullRequestReviewers.get_pull_request_reviewers(
pr_id, role=PullRequestReviewers.ROLE_REVIEWER
)
ai_user = User.get_ai_user()
assert response.status_code == 200
assert run_task_mock.call_count == 1
assert len(existing_reviewers) == 1
assert existing_reviewers[0].user_id == ai_user.user_id

View file

@ -1310,7 +1310,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
pull_request_id=pull_request.pull_request_id,
)
self._try_start_ai_code_review(c, _, pull_request, add_existing_users=True)
self._try_start_ai_code_review(_, pull_request, add_existing_users=True)
self._set_ai_pr_state_pending(pull_request)
return {"response": True, "redirect_url": redirect_url}
@ -1328,12 +1328,12 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
pull_request.ai_code_review_state = ai_state
Session().commit()
def _try_start_ai_code_review(self, c, _, pull_request: PullRequest, add_existing_users: bool = False) -> bool:
def _try_start_ai_code_review(self, _, pull_request: PullRequest, add_existing_users: bool = False) -> bool:
already_finished = False
ai_state = pull_request.ai_code_review_state
if ai_state:
already_finished = ai_state.get("reviewed", False)
already_finished = ai_state.get("review_state") in ["pending", "finished"]
if already_finished:
log.warning(f"Code review already finished.")
@ -1439,7 +1439,7 @@ class RepoPullRequestsView(RepoAppView, DataGridAppView):
user_id = safe_int(member_data.get("user_id", -1))
if ai_user_id == user_id:
success = self._try_start_ai_code_review(c, _, pull_request)
success = self._try_start_ai_code_review(_, pull_request)
if not success:
del members_[i]

View file

@ -262,6 +262,7 @@ def get_url_defs():
"pullrequest_repo_refs": "/{repo_name}/pull-request/refs/{target_repo_name:.*?[^/]}",
"pullrequest_repo_targets": "/{repo_name}/pull-request/repo-destinations",
"pullrequest_new": "/{repo_name}/pull-request/new",
"pullrequest_start_ai_code_review": "/{repo_name}/pull-request/{pull_request_id}/ai/review",
"pullrequest_create": "/{repo_name}/pull-request/create",
"pullrequest_update": "/{repo_name}/pull-request/{pull_request_id}/update",
"pullrequest_merge": "/{repo_name}/pull-request/{pull_request_id}/merge",