test: Add tests for export cleanup tracking and module aliasing behavior

This commit is contained in:
2025-08-10 10:57:56 -07:00
parent 7208a689bd
commit 0ed176427a
2 changed files with 107 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
"""
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

View File

@@ -0,0 +1,40 @@
"""
Tiny tests to verify module aliasing behavior between 'src.*' and top-level
modules for compatibility with test patching.
"""
import os
import sys
import importlib
# Ensure 'src' is importable like other tests do
sys.path.insert(0, os.path.join(os.path.dirname(__file__), "..", "src"))
def test_graph_manager_aliasing_shared_module_object():
import src.graph_manager as gm_src
gm_top = importlib.import_module("graph_manager")
# Both import paths should refer to the same module object
assert gm_src is gm_top
# Patching a symbol on one should reflect in the other
sentinel = object()
setattr(gm_top, "ALIAS_TEST_SENTINEL", sentinel)
assert getattr(gm_src, "ALIAS_TEST_SENTINEL") is sentinel
def test_logger_aliasing_shared_module_object():
import src.logger as logger_src
logger_top = importlib.import_module("logger")
# Both import paths should refer to the same module object
assert logger_src is logger_top
# Changing a config attribute should be visible via the other name
new_path = "/tmp/thechart-test-alias"
logger_top.LOG_PATH = new_path
assert logger_src.LOG_PATH == new_path