fix(database): honor package-scoped configuration (#211)
This commit is contained in:
@@ -87,7 +87,10 @@ def _resolve_db_path(db_path: Optional[str] = None) -> str:
|
||||
|
||||
if db_path is None:
|
||||
try:
|
||||
from py.config import PromptManagerConfig
|
||||
try:
|
||||
from ..py.config import PromptManagerConfig
|
||||
except ImportError:
|
||||
from py.config import PromptManagerConfig
|
||||
|
||||
db_path = PromptManagerConfig.DEFAULT_DB_PATH
|
||||
except Exception:
|
||||
|
||||
@@ -14,6 +14,7 @@ from unittest.mock import patch
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
import database.operations as operations
|
||||
from database.operations import _resolve_db_path
|
||||
|
||||
EXTENSION_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
@@ -22,6 +23,37 @@ EXTENSION_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
class TestResolveDbPath(unittest.TestCase):
|
||||
"""Test _resolve_db_path() database path resolution."""
|
||||
|
||||
def test_package_qualified_config_precedes_unrelated_standalone_config(
|
||||
self,
|
||||
) -> None:
|
||||
"""A custom-node package must use its own configured disposable database."""
|
||||
with tempfile.TemporaryDirectory() as directory:
|
||||
configured = os.path.join(directory, "owned", "prompts.db")
|
||||
package_config = types.ModuleType("owned_promptmanager.py.config")
|
||||
package_config.PromptManagerConfig = types.SimpleNamespace(
|
||||
DEFAULT_DB_PATH=configured
|
||||
)
|
||||
standalone_config = types.ModuleType("py.config")
|
||||
standalone_config.PromptManagerConfig = types.SimpleNamespace(
|
||||
DEFAULT_DB_PATH=os.path.join(directory, "unrelated.db")
|
||||
)
|
||||
with (
|
||||
patch.dict(
|
||||
operations.__dict__,
|
||||
{"__package__": "owned_promptmanager.database", "__spec__": None},
|
||||
),
|
||||
patch.dict(
|
||||
sys.modules,
|
||||
{
|
||||
"owned_promptmanager.py.config": package_config,
|
||||
"py.config": standalone_config,
|
||||
},
|
||||
),
|
||||
):
|
||||
resolved = _resolve_db_path(None)
|
||||
self.assertEqual(resolved, configured)
|
||||
self.assertTrue(os.path.isdir(os.path.dirname(configured)))
|
||||
|
||||
def test_explicit_absolute_path_used_as_is(self):
|
||||
"""An explicit absolute path should be returned unchanged."""
|
||||
with tempfile.TemporaryDirectory() as tmpdir:
|
||||
|
||||
@@ -5,6 +5,7 @@ imports) with ComfyUI's ``server`` and ``folder_paths`` modules stubbed.
|
||||
"""
|
||||
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import tempfile
|
||||
@@ -65,7 +66,13 @@ def load_package(name, db_path, output_dir, server=None):
|
||||
try:
|
||||
from py.config import PromptManagerConfig
|
||||
|
||||
with mock.patch.object(PromptManagerConfig, "DEFAULT_DB_PATH", db_path):
|
||||
config_path = os.path.join(output_dir, "promptmanager-test-config.json")
|
||||
with open(config_path, "w", encoding="utf-8") as config:
|
||||
json.dump({"database": {"default_path": db_path}}, config)
|
||||
with (
|
||||
mock.patch.object(PromptManagerConfig, "DEFAULT_DB_PATH", db_path),
|
||||
mock.patch.dict(os.environ, {"PROMPT_MANAGER_CONFIG_PATH": config_path}),
|
||||
):
|
||||
spec = importlib.util.spec_from_file_location(
|
||||
name,
|
||||
os.path.join(ROOT, "__init__.py"),
|
||||
|
||||
Reference in New Issue
Block a user