#!/usr/bin/env python3 """ Test script to verify the logging system is working correctly. """ from app import create_app, db from services import ( log_script_start, log_script_completion, log_batch_created, log_payment_plan_run, log_payment_intent_followup, log_activity ) from models import Logs import time def test_logging_functions(): """Test all logging functions.""" print("Testing logging system...") # Test basic log_activity function log_id = log_activity( user_id=1, # System user action="TEST_LOGGING", entity_type="Test", entity_id=999, details="Testing the logging system functionality" ) print(f"✓ Basic logging test - Log ID: {log_id}") # Test script start logging start_log_id = log_script_start("test_logging.py", "test", "sandbox") print(f"✓ Script start logging - Log ID: {start_log_id}") # Test batch creation logging batch_log_id = log_batch_created(123, "Direct Debit", 45) print(f"✓ Batch creation logging - Log ID: {batch_log_id}") # Test payment plan run logging payplan_log_id = log_payment_plan_run( active_plans=25, due_plans=8, processed_count=7, failed_count=1, total_amount=1247.50 ) print(f"✓ Payment plan run logging - Log ID: {payplan_log_id}") # Test payment intent follow-up logging intent_log_id = log_payment_intent_followup( pending_count=15, succeeded_count=12, failed_count=2, still_pending=1 ) print(f"✓ Payment intent follow-up logging - Log ID: {intent_log_id}") # Test script completion logging completion_log_id = log_script_completion( script_name="test_logging.py", mode="test", success_count=5, failed_count=1, total_amount=1247.50, batch_ids=[123, 124], duration_seconds=2.5, errors=["Test error message"] ) print(f"✓ Script completion logging - Log ID: {completion_log_id}") return True def verify_logs_in_database(): """Verify that logs were actually written to the database.""" print("\nVerifying logs in database...") # Query recent test logs recent_logs = db.session.query(Logs).filter( (Logs.Action.like('TEST_%')) | (Logs.Entity_Type == 'Test') ).order_by(Logs.Added.desc()).limit(10).all() print(f"Found {len(recent_logs)} test log entries:") for log in recent_logs: print(f" - ID: {log.id}, Action: {log.Action}, Entity: {log.Entity_Type}, Details: {log.Log_Entry[:50]}...") return len(recent_logs) > 0 if __name__ == "__main__": app = create_app() with app.app_context(): print("=" * 60) print("PLUTUS LOGGING SYSTEM TEST") print("=" * 60) start_time = time.time() # Test logging functions test_success = test_logging_functions() # Verify logs were written to database db_success = verify_logs_in_database() end_time = time.time() duration = end_time - start_time print(f"\nTest completed in {duration:.2f} seconds") if test_success and db_success: print("✅ All logging tests passed!") # Log this test completion to demonstrate the system log_activity( user_id=1, action="TEST_COMPLETED", entity_type="Test", details=f"Logging system test completed successfully in {duration:.2f}s" ) else: print("❌ Some logging tests failed!") print("=" * 60)