Spaces:
Runtime error
Runtime error
| #!/usr/bin/env python3 | |
| """ | |
| Test script for Catalogue MongoDB-PostgreSQL synchronization. | |
| Verifies that catalogue CRUD operations sync correctly between databases. | |
| """ | |
| import asyncio | |
| import sys | |
| import os | |
| import json | |
| from datetime import datetime | |
| # Add the app directory to Python path | |
| sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'app')) | |
| from app.catalogues.schemas.schema import Catalogue, Identifier, Pricing, Tax, Inventory | |
| from app.catalogues.services.service import CatalogueService | |
| from app.catalogues.sync_service import CatalogueSyncService | |
| from app.catalogues.models.model import CatalogueRef | |
| async def test_catalogue_sync(): | |
| """Test catalogue synchronization between MongoDB and PostgreSQL""" | |
| print("π§ͺ Testing Catalogue MongoDB-PostgreSQL Sync") | |
| print("=" * 60) | |
| # Test data | |
| test_catalogue = Catalogue( | |
| catalogue_name="L'Oreal Professional Shampoo 500ml", | |
| catalogue_type="Product", | |
| brand="LOREAL", | |
| category="SHAMPOO", | |
| description="Professional grade shampoo for all hair types", | |
| identifiers=Identifier( | |
| sku="LOR-SHA-500ML", | |
| ean_code="1234567890123", | |
| barcode_number="123456789012" | |
| ), | |
| pricing=Pricing( | |
| retail_price=850.00, | |
| mrp=1000.00, | |
| currency="INR" | |
| ), | |
| tax=Tax( | |
| gst_rate=18.0, | |
| hsn_code="33051000" | |
| ), | |
| inventory=Inventory( | |
| track_inventory=True, | |
| batch_managed=True | |
| ) | |
| ) | |
| print("\nπ Test Catalogue Data:") | |
| print(f"Name: {test_catalogue.catalogue_name}") | |
| print(f"Brand: {test_catalogue.brand}") | |
| print(f"Category: {test_catalogue.category}") | |
| print(f"SKU: {test_catalogue.identifiers.sku}") | |
| print(f"MRP: βΉ{test_catalogue.pricing.mrp}") | |
| print(f"GST Rate: {test_catalogue.tax.gst_rate}%") | |
| # Test sync data extraction | |
| print(f"\nπ Testing Sync Data Extraction:") | |
| try: | |
| # This would require actual database connections | |
| print("β οΈ Database sync tests require MongoDB and PostgreSQL connections") | |
| print(" Run with actual database connections to test sync functionality") | |
| # Example of what would work with actual connections: | |
| # mongo_db = get_database() | |
| # async with async_session() as pg_session: | |
| # sync_service = CatalogueSyncService(pg_session) | |
| # | |
| # # Test data extraction | |
| # sync_data = sync_service._extract_sync_data(test_catalogue) | |
| # print(f"Extracted sync data: {json.dumps(sync_data, indent=2, default=str)}") | |
| # | |
| # # Test full CRUD sync | |
| # catalogue_service = CatalogueService(mongo_db, pg_session) | |
| # | |
| # # Create | |
| # created_catalogue = await catalogue_service.create_catalogue_item(test_catalogue, "test_user") | |
| # print(f"β Created catalogue: {created_catalogue.catalogue_id}") | |
| # | |
| # # Verify PostgreSQL sync | |
| # pg_record = await sync_service.get_catalogue_ref(created_catalogue.catalogue_id) | |
| # print(f"β PostgreSQL record exists: {pg_record is not None}") | |
| # | |
| # # Update | |
| # update_data = {"pricing.mrp": 1200.00} | |
| # await catalogue_service.update_catalogue_item(created_catalogue.catalogue_id, update_data, "test_user") | |
| # print(f"β Updated catalogue pricing") | |
| # | |
| # # Activate/Deactivate | |
| # await catalogue_service.deactivate_catalogue_item(created_catalogue.catalogue_id, "test_user") | |
| # print(f"β Deactivated catalogue") | |
| # | |
| # await catalogue_service.activate_catalogue_item(created_catalogue.catalogue_id, "test_user") | |
| # print(f"β Activated catalogue") | |
| # | |
| # # Delete | |
| # await catalogue_service.delete_catalogue_item(created_catalogue.catalogue_id) | |
| # print(f"β Deleted catalogue") | |
| # | |
| # # Verify PostgreSQL cleanup | |
| # pg_record_after = await sync_service.get_catalogue_ref(created_catalogue.catalogue_id) | |
| # print(f"β PostgreSQL record cleaned up: {pg_record_after is None}") | |
| except Exception as e: | |
| print(f"β Database test failed: {e}") | |
| return False | |
| print("\nβ Static tests completed!") | |
| return True | |
| def test_sync_data_extraction(): | |
| """Test sync data extraction without database""" | |
| print("\nπ Testing Sync Data Extraction (Static):") | |
| print("-" * 40) | |
| # Create test catalogue | |
| test_catalogue = Catalogue( | |
| catalogue_id="test-catalogue-123", | |
| catalogue_code="LOR-SHA-000001", | |
| catalogue_name="Test Shampoo", | |
| catalogue_type="Product", | |
| brand="LOREAL", | |
| category="SHAMPOO", | |
| identifiers=Identifier( | |
| sku="TEST-SKU-001", | |
| barcode_number="1234567890" | |
| ), | |
| pricing=Pricing( | |
| retail_price=500.00, | |
| mrp=600.00 | |
| ), | |
| tax=Tax( | |
| gst_rate=18.0, | |
| hsn_code="33051000" | |
| ) | |
| ) | |
| # Mock sync service for testing | |
| class MockSyncService(CatalogueSyncService): | |
| def __init__(self): | |
| pass # Skip database initialization | |
| sync_service = MockSyncService() | |
| sync_data = sync_service._extract_sync_data(test_catalogue) | |
| print("Extracted PostgreSQL sync data:") | |
| for key, value in sync_data.items(): | |
| print(f" {key}: {value}") | |
| # Verify expected fields | |
| expected_fields = [ | |
| "catalogue_id", "catalogue_code", "catalogue_type", "catalogue_name", | |
| "sku", "barcode_number", "hsn_code", "gst_rate", "mrp", "base_price", | |
| "track_inventory", "batch_managed", "status", "merchant_id" | |
| ] | |
| missing_fields = [field for field in expected_fields if field not in sync_data] | |
| if missing_fields: | |
| print(f"β Missing fields: {missing_fields}") | |
| return False | |
| print("β All expected fields present") | |
| return True | |
| def test_postgresql_schema(): | |
| """Test PostgreSQL schema definition""" | |
| print("\nποΈ Testing PostgreSQL Schema:") | |
| print("-" * 30) | |
| # Test model creation | |
| try: | |
| catalogue_ref = CatalogueRef( | |
| catalogue_id="test-id-123", | |
| catalogue_code="TEST-CODE-001", | |
| catalogue_type="Product", | |
| catalogue_name="Test Product", | |
| sku="TEST-SKU", | |
| status="Active" | |
| ) | |
| print(f"β CatalogueRef model created: {catalogue_ref}") | |
| print(f" Table name: {catalogue_ref.__tablename__}") | |
| print(f" Primary key: {catalogue_ref.catalogue_id}") | |
| # Test field access | |
| print(f" Fields accessible:") | |
| print(f" - catalogue_code: {catalogue_ref.catalogue_code}") | |
| print(f" - catalogue_type: {catalogue_ref.catalogue_type}") | |
| print(f" - status: {catalogue_ref.status}") | |
| return True | |
| except Exception as e: | |
| print(f"β Schema test failed: {e}") | |
| return False | |
| if __name__ == "__main__": | |
| print("π Starting Catalogue Sync Tests\n") | |
| # Run static tests | |
| success1 = test_sync_data_extraction() | |
| success2 = test_postgresql_schema() | |
| # Run async tests | |
| success3 = asyncio.run(test_catalogue_sync()) | |
| overall_success = success1 and success2 and success3 | |
| print(f"\n{'='*60}") | |
| print(f"π Test Results:") | |
| print(f" Sync Data Extraction: {'β PASS' if success1 else 'β FAIL'}") | |
| print(f" PostgreSQL Schema: {'β PASS' if success2 else 'β FAIL'}") | |
| print(f" Database Sync: {'β PASS' if success3 else 'β FAIL'}") | |
| print(f" Overall: {'β PASS' if overall_success else 'β FAIL'}") | |
| sys.exit(0 if overall_success else 1) |