Skip to content

Conversation

@codesungrape
Copy link
Collaborator

Description

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

This change finalizes the transition of the GET /books/{id} endpoint. The endpoint now retrieves book details directly from the MongoDB database using a find_one() query, replacing the previous in-memory or mock implementation. It includes an updated, comprehensive test suite to achieve 100% test coverage for the new logic.

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 GET /books/{id} endpoint, transitioning from an in-memory implementation to a database-backed solution. The endpoint now queries MongoDB directly using ObjectId validation and proper error handling.

  • Replace in-memory book lookup with MongoDB collection.find_one() queries
  • Add ObjectId validation for book IDs with appropriate 400 error responses
  • Implement comprehensive test suite with both unit and integration tests covering all error scenarios

Reviewed Changes

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

File Description
app/routes.py Replaces in-memory book lookup with MongoDB queries, adds ObjectId validation and proper error handling
tests/test_app.py Adds comprehensive test coverage with unit tests using mocks and integration tests with real database operations
tests/conftest.py Introduces db_setup fixture for clean database state management in tests
openapi.yml Updates API documentation to reflect new error responses (400, 404, 500) with proper schemas

mock_collection.find_one.assert_called_once()
mock_collection.find_one.assert_called_once_with(
{"_id": fake_book_id, "state": {"$ne": "deleted"}}
) # pylint: disable=line-too-long
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

[nitpick] The pylint disable comment for line-too-long is unnecessary. The line is well within reasonable limits and the comment adds clutter.

Suggested change
) # pylint: disable=line-too-long
)

Copilot uses AI. Check for mistakes.
# Check that the JSON error message is exactly what the code returns
expected_error = {"error": "Invalid book ID format"}
assert response.get_json() == expected_error

Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

[nitpick] This function is missing a blank line before the function definition, which is inconsistent with the formatting of other test functions in the file.

Suggested change

Copilot uses AI. Check for mistakes.
# Query db for a non-deleted book
query = {"_id": obj_id, "state": {"$ne": "deleted"}}
# look it up in MongoDB
book = collection.find_one(query)
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

Consider adding exception handling around the MongoDB query. Database operations can raise exceptions (ConnectionFailure, ServerSelectionTimeoutError) that should be caught and converted to appropriate HTTP error responses.

Copilot uses AI. Check for mistakes.
Comment on lines +159 to +160
# also equivalent to Key version
# book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"})
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

[nitpick] The comment 'also equivalent to Key version' is unclear and doesn't add value. Consider removing it or making it more descriptive.

Suggested change
# also equivalent to Key version
# book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"})
# Alternative query to fetch a non-deleted book by its ID
# book = collection.find_one({"_id": obj_id, "state": {"$ne": "deleted"}})

Copilot uses AI. Check for mistakes.
# look it up in MongoDB
book = collection.find_one(query)
# also equivalent to Key version
# book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"})
Copy link

Copilot AI Jul 30, 2025

Choose a reason for hiding this comment

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

This commented-out code should be removed as it serves no purpose and adds confusion. The variable name 'raw_books' doesn't match the actual collection variable name.

Suggested change
# book = raw_books.find_one(_id=obj_id, state={"$ne": "deleted"})

Copilot uses AI. Check for mistakes.
@codesungrape codesungrape merged commit 56dbf0d into main Jul 30, 2025
2 checks passed
@codesungrape codesungrape deleted the Complete-MongoDB-Integration-for-GET-/books/{id}) branch July 30, 2025 14:28
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