#!/usr/bin/env python3 """ Test script to verify edit window functionality (save and delete) after dose tracking implementation. """ import logging import os import sys # Add the src directory to the path so we can import our modules sys.path.insert(0, os.path.join(os.path.dirname(__file__), "src")) from data_manager import DataManager def test_edit_window_functionality(): """Test both save and delete functionality with the new CSV format.""" print("Testing edit window functionality...") # Create a backup of the current CSV import shutil try: shutil.copy("thechart_data.csv", "thechart_data_backup.csv") print("✓ Created backup of current CSV") except Exception as e: print(f"✗ Failed to create backup: {e}") return False try: # Create a logger for the DataManager logger = logging.getLogger("test_logger") logger.setLevel(logging.DEBUG) # Initialize data manager data_manager = DataManager("thechart_data.csv", logger) # Load current data df = data_manager.load_data() print(f"✓ Loaded {len(df)} entries from CSV") if df.empty: print("✗ No data to test edit functionality") return False # Test 1: Test delete functionality print("\n=== Testing Delete Functionality ===") last_entry_date = df.iloc[-1]["date"] print(f"Attempting to delete entry with date: {last_entry_date}") success = data_manager.delete_entry(last_entry_date) if success: print("✓ Delete operation successful") df_after_delete = data_manager.load_data() if last_entry_date not in df_after_delete["date"].values: print("✓ Entry successfully removed from CSV") else: print("✗ Entry still exists after delete") return False else: print("✗ Delete operation failed") return False # Test 2: Test update functionality print("\n=== Testing Update Functionality ===") if not df_after_delete.empty: # Get first entry to test update first_entry = df_after_delete.iloc[0] test_date = first_entry["date"] original_note = first_entry["note"] print(f"Testing update for date: {test_date}") print(f"Original note: '{original_note}'") # Create updated data (simulating what the edit window would do) updated_data = [ test_date, # date int(first_entry["depression"]), # depression int(first_entry["anxiety"]), # anxiety int(first_entry["sleep"]), # sleep int(first_entry["appetite"]), # appetite int(first_entry["bupropion"]), # bupropion str(first_entry["bupropion_doses"]), # bupropion_doses int(first_entry["hydroxyzine"]), # hydroxyzine str(first_entry["hydroxyzine_doses"]), # hydroxyzine_doses int(first_entry["gabapentin"]), # gabapentin str(first_entry["gabapentin_doses"]), # gabapentin_doses int(first_entry["propranolol"]), # propranolol str(first_entry["propranolol_doses"]), # propranolol_doses f"{original_note} [UPDATED BY TEST]", # note ] print(f"Data to update with: {updated_data}") print(f"Length of update data: {len(updated_data)}") success = data_manager.update_entry(test_date, updated_data) if success: print("✓ Update operation successful") # Verify the update df_after_update = data_manager.load_data() updated_entry = df_after_update[ df_after_update["date"] == test_date ].iloc[0] if "[UPDATED BY TEST]" in updated_entry["note"]: print("✓ Entry successfully updated in CSV") print(f"New note: '{updated_entry['note']}'") else: print("✗ Entry was not properly updated") return False else: print("✗ Update operation failed") return False print("\n✓ All edit window functionality tests passed!") return True except Exception as e: print(f"✗ Error during test: {e}") import traceback traceback.print_exc() return False finally: # Restore the backup try: shutil.move("thechart_data_backup.csv", "thechart_data.csv") print("✓ Restored original CSV from backup") except Exception as e: print(f"✗ Failed to restore backup: {e}") if __name__ == "__main__": test_edit_window_functionality()