From 0ed176427abaa9e17a8ee6c23cd15ab98913722b Mon Sep 17 00:00:00 2001 From: Will Date: Sun, 10 Aug 2025 10:57:56 -0700 Subject: [PATCH] test: Add tests for export cleanup tracking and module aliasing behavior --- tests/test_export_cleanup.py | 67 +++++++++++++++++++++++++++++++++++ tests/test_module_aliasing.py | 40 +++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tests/test_export_cleanup.py create mode 100644 tests/test_module_aliasing.py diff --git a/tests/test_export_cleanup.py b/tests/test_export_cleanup.py new file mode 100644 index 0000000..132c395 --- /dev/null +++ b/tests/test_export_cleanup.py @@ -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 diff --git a/tests/test_module_aliasing.py b/tests/test_module_aliasing.py new file mode 100644 index 0000000..d5fc18f --- /dev/null +++ b/tests/test_module_aliasing.py @@ -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