Skip to content

Commit 833321f

Browse files
committed
Add integration tests for remaining aggregation APIs
1 parent 050539d commit 833321f

File tree

2 files changed

+70
-8
lines changed

2 files changed

+70
-8
lines changed

server/python/tests/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ The test suite is organized into three categories:
2929
- Requires a running MongoDB instance with MFlix dataset
3030
- Uses a real server running in a subprocess
3131
- Tests are idempotent (clean up after themselves)
32-
- **8 tests** (7 passing, 1 skipped due to known bug) covering:
33-
- End-to-end CRUD operations
34-
- Search against real data
35-
- Batch operations with cleanup
36-
- Aggregation queries
32+
- **10 tests** covering:
33+
- CRUD operations
34+
- Batch operations
35+
- Search functionality
36+
- Aggregation pipelines
3737

3838
## Running Tests
3939

@@ -56,23 +56,23 @@ The test suite is organized into three categories:
5656
pytest tests/ -v
5757
```
5858

59-
**Expected output:** 69 passed in ~5 seconds
59+
**Expected output:** 71 passed in ~6 seconds
6060

6161
### Run Only Unit Tests (Fast, No Database Required)
6262

6363
```bash
6464
pytest -m unit -v
6565
```
6666

67-
**Expected output:** 61 passed, 8 deselected in ~1 second
67+
**Expected output:** 61 passed, 10 deselected in ~1.5 seconds
6868

6969
### Run Only Integration Tests (Requires Database)
7070

7171
```bash
7272
pytest -m integration -v
7373
```
7474

75-
**Expected output:** 8 passed, 61 deselected in ~4 seconds
75+
**Expected output:** 10 passed, 61 deselected in ~5 seconds
7676

7777
### Run Specific Test File
7878

server/python/tests/integration/test_movie_routes_integration.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)