Files
thechart/tests/test_export_cleanup.py

68 lines
1.8 KiB
Python

"""
Tests for export cleanup tracking in ExportManager.
"""
import os
import sys
import tempfile
from pathlib import Path
# Ensure src imports like other tests do
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
from export_manager import ExportManager
from data_manager import DataManager
from medicine_manager import MedicineManager
from pathology_manager import PathologyManager
from init import logger
def test_export_cleanup_on_del():
# Setup a temporary workspace and CSV
tmpdir = tempfile.mkdtemp()
csv_path = os.path.join(tmpdir, "data.csv")
# Minimal managers
med = MedicineManager(logger=logger)
path = PathologyManager(logger=logger)
data = DataManager(csv_path, logger, med, path)
# Create a couple of rows so export works
data.add_entry([
"2024-01-01", 1, 1, 1, 1, 1, "", 0, "", 0, "", 0, "", 0, "", "note"
])
em = ExportManager(data, graph_manager=None, medicine_manager=med, pathology_manager=path, logger=logger)
json_path = os.path.join(tmpdir, "out.json")
xml_path = os.path.join(tmpdir, "out.xml")
assert em.export_data_to_json(json_path) is True
assert em.export_data_to_xml(xml_path) is True
# Files should exist now
assert os.path.exists(json_path)
assert os.path.exists(xml_path)
# Deleting the export manager should best-effort remove its tracked files
del em
# Force garbage collection to trigger __del__ in CPython test environment
import gc
gc.collect()
assert not os.path.exists(json_path)
assert not os.path.exists(xml_path)
# Cleanup temp dir
try:
os.unlink(csv_path)
except Exception:
pass
try:
os.rmdir(tmpdir)
except Exception:
# If test fails earlier, ignore
pass