"""目録への接続(SPEC_02 冒頭: 外部キー制約を有効にする)."""
from pathlib import Path
import pytest
from tiasdata import catalog
def test_connect_enables_foreign_keys(tmp_path: Path) -> None:
conn = catalog.connect(tmp_path / "db.sqlite", create=True)
try:
assert conn.execute("PRAGMA foreign_keys").fetchone()[0] == 1
finally:
conn.close()
def test_connect_enables_foreign_keys_on_every_connection(tmp_path: Path) -> None:
path = tmp_path / "db.sqlite"
catalog.connect(path, create=True).close()
conn = catalog.connect(path)
try:
assert conn.execute("PRAGMA foreign_keys").fetchone()[0] == 1
finally:
conn.close()
def test_connect_with_create_makes_db_file(tmp_path: Path) -> None:
path = tmp_path / "db.sqlite"
catalog.connect(path, create=True).close()
assert path.is_file()
def test_connect_without_create_refuses_missing_db(tmp_path: Path) -> None:
path = tmp_path / "missing.sqlite"
with pytest.raises(ValueError):
catalog.connect(path)
# 空の目録を誤って作らない
assert not path.exists()
def test_connect_missing_db_message_does_not_contain_path(tmp_path: Path) -> None:
path = tmp_path / "missing.sqlite"
with pytest.raises(ValueError) as exc:
catalog.connect(path)
assert str(tmp_path) not in str(exc.value)
assert tmp_path.as_posix() not in str(exc.value)