Skip to content

Conversation

@codesungrape
Copy link
Collaborator

@codesungrape codesungrape commented Aug 4, 2025

Description

Trello ticket: https://trello.com/c/O9jraVyM

This PR completes the MongoDB integration for the PUT /books/{id} endpoint by implementing proper payload validation and replacing the existing in-memory approach with database operations. The changes introduce comprehensive validation logic and transition from updating objects in a Python list to using MongoDB's replace_one operation.

  • Adds new validation helper function validate_book_put_payload() to enforce strict field requirements
  • Implements replace_book_by_id() helper to perform MongoDB document replacement
  • Updates the PUT endpoint to use MongoDB operations instead of in-memory list manipulation

Fixes # (issue number if applicable)

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Code refactor (improving code quality without changing functionality)
  • Performance improvement
  • Other (please describe):

How Has This Been Tested?

Automated tests, manual testing, cURL, CI/CD pipeline

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • My individual commit messages are descriptive and follow our commit guidelines
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR completes the MongoDB integration for the PUT /books/{id} endpoint by implementing proper payload validation and replacing the existing in-memory approach with database operations. The changes introduce comprehensive validation logic and transition from updating objects in a Python list to using MongoDB's replace_one operation.

  • Adds new validation helper function validate_book_put_payload() to enforce strict field requirements
  • Implements replace_book_by_id() helper to perform MongoDB document replacement
  • Updates the PUT endpoint to use MongoDB operations instead of in-memory list manipulation

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
app/datastore/mongo_helper.py Adds validation and replacement helper functions for PUT operations
app/routes.py Refactors PUT endpoint to use MongoDB helpers and proper error handling
tests/test_mongo_helper.py Comprehensive test coverage for new helper functions
tests/test_app.py Updated integration tests for PUT endpoint with MongoDB mocking
tests/test_api_security.py Security tests updated to work with MongoDB operations
tests/test_app_utils_helper.py New utility function tests for hostname appending
tests/test_data.py Common test constants extracted to shared module
openapi.yml Updated API documentation with detailed error responses
Comments suppressed due to low confidence (1)

tests/test_app_utils_helper.py:130

  • Typo in comment: 'monkeypatcj' should be 'monkeypatch'.
    monkeypatch.setattr(routes, "get_book_collection", lambda: fake_collection)

expected_error_message = "Unexpected fields provided: rating"
assert error_dict["error"] == expected_error_message

def test_validate_payload_fails_with_multiple_extra_fields():
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

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

Missing blank line before function definition. There should be two blank lines between function definitions at module level according to PEP 8.

Copilot uses AI. Check for mistakes.
), f"Link should end with the resource path '{expected_path}'"
headers_with_bad_body = {
"Content-Type": "application/json",
"X-API-KEY": "test-key-123"
Copy link

Copilot AI Aug 4, 2025

Choose a reason for hiding this comment

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

Trailing whitespace after the string literal. This should be removed for consistency.

Copilot uses AI. Check for mistakes.
codesungrape and others added 5 commits August 4, 2025 10:54
Fix test with correct assertion (replace_one())

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
unpack with '_' vs kwargs, indicate intentional throwaway

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Use pymongo Collection type for book_collection annotation

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@codesungrape codesungrape merged commit 1747121 into main Aug 4, 2025
2 checks passed
@codesungrape codesungrape deleted the Complete-MongoDB-Integration-for-PUT-/books/{id}) branch August 4, 2025 13:17
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