"""Offline path configuration checks; no external services.""" import os import tempfile import unittest from pathlib import Path from unittest.mock import patch from step2_graph_building.config import get_metadata_template_dir, get_relation_path class TemplatePathTests(unittest.TestCase): def test_relative_paths_use_project_root_from_another_working_directory(self): project = Path(__file__).resolve().parents[1] previous = Path.cwd() with tempfile.TemporaryDirectory() as elsewhere, patch.dict(os.environ, { 'METADATA_TEMPLATE_DIR': 'data/templates/metadata', 'RELATION_DIR': 'data/templates', }): try: os.chdir(elsewhere) self.assertEqual(get_metadata_template_dir(), project / 'data/templates/metadata') self.assertEqual(get_relation_path(), project / 'data/templates/relation.xlsx') finally: os.chdir(previous) def test_custom_env_external_directories_and_process_override(self): with tempfile.TemporaryDirectory() as tmp, patch.dict(os.environ, {}, clear=True): root = Path(tmp) metadata = root / '外部 metadata' relations = root / '外部 relations' metadata.mkdir() relations.mkdir() (relations / 'relation.xlsx').touch() config = root / 'custom.env' config.write_text(f'METADATA_TEMPLATE_DIR="{metadata.as_posix()}"\nRELATION_DIR="{relations.as_posix()}"\n', encoding='utf-8') self.assertEqual(get_metadata_template_dir(config), metadata) self.assertEqual(get_relation_path(config), relations / 'relation.xlsx') override = root / 'override' override.mkdir() os.environ['METADATA_TEMPLATE_DIR'] = str(override) self.assertEqual(get_metadata_template_dir(config), override) def test_missing_blank_directory_and_missing_fixed_filename_fail(self): with tempfile.TemporaryDirectory() as tmp, patch('step2_graph_building.config._load'), patch.dict(os.environ, {}, clear=True): root = Path(tmp) for bad in (None, '', str(root / 'missing')): if bad is None: os.environ.pop('METADATA_TEMPLATE_DIR', None) else: os.environ['METADATA_TEMPLATE_DIR'] = bad with self.assertRaises((RuntimeError, ValueError)): get_metadata_template_dir() file = root / 'not-a-directory' file.touch() os.environ['METADATA_TEMPLATE_DIR'] = str(file) with self.assertRaises(ValueError): get_metadata_template_dir() os.environ['RELATION_DIR'] = str(root) (root / 'renamed.xlsx').touch() with self.assertRaisesRegex(ValueError, 'relation.xlsx'): get_relation_path() if __name__ == '__main__': unittest.main()