from __future__ import annotations

import sys
from pathlib import Path

ROOT = Path(__file__).resolve().parents[2]
if str(ROOT) not in sys.path:
    sys.path.insert(0, str(ROOT))

from src.quant_engine.storage_backend_v1 import StoreSpec, default_sqlite_store_path, normalize_store_spec


def test_default_sqlite_store_path_uses_named_subdir(tmp_path):
    path = default_sqlite_store_path(tmp_path, "qualitative_sell_strategy/qualitative_sell_strategy.db")
    assert str(path).endswith("qualitative_sell_strategy.db")


def test_normalize_store_spec_supports_sqlite_and_postgresql(tmp_path):
    backend_sqlite, sqlite_location = normalize_store_spec(StoreSpec(location=tmp_path / "collector.db"), ROOT)
    assert backend_sqlite == "sqlite"
    assert str(sqlite_location).endswith("collector.db")

    backend_pg, pg_location = normalize_store_spec(
        StoreSpec(backend="postgresql", location="postgresql://user:pass@localhost/db"),
        ROOT,
    )
    assert backend_pg == "postgresql"
    assert "postgresql://" in str(pg_location)


def test_postgresql_upgrade_stub_script_exists():
    assert (ROOT / "tools" / "generate_postgresql_upgrade_stub_v1.py").exists()
