@@ -323,3 +323,65 @@ async def test_aggregate_movies_by_year(self, client):
323323 assert "lowestRating" in first_result
324324 assert "totalVotes" in first_result
325325
326+ @pytest .mark .asyncio
327+ async def test_aggregate_movies_by_comments (self , client ):
328+ """
329+ Test aggregation reporting by comments.
330+
331+ This test demonstrates:
332+ - $lookup aggregation (joining collections)
333+ - Testing against existing dataset with comments
334+ - Validating nested data structures
335+ """
336+ response = await client .get ("/api/movies/aggregations/reportingByComments?limit=5" )
337+
338+ assert response .status_code == 200
339+ data = response .json ()
340+ assert data ["success" ] is True
341+
342+ # Should return movies that have comments
343+ if len (data ["data" ]) > 0 :
344+ first_result = data ["data" ][0 ]
345+ # Validate structure of aggregation results
346+ assert "_id" in first_result
347+ assert "title" in first_result
348+ assert "year" in first_result
349+ assert "totalComments" in first_result
350+ assert "recentComments" in first_result
351+ assert isinstance (first_result ["recentComments" ], list )
352+
353+ # If there are recent comments, validate their structure
354+ if len (first_result ["recentComments" ]) > 0 :
355+ comment = first_result ["recentComments" ][0 ]
356+ assert "userName" in comment
357+ assert "userEmail" in comment
358+ assert "text" in comment
359+ assert "date" in comment
360+
361+ @pytest .mark .asyncio
362+ async def test_aggregate_directors_most_movies (self , client ):
363+ """
364+ Test aggregation reporting by directors.
365+
366+ This test demonstrates:
367+ - $unwind aggregation (array flattening)
368+ - Grouping and sorting operations
369+ - Testing against existing dataset
370+ """
371+ response = await client .get ("/api/movies/aggregations/reportingByDirectors?limit=10" )
372+
373+ assert response .status_code == 200
374+ data = response .json ()
375+ assert data ["success" ] is True
376+ assert len (data ["data" ]) > 0
377+
378+ # Validate structure of aggregation results
379+ first_result = data ["data" ][0 ]
380+ assert "director" in first_result
381+ assert "movieCount" in first_result
382+ assert "averageRating" in first_result
383+
384+ # Verify results are sorted by movieCount (descending)
385+ if len (data ["data" ]) > 1 :
386+ assert data ["data" ][0 ]["movieCount" ] >= data ["data" ][1 ]["movieCount" ]
387+
0 commit comments