From e98a3efe57cf60b54ade299d30c9889e5a879eb5 Mon Sep 17 00:00:00 2001 From: Samir Boulahtit Date: Sun, 7 Dec 2025 18:28:59 +0100 Subject: [PATCH] fix: correct failing unit tests for product and import job services MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - test_create_import_job_database_error: Monkeypatch db.flush instead of db.commit since service uses flush() per architecture rules - test_create_product_already_exists: Store product_id before exception and rollback session to clear PendingRollbackError state - test_delete_product_success: Add db.flush() to delete_product service method to ensure deletion is applied before verification 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- app/services/marketplace_product_service.py | 1 + tests/unit/services/test_marketplace_service.py | 6 +++--- tests/unit/services/test_product_service.py | 12 +++++++++--- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/app/services/marketplace_product_service.py b/app/services/marketplace_product_service.py index bc5195d1..c8690fec 100644 --- a/app/services/marketplace_product_service.py +++ b/app/services/marketplace_product_service.py @@ -306,6 +306,7 @@ class MarketplaceProductService: db.query(Inventory).filter(Inventory.gtin == product.gtin).delete() db.delete(product) + db.flush() logger.info(f"Deleted product {marketplace_product_id}") return True diff --git a/tests/unit/services/test_marketplace_service.py b/tests/unit/services/test_marketplace_service.py index f828b9f6..505db687 100644 --- a/tests/unit/services/test_marketplace_service.py +++ b/tests/unit/services/test_marketplace_service.py @@ -59,10 +59,10 @@ class TestMarketplaceImportJobService: marketplace="Amazon", ) - def mock_commit(): - raise Exception("Database commit failed") + def mock_flush(): + raise Exception("Database flush failed") - monkeypatch.setattr(db, "commit", mock_commit) + monkeypatch.setattr(db, "flush", mock_flush) with pytest.raises(ValidationException) as exc_info: self.service.create_import_job(db, request, test_vendor, test_user) diff --git a/tests/unit/services/test_product_service.py b/tests/unit/services/test_product_service.py index 5d5f720e..ae7bd3dc 100644 --- a/tests/unit/services/test_product_service.py +++ b/tests/unit/services/test_product_service.py @@ -87,8 +87,11 @@ class TestProductService: def test_create_product_already_exists(self, db, test_marketplace_product): """Test creating product with existing ID raises MarketplaceProductAlreadyExistsException""" + # Store the product ID before the exception (session may be invalid after) + existing_product_id = test_marketplace_product.marketplace_product_id + product_data = MarketplaceProductCreate( - marketplace_product_id=test_marketplace_product.marketplace_product_id, # Use existing product ID + marketplace_product_id=existing_product_id, # Use existing product ID title="Duplicate MarketplaceProduct", price="29.99", ) @@ -96,12 +99,15 @@ class TestProductService: with pytest.raises(MarketplaceProductAlreadyExistsException) as exc_info: self.service.create_product(db, product_data) + # Rollback to clear the session's invalid state from IntegrityError + db.rollback() + assert exc_info.value.error_code == "PRODUCT_ALREADY_EXISTS" - assert test_marketplace_product.marketplace_product_id in str(exc_info.value) + assert existing_product_id in str(exc_info.value) assert exc_info.value.status_code == 409 assert ( exc_info.value.details.get("marketplace_product_id") - == test_marketplace_product.marketplace_product_id + == existing_product_id ) def test_create_product_invalid_price(self, db):