@@ -428,7 +428,9 @@ def test_get_book_returns_specified_book(
428428 assert response_data ["author" ] == "Kent Beck"
429429
430430
431- def test_get_book_with_invalid_id_format_returns_400 (client , db_setup ): # pylint: disable=unused-argument
431+ def test_get_book_with_invalid_id_format_returns_400 (
432+ client , db_setup
433+ ): # pylint: disable=unused-argument
432434 # Arrange
433435 # an ID that is clearly not a valid MongoDB ObjectId
434436 invalid_book_id = "this-is-not-a-valid-id"
@@ -444,6 +446,7 @@ def test_get_book_with_invalid_id_format_returns_400(client, db_setup): # pylin
444446 expected_error = {"error" : "Invalid book ID format" }
445447 assert response .get_json () == expected_error
446448
449+
447450def test_get_book_not_found_returns_404 (client , monkeypatch ):
448451 """
449452 WHEN given a well-formed but non-existent ObjectId,
@@ -508,18 +511,35 @@ def test_invalid_urls_return_404(client):
508511
509512# ------------------------ Tests for DELETE --------------------------------------------
510513
514+ VALID_OID_STRING = "635c02a7a5f6e1e2b3f4d5e6"
515+
511516
512517def test_book_is_soft_deleted_on_delete_request (client ):
513- with patch ("app.routes.books" , books_database ):
514- # Send DELETE request with valid API header
515- book_id = "1"
516- headers = {"X-API-KEY" : "test-key-123" }
517- response = client .delete (f"/books/{ book_id } " , headers = headers )
518+ """
519+ GIVEN a valid book ID and API key
520+ WHEN a DELETE request is made
521+ THEN the view function should call the database helper correctly and return 204.
522+
523+ This test verifies the integration between the Flask route and the data layer.
524+ """
525+ with patch ("app.routes.delete_book_by_id" ) as mock_delete_helper :
526+ # Arrange
527+ # Configure the mock to simulate a successful deletion
528+ mock_delete_helper .return_value = {"_id" : VALID_OID_STRING }
529+
530+ # Mock get_book_collection to avoid a real DB connection
531+ with patch ("app.routes.get_book_collection" , return_value = "fake_collection" ):
532+ # --- Act ---
533+ # Send the DELETE request using a valid API header.
534+ headers = {"X-API-KEY" : "test-key-123" }
535+ response = client .delete (f"/books/{ VALID_OID_STRING } " , headers = headers )
518536
519537 assert response .status_code == 204
520- assert response .data == b""
521- # check that the book's state has changed to deleted
522- assert books_database [0 ]["state" ] == "deleted"
538+ mock_delete_helper .assert_called_once ()
539+ mock_delete_helper .assert_called_once_with (
540+ "fake_collection" , # The (mocked) collection object
541+ VALID_OID_STRING , # The ID passed from the URL
542+ )
523543
524544
525545def test_delete_empty_book_id (client ):
@@ -531,19 +551,51 @@ def test_delete_empty_book_id(client):
531551
532552
533553def test_delete_invalid_book_id (client ):
534- headers = {"X-API-KEY" : "test-key-123" }
535- response = client .delete ("/books/12341234" , headers = headers )
536- assert response .status_code == 404
554+ """
555+ GIVEN a malformed book ID (not a valid ObjectId format)
556+ WHEN a DELETE request is made
557+ THEN the response should be 400 InvalidId Error.
558+ """
559+ invalid_id = "1234-this-is-not-a-valid-id"
560+
561+ # Mock get_book_collection to avoid a real DB connection
562+ with patch ("app.routes.get_book_collection" , return_value = "fake_collection" ):
563+ # --- Act ---
564+ # Send the DELETE request using a valid API header.
565+ headers = {"X-API-KEY" : "test-key-123" }
566+ response = client .delete (f"/books/{ invalid_id } " , headers = headers )
567+
568+ assert response .status_code == 400
537569 assert response .content_type == "application/json"
538- assert "Book not found" in response .get_json ()["error" ]
570+ response_data = response .get_json ()
571+ assert "error" in response_data
572+ assert "Invalid Book ID format" in response_data ["error" ]
539573
540574
541575def test_book_database_is_initialized_for_delete_book_route (client ):
542- with patch ("app.routes.books" , None ):
576+ with patch ("app.routes.get_book_collection" ) as mock_get_collection :
577+ mock_get_collection .return_value = None
578+
543579 headers = {"X-API-KEY" : "test-key-123" }
544- response = client .delete ("/books/1" , headers = headers )
580+ response = client .delete (f"/books/{ VALID_OID_STRING } " , headers = headers )
581+
545582 assert response .status_code == 500
546- assert "Book collection not initialized" in response .get_json ()["error" ]
583+ response_data = response .get_json ()
584+ assert "error" in response_data
585+ assert "Book collection not initialized" in response_data ["error" ]
586+
587+
588+ def test_returns_404_if_helper_function_result_is_none (client ):
589+ with patch ("app.routes.delete_book_by_id" ) as mock_delete_book :
590+ mock_delete_book .return_value = None
591+
592+ headers = {"X-API-KEY" : "test-key-123" }
593+ response = client .delete (f"/books/{ VALID_OID_STRING } " , headers = headers )
594+
595+ assert response .status_code == 404
596+ response_data = response .get_json ()
597+ assert "error" in response_data
598+ assert "Book not found" in response_data ["error" ]
547599
548600
549601# ------------------------ Tests for PUT --------------------------------------------
0 commit comments