#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Ejecutar tests de cancelación en cascada
"""

import odoo
import sys

def run_tests(cr):
    """Ejecutar los tests"""
    env = odoo.api.Environment(cr, odoo.SUPERUSER_ID, {})
    
    # Importar y ejecutar tests
    from odoo.addons.lims_management.tests.test_order_cancel_cascade import TestOrderCancelCascade
    
    suite = odoo.tests.loader.make_suite(TestOrderCancelCascade)
    result = odoo.tests.runner.run(suite)
    
    print(f"\n📊 Resultados de los tests:")
    print(f"   Tests ejecutados: {result.testsRun}")
    print(f"   Errores: {len(result.errors)}")
    print(f"   Fallos: {len(result.failures)}")
    
    if result.errors:
        print("\n❌ Errores:")
        for test, error in result.errors:
            print(f"   - {test}: {error}")
    
    if result.failures:
        print("\n❌ Fallos:")
        for test, failure in result.failures:
            print(f"   - {test}: {failure}")
    
    if result.wasSuccessful():
        print("\n✅ Todos los tests pasaron exitosamente!")
    else:
        print("\n❌ Algunos tests fallaron")
    
    return result.wasSuccessful()

if __name__ == '__main__':
    db_name = 'lims_demo'
    try:
        registry = odoo.modules.registry.Registry(db_name)
        with registry.cursor() as cr:
            success = run_tests(cr)
            sys.exit(0 if success else 1)
    except Exception as e:
        print(f"Error: {e}")
        import traceback
        traceback.print_exc()
        sys.exit(1)