LinkDesk/backend/test_shot_deletion_fix.py

76 lines
2.6 KiB
Python

#!/usr/bin/env python3
"""
Test script to verify that shot deletion properly sets deleted_by and deleted_at fields.
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from database import get_db
from models.shot import Shot
from models.user import User
from models.project import Project
from models.episode import Episode
from services.shot_soft_deletion import ShotSoftDeletionService
from sqlalchemy.orm import Session
def test_shot_deletion_fields():
"""Test that shot deletion properly sets deleted_by and deleted_at fields."""
# Get database session
db = next(get_db())
try:
# Find an existing shot that's not deleted
shot = db.query(Shot).filter(Shot.deleted_at.is_(None)).first()
if not shot:
print("No active shots found to test deletion")
return
# Find an admin user to perform the deletion
admin_user = db.query(User).filter(User.is_admin == True).first()
if not admin_user:
print("No admin user found to perform deletion")
return
print(f"Testing deletion of shot: {shot.name} (ID: {shot.id})")
print(f"Deleting as user: {admin_user.email} (ID: {admin_user.id})")
# Check initial state
print(f"Before deletion - deleted_at: {shot.deleted_at}, deleted_by: {shot.deleted_by}")
# Perform soft deletion
deletion_service = ShotSoftDeletionService()
result = deletion_service.soft_delete_shot_cascade(shot.id, db, admin_user)
if not result.success:
print(f"Deletion failed: {result.errors}")
return
# Commit the transaction (this was the missing piece!)
db.commit()
# Refresh the shot object to get updated values
db.refresh(shot)
# Check final state
print(f"After deletion - deleted_at: {shot.deleted_at}, deleted_by: {shot.deleted_by}")
print(f"Deletion result - success: {result.success}")
print(f"Deletion result - deleted_by: {result.deleted_by}")
print(f"Deletion result - deleted_at: {result.deleted_at}")
# Verify the fields are set
if shot.deleted_at is not None and shot.deleted_by is not None:
print("✅ SUCCESS: Both deleted_at and deleted_by fields are properly set!")
else:
print("❌ FAILURE: deleted_at or deleted_by fields are still None")
except Exception as e:
print(f"Error during test: {e}")
db.rollback()
finally:
db.close()
if __name__ == "__main__":
test_shot_deletion_fields()