|
7 | 7 | from app.models.register_nodes_request import RegisterNodesRequestModel |
8 | 8 | from app.models.secrets_response import SecretsResponseModel |
9 | 9 | from app.models.list_models import ListRegisteredNodesResponse, ListGraphTemplatesResponse |
| 10 | +from app.models.run_models import RunsResponse, RunListItem, RunStatusEnum |
10 | 11 |
|
11 | 12 |
|
12 | 13 | import pytest |
@@ -819,16 +820,138 @@ async def test_re_enqueue_after_state_route_with_different_delays(self, mock_re_ |
819 | 820 | async def test_get_runs_route_with_valid_api_key(self, mock_get_runs, mock_request): |
820 | 821 | """Test get_runs_route with valid API key""" |
821 | 822 | from app.routes import get_runs_route |
| 823 | + from app.models.run_models import RunsResponse, RunListItem, RunStatusEnum |
| 824 | + from datetime import datetime |
822 | 825 |
|
823 | | - # Arrange |
824 | | - mock_get_runs.return_value = MagicMock() |
| 826 | + # Arrange - Create a comprehensive mock response |
| 827 | + mock_run_1 = MagicMock(spec=RunListItem) |
| 828 | + mock_run_1.run_id = "test_run_123" |
| 829 | + mock_run_1.graph_name = "test_graph" |
| 830 | + mock_run_1.success_count = 5 |
| 831 | + mock_run_1.pending_count = 2 |
| 832 | + mock_run_1.errored_count = 0 |
| 833 | + mock_run_1.retried_count = 1 |
| 834 | + mock_run_1.total_count = 8 |
| 835 | + mock_run_1.status = RunStatusEnum.SUCCESS |
| 836 | + mock_run_1.created_at = datetime(2024, 1, 15, 10, 30, 0) |
| 837 | + |
| 838 | + mock_run_2 = MagicMock(spec=RunListItem) |
| 839 | + mock_run_2.run_id = "test_run_456" |
| 840 | + mock_run_2.graph_name = "production_graph" |
| 841 | + mock_run_2.success_count = 10 |
| 842 | + mock_run_2.pending_count = 3 |
| 843 | + mock_run_2.errored_count = 1 |
| 844 | + mock_run_2.retried_count = 2 |
| 845 | + mock_run_2.total_count = 16 |
| 846 | + mock_run_2.status = RunStatusEnum.PENDING |
| 847 | + mock_run_2.created_at = datetime(2024, 1, 15, 11, 45, 0) |
| 848 | + |
| 849 | + expected_response = RunsResponse( |
| 850 | + namespace="test_namespace", |
| 851 | + total=2, |
| 852 | + page=1, |
| 853 | + size=10, |
| 854 | + runs=[mock_run_1, mock_run_2] |
| 855 | + ) |
| 856 | + |
| 857 | + mock_get_runs.return_value = expected_response |
825 | 858 |
|
826 | 859 | # Act |
827 | 860 | result = await get_runs_route("test_namespace", 1, 10, mock_request, "valid_key") |
828 | 861 |
|
829 | 862 | # Assert |
830 | 863 | mock_get_runs.assert_called_once_with("test_namespace", 1, 10, "test-request-id") |
831 | | - assert result == mock_get_runs.return_value |
| 864 | + assert result == expected_response |
| 865 | + |
| 866 | + # Verify response structure and content |
| 867 | + assert result.namespace == "test_namespace" |
| 868 | + assert result.total == 2 |
| 869 | + assert result.page == 1 |
| 870 | + assert result.size == 10 |
| 871 | + assert len(result.runs) == 2 |
| 872 | + |
| 873 | + # Verify first run details |
| 874 | + assert result.runs[0].run_id == "test_run_123" |
| 875 | + assert result.runs[0].graph_name == "test_graph" |
| 876 | + assert result.runs[0].status == RunStatusEnum.SUCCESS |
| 877 | + assert result.runs[0].total_count == 8 |
| 878 | + |
| 879 | + # Verify second run details |
| 880 | + assert result.runs[1].run_id == "test_run_456" |
| 881 | + assert result.runs[1].graph_name == "production_graph" |
| 882 | + assert result.runs[1].status == RunStatusEnum.PENDING |
| 883 | + assert result.runs[1].total_count == 16 |
| 884 | + |
| 885 | + @patch('app.routes.get_runs') |
| 886 | + async def test_get_runs_route_pagination_and_edge_cases(self, mock_get_runs, mock_request): |
| 887 | + """Test get_runs_route with different pagination scenarios and edge cases""" |
| 888 | + from app.routes import get_runs_route |
| 889 | + from app.models.run_models import RunsResponse, RunListItem, RunStatusEnum |
| 890 | + from datetime import datetime |
| 891 | + |
| 892 | + # Test case 1: Empty results (page 2 with no data) |
| 893 | + mock_get_runs.return_value = RunsResponse( |
| 894 | + namespace="test_namespace", |
| 895 | + total=5, |
| 896 | + page=2, |
| 897 | + size=10, |
| 898 | + runs=[] |
| 899 | + ) |
| 900 | + |
| 901 | + result = await get_runs_route("test_namespace", 2, 10, mock_request, "valid_key") |
| 902 | + |
| 903 | + mock_get_runs.assert_called_with("test_namespace", 2, 10, "test-request-id") |
| 904 | + assert result.namespace == "test_namespace" |
| 905 | + assert result.total == 5 |
| 906 | + assert result.page == 2 |
| 907 | + assert result.size == 10 |
| 908 | + assert len(result.runs) == 0 |
| 909 | + |
| 910 | + # Test case 2: Single result with different page size |
| 911 | + mock_run = MagicMock(spec=RunListItem) |
| 912 | + mock_run.run_id = "single_run_789" |
| 913 | + mock_run.graph_name = "single_graph" |
| 914 | + mock_run.success_count = 1 |
| 915 | + mock_run.pending_count = 0 |
| 916 | + mock_run.errored_count = 0 |
| 917 | + mock_run.retried_count = 0 |
| 918 | + mock_run.total_count = 1 |
| 919 | + mock_run.status = RunStatusEnum.SUCCESS |
| 920 | + mock_run.created_at = datetime(2024, 1, 15, 12, 0, 0) |
| 921 | + |
| 922 | + mock_get_runs.return_value = RunsResponse( |
| 923 | + namespace="test_namespace", |
| 924 | + total=1, |
| 925 | + page=1, |
| 926 | + size=5, |
| 927 | + runs=[mock_run] |
| 928 | + ) |
| 929 | + |
| 930 | + result = await get_runs_route("test_namespace", 1, 5, mock_request, "valid_key") |
| 931 | + |
| 932 | + mock_get_runs.assert_called_with("test_namespace", 1, 5, "test-request-id") |
| 933 | + assert result.namespace == "test_namespace" |
| 934 | + assert result.total == 1 |
| 935 | + assert result.page == 1 |
| 936 | + assert result.size == 5 |
| 937 | + assert len(result.runs) == 1 |
| 938 | + assert result.runs[0].run_id == "single_run_789" |
| 939 | + assert result.runs[0].status == RunStatusEnum.SUCCESS |
| 940 | + |
| 941 | + @patch('app.routes.get_runs') |
| 942 | + async def test_get_runs_route_service_error(self, mock_get_runs, mock_request): |
| 943 | + """Test get_runs_route when service raises an exception""" |
| 944 | + from app.routes import get_runs_route |
| 945 | + |
| 946 | + # Arrange - Mock service to raise an exception |
| 947 | + mock_get_runs.side_effect = Exception("Database connection error") |
| 948 | + |
| 949 | + # Act & Assert - Test error handling when service fails |
| 950 | + with pytest.raises(Exception) as exc_info: |
| 951 | + await get_runs_route("test_namespace", 1, 10, mock_request, "valid_key") |
| 952 | + |
| 953 | + assert str(exc_info.value) == "Database connection error" |
| 954 | + mock_get_runs.assert_called_once_with("test_namespace", 1, 10, "test-request-id") |
832 | 955 |
|
833 | 956 | @patch('app.routes.get_runs') |
834 | 957 | async def test_get_runs_route_with_invalid_api_key(self, mock_get_runs, mock_request): |
|
0 commit comments