Skip to content

Add more tests#74

Merged
ludavidca merged 25 commits intomainfrom
colin/add-tests
Feb 12, 2026
Merged

Add more tests#74
ludavidca merged 25 commits intomainfrom
colin/add-tests

Conversation

@ColinToft
Copy link
Collaborator

@ColinToft ColinToft commented Dec 30, 2025

Implementation description

  • Adds more tests to API routes
  • Add a test for the seeding script
  • Fixes issue where tests used the actual DB (not the test DB) which wiped out actual info stored in the local DB

Steps to test

  1. Ensure all API routes are covered

What should reviewers focus on?

Checklist

  • My PR name is descriptive and in imperative tense
  • My commit messages are descriptive and in imperative tense. My commits are atomic and trivial commits are squashed or fixup'd into non-trivial commits
  • I have requested a review from the PL, as well as other devs who have background knowledge on this PR or who will be building on top of this PR

@ColinToft ColinToft requested a review from ludavidca December 30, 2025 17:30
@ColinToft ColinToft changed the base branch from main to hylac/restructure-user-system December 30, 2025 17:31
@ColinToft ColinToft changed the title Add more tests [DO NOT MERGE] Add more tests Dec 30, 2025
@ColinToft ColinToft changed the base branch from hylac/restructure-user-system to main December 30, 2025 17:53
@ColinToft ColinToft changed the base branch from main to hylac/restructure-user-system December 30, 2025 17:53
@ColinToft ColinToft changed the base branch from hylac/restructure-user-system to main December 30, 2025 17:54
@ColinToft ColinToft changed the base branch from main to hylac/restructure-user-system December 30, 2025 20:57
@ludavidca
Copy link
Collaborator

@claude please review this PR

@claude
Copy link

claude bot commented Dec 31, 2025

Code Review - PR #74: Add more tests

Summary

This PR adds comprehensive test coverage for API routes and the database seeding script. The changes also fix a critical bug where tests were using the production database instead of a test database, and improve error handling and code organization across several service implementations.

Positive Aspects ✅

Critical Bug Fix

  • Fixed test database isolation (conftest.py:39): Changed from f4k to f4k_test database - this is crucial and prevents tests from wiping production data
  • Simplified override_get_session fixture implementation - removed unnecessary nested function wrapper

Excellent Test Coverage

  • Added 400+ lines of comprehensive route tests covering:
    • Driver CRUD operations with proper relationship handling
    • Location CRUD operations
    • Route operations
    • Route group operations with date filtering
    • Proper error handling (404s, validation errors)
  • Added 275 lines of seeding script tests with schema validation
  • Test organization is clean with proper class grouping

Code Quality Improvements

  • Driver service improvements (driver_service.py): Added selectinload(Driver.user) to prevent N+1 query issues
  • Better serialization (driver_routes.py:19-35): Created dedicated driver_to_driver_read() converter instead of relying on model_validate(), which is more reliable
  • Improved docstrings: Replaced vague "Modern FastAPI-style" comments with descriptive service purposes
  • Better error handling (route_group_routes.py): Added explicit HTTPException re-raise to preserve 404 responses

Testing Infrastructure

  • Created test CSV data for locations (test_locations.csv) with 30+ test records
  • Made CSV path configurable via environment variable for testing flexibility
  • Fixed duplicate email issue in seed script with proper uniqueness checking

Issues & Concerns 🔴

1. Security: Removed User Authentication Check (.github/workflows/claude-code-review.yml:15-17)

- if: |
-   github.actor == 'ludavidca'

Impact: HIGH - This workflow now runs for ALL pull requests from ANY user
Risk: Unauthorized users could trigger this workflow, potentially consuming resources or exposing workflow behavior
Recommendation: Either restore the user check or add proper authentication/authorization

2. Potential Data Race in Seed Script (seed_database.py:511-522)

email = fake.email()
max_email_attempts = 100
email_attempts = 0
while email in used_emails and email_attempts < max_email_attempts:
    email = fake.email()
    email_attempts += 1
used_emails.add(email)

Issue: If loop exhausts attempts (100), duplicate email is still added
Fix: Add validation after loop:

if email_attempts >= max_email_attempts:
    raise ValueError(f"Could not generate unique email after {max_email_attempts} attempts")

3. Database Transaction Management Issue (conftest.py:88-99)

async with async_session() as session:
    transaction = await session.begin()
    try:
        yield session
    finally:
        with contextlib.suppress(Exception):
            await transaction.rollback()

Issue: If session.begin() is called but session already has an active transaction, this could cause issues
Also: The transaction rollback happens in fixture, but tests do explicit commits. The rollback may not properly clean up committed changes
Recommendation: Consider using nested transactions or ensure tests don't commit

4. Incomplete Error Handling (driver_routes.py:59-73)

The get_drivers() endpoint has inconsistent error handling:

  • Returns 404 when filtering by specific ID/email
  • But returns 500 for generic errors via catch-all
    Issue: Generic database errors leak to 500 responses without logging
    Fix: Add specific error logging before re-raising

5. Missing Validation (route_group_routes.py:37-41)

data = RouteGroupRead.model_validate(
    route_group, from_attributes=True
).model_dump()
if include_routes:
    data["routes"] = [...]

Issue: Removed num_routes field calculation that was in the original code
Impact: API response changed - breaking change if clients depend on num_routes
Check: Verify if RouteGroupRead model now computes this field automatically

6. Test Database Cleanup (conftest.py:66-74)

await conn.run_sync(SQLModel.metadata.drop_all)
await conn.run_sync(SQLModel.metadata.create_all)
# ...
yield engine
# ...
await conn.run_sync(SQLModel.metadata.drop_all)

Issue: Drop/create happens per function scope - could be slow for large test suites
Recommendation: Consider session-level table creation with function-level transaction rollback


Performance Considerations ⚡

N+1 Query Prevention

The addition of selectinload(Driver.user) is excellent and prevents N+1 queries when fetching drivers. Similar optimization should be applied to other relationships:

  • RouteStopLocation
  • DriverAssignmentDriver, Route
  • RouteRouteStop

Test Performance

  • Seed database test runs the full seeding script which creates 100+ entities
  • Consider splitting into unit tests for individual seeding functions
  • Current approach is good for integration testing but may be slow

Test Coverage Gaps 🧪

Missing test scenarios:

  1. Concurrent driver creation with same email - tests uniqueness constraint
  2. Invalid UUID formats in path parameters
  3. Malformed JSON in request bodies
  4. Query parameter validation (e.g., invalid date formats for route groups)
  5. Pagination/filtering edge cases (empty filters, invalid filter values)
  6. Authentication/authorization - all tests bypass auth checks
  7. Rate limiting behavior (if implemented)

Code Quality Suggestions 💡

1. Type Hints (driver_to_driver_read:14)

def driver_to_driver_read(driver: Any) -> DriverRead:

Should be:

def driver_to_driver_read(driver: Driver) -> DriverRead:

2. Magic Numbers

  • max_email_attempts = 100 should be a module-level constant
  • MIN_DRIVERS constant is referenced but not visible in diff

3. Error Messages

Route group errors at route_group_routes.py:104, 129 say "RouteGroup with id {id} not found" but should be consistent with other error messages that don't capitalize entity names

4. Test Data Management

Consider using factory pattern (factory_boy) instead of fixtures for test data - more flexible and reduces code duplication


Security Considerations 🔒

  1. SQL Injection: ✅ Using SQLAlchemy ORM properly prevents SQL injection
  2. Input Validation: ⚠️ Tests don't verify input sanitization (XSS in notes/name fields)
  3. Authentication: ⚠️ Tests bypass auth - ensure auth is tested separately
  4. Rate Limiting: ❓ Not visible if implemented
  5. Removed workflow restriction: 🔴 See issue Add starter code + FastAPI #1 above

Dependencies 📦

Matplotlib Version Bump (requirements.txt:77)

-matplotlib==3.10.0
+matplotlib>=3.10.8

Question: Why the change from exact version to minimum version?
Risk: Could introduce breaking changes in patch versions
Recommendation: Use ~=3.10.8 (compatible release) or >=3.10.8,<3.11.0 to prevent major version bumps


Recommendations

Must Fix Before Merge

  1. ✅ Restore authentication check in GitHub workflow or document why it was removed
  2. ✅ Fix email uniqueness validation in seed script
  3. ✅ Verify num_routes field removal doesn't break existing clients
  4. ✅ Add error handling for edge case where email generation fails

Should Fix

  1. Improve transaction management in test fixtures
  2. Add type hints for driver parameter
  3. Clarify matplotlib version constraint reasoning
  4. Add logging for database errors in route handlers

Nice to Have

  1. Add missing test coverage (auth, invalid inputs, edge cases)
  2. Consider performance optimization for test suite
  3. Add relationship eager loading to other services
  4. Use factory pattern for test data

Overall Assessment

Code Quality: ⭐⭐⭐⭐ (4/5)
Test Coverage: ⭐⭐⭐⭐ (4/5)
Security: ⭐⭐⭐ (3/5) - workflow auth removal is concerning
Performance: ⭐⭐⭐⭐ (4/5) - good N+1 prevention

Verdict: This is a strong PR that significantly improves test coverage and fixes a critical database isolation bug. However, the workflow authentication removal and a few edge cases in error handling should be addressed before merging.

The test infrastructure is well-designed and the eager loading improvements are excellent. Great work on the comprehensive route testing!

🤖 Generated with Claude Code

Copy link
Collaborator

@ludavidca ludavidca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a couple of comments, but should be fine once we address C Dawg's (Claude's) review + a couple of small nits from me. Happy New Year!

Comprehensive integration tests for API routes.

Tests cover:
- Driver routes (CRUD operations)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that these are missing a couple of other api routes, driver history, driver deletion etc. Do we want 100% test coverage, if you're just targeting core functionalities, then I think it should be okay.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it also because we're making schema changes to the db as well (which requires modifying the tests)

@ludavidca ludavidca changed the base branch from hylac/restructure-user-system to main February 12, 2026 04:11
@ludavidca ludavidca changed the title [DO NOT MERGE] Add more tests Add more tests Feb 12, 2026
@ludavidca ludavidca self-requested a review February 12, 2026 04:34
Copy link
Collaborator

@ludavidca ludavidca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LFGTM

@ludavidca ludavidca merged commit 62496c3 into main Feb 12, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants