@@ -1091,3 +1091,82 @@ async def test_aggregate_directors_empty_results(self, mock_execute_aggregation)
10911091 # Assertions
10921092 assert result .success is True
10931093 assert len (result .data ) == 0
1094+
1095+
1096+ @pytest .mark .unit
1097+ @pytest .mark .asyncio
1098+ class TestGetDistinctGenres :
1099+ """Tests for GET /api/movies/genres endpoint."""
1100+
1101+ @patch ('src.routers.movies.get_collection' )
1102+ async def test_get_distinct_genres_success (self , mock_get_collection ):
1103+ """Should return list of distinct genres sorted alphabetically."""
1104+ # Setup mock
1105+ mock_collection = AsyncMock ()
1106+ mock_collection .distinct .return_value = ["Drama" , "Action" , "Comedy" , "Horror" , "Sci-Fi" ]
1107+ mock_get_collection .return_value = mock_collection
1108+
1109+ # Call the route handler
1110+ from src .routers .movies import get_distinct_genres
1111+ result = await get_distinct_genres ()
1112+
1113+ # Assertions
1114+ assert result .success is True
1115+ assert len (result .data ) == 5
1116+ # Verify alphabetical sorting
1117+ assert result .data == ["Action" , "Comedy" , "Drama" , "Horror" , "Sci-Fi" ]
1118+ mock_collection .distinct .assert_called_once_with ("genres" )
1119+
1120+ @patch ('src.routers.movies.get_collection' )
1121+ async def test_get_distinct_genres_empty_list (self , mock_get_collection ):
1122+ """Should return empty list when no genres exist."""
1123+ # Setup mock
1124+ mock_collection = AsyncMock ()
1125+ mock_collection .distinct .return_value = []
1126+ mock_get_collection .return_value = mock_collection
1127+
1128+ # Call the route handler
1129+ from src .routers .movies import get_distinct_genres
1130+ result = await get_distinct_genres ()
1131+
1132+ # Assertions
1133+ assert result .success is True
1134+ assert len (result .data ) == 0
1135+
1136+ @patch ('src.routers.movies.get_collection' )
1137+ async def test_get_distinct_genres_filters_null_and_empty (self , mock_get_collection ):
1138+ """Should filter out null and empty genre values."""
1139+ # Setup mock
1140+ mock_collection = AsyncMock ()
1141+ mock_collection .distinct .return_value = ["Action" , None , "" , "Drama" , "Comedy" ]
1142+ mock_get_collection .return_value = mock_collection
1143+
1144+ # Call the route handler
1145+ from src .routers .movies import get_distinct_genres
1146+ result = await get_distinct_genres ()
1147+
1148+ # Assertions
1149+ assert result .success is True
1150+ assert len (result .data ) == 3
1151+ assert "Action" in result .data
1152+ assert "Drama" in result .data
1153+ assert "Comedy" in result .data
1154+ assert None not in result .data
1155+ assert "" not in result .data
1156+
1157+ @patch ('src.routers.movies.get_collection' )
1158+ async def test_get_distinct_genres_database_error (self , mock_get_collection ):
1159+ """Should handle database errors gracefully."""
1160+ # Setup mock to raise exception
1161+ mock_collection = AsyncMock ()
1162+ mock_collection .distinct .side_effect = Exception ("Database connection failed" )
1163+ mock_get_collection .return_value = mock_collection
1164+
1165+ # Call the route handler
1166+ from src .routers .movies import get_distinct_genres
1167+ with pytest .raises (HTTPException ) as exc_info :
1168+ await get_distinct_genres ()
1169+
1170+ # Assertions
1171+ assert exc_info .value .status_code == 500
1172+ assert "Database error" in str (exc_info .value .detail )
0 commit comments