|
1 | 1 | import logging |
2 | 2 | from datetime import datetime, timezone |
3 | 3 | from typing import Any |
4 | | -from unittest.mock import patch |
| 4 | +from unittest.mock import MagicMock, patch |
5 | 5 |
|
6 | 6 | import orjson |
7 | 7 | import pytest |
|
14 | 14 | from sentry.constants import ObjectStatus |
15 | 15 | from sentry.integrations.models.integration import Integration |
16 | 16 | from sentry.integrations.models.repository_project_path_config import RepositoryProjectPathConfig |
| 17 | +from sentry.models.project import Project |
17 | 18 | from sentry.models.repository import Repository |
18 | 19 | from sentry.seer.endpoints.seer_rpc import ( |
| 20 | + bulk_get_project_preferences, |
19 | 21 | check_repository_integrations_status, |
20 | 22 | generate_request_signature, |
21 | 23 | get_attributes_for_span, |
22 | 24 | get_github_enterprise_integration_config, |
| 25 | + get_project_preferences, |
23 | 26 | get_repo_installation_id, |
24 | 27 | has_repo_code_mappings, |
25 | 28 | trigger_coding_agent_launch, |
@@ -1484,6 +1487,76 @@ def test_get_repo_installation_id_integration_not_found(self) -> None: |
1484 | 1487 |
|
1485 | 1488 | assert result == {"error": "integration_not_found"} |
1486 | 1489 |
|
| 1490 | + @patch("sentry.seer.endpoints.seer_rpc.read_preference_from_sentry_db") |
| 1491 | + def test_get_project_preferences_returns_preference(self, mock_read: Any) -> None: |
| 1492 | + project = self.create_project(organization=self.organization) |
| 1493 | + mock_read.return_value = MagicMock( |
| 1494 | + dict=MagicMock(return_value={"project_id": project.id, "repositories": []}) |
| 1495 | + ) |
| 1496 | + result = get_project_preferences( |
| 1497 | + organization_id=self.organization.id, |
| 1498 | + project_id=project.id, |
| 1499 | + ) |
| 1500 | + assert result == {"project_id": project.id, "repositories": []} |
| 1501 | + mock_read.assert_called_once() |
| 1502 | + |
| 1503 | + @patch("sentry.seer.endpoints.seer_rpc.read_preference_from_sentry_db") |
| 1504 | + def test_get_project_preferences_returns_none_when_no_preference(self, mock_read: Any) -> None: |
| 1505 | + project = self.create_project(organization=self.organization) |
| 1506 | + mock_read.return_value = None |
| 1507 | + result = get_project_preferences( |
| 1508 | + organization_id=self.organization.id, |
| 1509 | + project_id=project.id, |
| 1510 | + ) |
| 1511 | + assert result is None |
| 1512 | + |
| 1513 | + def test_get_project_preferences_raises_for_nonexistent_project(self) -> None: |
| 1514 | + with pytest.raises(Project.DoesNotExist): |
| 1515 | + get_project_preferences( |
| 1516 | + organization_id=self.organization.id, |
| 1517 | + project_id=999999, |
| 1518 | + ) |
| 1519 | + |
| 1520 | + def test_get_project_preferences_raises_for_wrong_org(self) -> None: |
| 1521 | + project = self.create_project(organization=self.organization) |
| 1522 | + other_org = self.create_organization(owner=self.user) |
| 1523 | + with pytest.raises(Project.DoesNotExist): |
| 1524 | + get_project_preferences( |
| 1525 | + organization_id=other_org.id, |
| 1526 | + project_id=project.id, |
| 1527 | + ) |
| 1528 | + |
| 1529 | + @patch("sentry.seer.endpoints.seer_rpc.bulk_read_preferences_from_sentry_db") |
| 1530 | + def test_bulk_get_project_preferences_returns_preferences(self, mock_bulk_read: Any) -> None: |
| 1531 | + project1 = self.create_project(organization=self.organization) |
| 1532 | + project2 = self.create_project(organization=self.organization) |
| 1533 | + mock_bulk_read.return_value = { |
| 1534 | + project1.id: MagicMock( |
| 1535 | + dict=MagicMock(return_value={"project_id": project1.id, "repositories": []}) |
| 1536 | + ), |
| 1537 | + project2.id: None, |
| 1538 | + } |
| 1539 | + result = bulk_get_project_preferences( |
| 1540 | + organization_id=self.organization.id, |
| 1541 | + project_ids=[project1.id, project2.id], |
| 1542 | + ) |
| 1543 | + assert result == { |
| 1544 | + str(project1.id): {"project_id": project1.id, "repositories": []}, |
| 1545 | + str(project2.id): None, |
| 1546 | + } |
| 1547 | + mock_bulk_read.assert_called_once_with(self.organization.id, [project1.id, project2.id]) |
| 1548 | + |
| 1549 | + @patch("sentry.seer.endpoints.seer_rpc.bulk_read_preferences_from_sentry_db") |
| 1550 | + def test_bulk_get_project_preferences_returns_empty_for_no_projects( |
| 1551 | + self, mock_bulk_read: Any |
| 1552 | + ) -> None: |
| 1553 | + mock_bulk_read.return_value = {} |
| 1554 | + result = bulk_get_project_preferences( |
| 1555 | + organization_id=self.organization.id, |
| 1556 | + project_ids=[], |
| 1557 | + ) |
| 1558 | + assert result == {} |
| 1559 | + |
1487 | 1560 |
|
1488 | 1561 | class TestTriggerCodingAgentLaunch: |
1489 | 1562 | @patch("sentry.seer.endpoints.seer_rpc.launch_coding_agents_for_run") |
|
0 commit comments