@@ -234,22 +234,43 @@ def test_compare_versions_missing_parameters(self, client, sample_with_version):
234234 """Test comparing versions with missing parameters."""
235235 refcode = sample_with_version .refcode .split (":" )[1 ]
236236
237- # Missing v2
237+ # Missing v2 - request.args.get() returns "" for missing params, which fails ObjectId validation
238238 response = client .get (f"/items/{ refcode } /compare-versions/?v1=some_id" )
239239 assert response .status_code == 400
240- assert "Both v1 and v2 must be provided" in response .json ["message" ]
241-
242- # Missing v1
240+ assert response .json ["message" ] == "Invalid query parameters"
241+ assert "errors" in response .json
242+ errors = response .json ["errors" ]
243+ # Should have error for v2 (empty string is invalid ObjectId)
244+ v2_errors = [e for e in errors if "v2" in str (e ["loc" ])]
245+ assert len (v2_errors ) == 1
246+ assert "valid objectid" in v2_errors [0 ]["msg" ].lower ()
247+
248+ # Missing v1 - same behavior
243249 response = client .get (f"/items/{ refcode } /compare-versions/?v2=some_id" )
244250 assert response .status_code == 400
251+ assert response .json ["message" ] == "Invalid query parameters"
252+ assert "errors" in response .json
253+ errors = response .json ["errors" ]
254+ # Should have error for v1 (empty string is invalid ObjectId)
255+ v1_errors = [e for e in errors if "v1" in str (e ["loc" ])]
256+ assert len (v1_errors ) == 1
257+ assert "valid objectid" in v1_errors [0 ]["msg" ].lower ()
245258
246259 def test_compare_versions_invalid_id (self , client , sample_with_version ):
247260 """Test comparing versions with invalid ID format."""
248261 refcode = sample_with_version .refcode .split (":" )[1 ]
249262 response = client .get (f"/items/{ refcode } /compare-versions/?v1=invalid&v2=invalid" )
250263
251264 assert response .status_code == 400
252- assert "Invalid version ID format" in response .json ["message" ]
265+ assert response .json ["message" ] == "Invalid query parameters"
266+ # Check Pydantic's structured error response
267+ assert "errors" in response .json
268+ errors = response .json ["errors" ]
269+ # Should have errors for both v1 and v2
270+ assert len (errors ) == 2
271+ for error in errors :
272+ assert error ["loc" ][0 ] in ["v1" , "v2" ]
273+ assert "valid ObjectId" in error ["msg" ]
253274
254275 def test_compare_versions_detects_changes (self , client , sample_with_version ):
255276 """Test that compare_versions properly detects changes using DeepDiff."""
@@ -417,15 +438,27 @@ def test_restore_version_missing_version_id(self, client, sample_with_version):
417438 response = client .post (f"/items/{ refcode } /restore-version/" , json = {})
418439
419440 assert response .status_code == 400
420- assert "version_id must be provided" in response .json ["message" ]
441+ assert response .json ["message" ] == "Invalid request body"
442+ # Check Pydantic's structured error response
443+ assert "errors" in response .json
444+ errors = response .json ["errors" ]
445+ assert len (errors ) == 1
446+ assert errors [0 ]["loc" ] == ["version_id" ]
447+ assert "required" in errors [0 ]["msg" ].lower ()
421448
422449 def test_restore_version_invalid_id (self , client , sample_with_version ):
423450 """Test restoring with invalid version ID."""
424451 refcode = sample_with_version .refcode .split (":" )[1 ]
425452 response = client .post (f"/items/{ refcode } /restore-version/" , json = {"version_id" : "invalid" })
426453
427454 assert response .status_code == 400
428- assert "Invalid version_id" in response .json ["message" ]
455+ assert response .json ["message" ] == "Invalid request body"
456+ # Check Pydantic's structured error response
457+ assert "errors" in response .json
458+ errors = response .json ["errors" ]
459+ assert len (errors ) == 1
460+ assert errors [0 ]["loc" ] == ["version_id" ]
461+ assert "valid ObjectId" in errors [0 ]["msg" ]
429462
430463 def test_restore_version_nonexistent (self , client , sample_with_version ):
431464 """Test restoring non-existent version."""
0 commit comments