"""Test fixtures for Piggie SDK tests."""

from __future__ import annotations

import os

import pytest

import piggie
from piggie.connection import drop_graph, ensure_graph

DSN = os.environ.get(
    "PIGGIE_TEST_DSN",
    "host=/var/run/postgresql port=5433 dbname=piggie user=gregf",
)
TEST_GRAPH = "piggie_test_graph"


@pytest.fixture(scope="session")
def db():
    """Session-scoped PiggieDriver connected to the test database."""
    driver = piggie.connect(DSN, graph=TEST_GRAPH)
    yield driver
    # Clean up test graph
    try:
        drop_graph(driver.connection, TEST_GRAPH)
    except Exception:
        pass
    driver.close()


@pytest.fixture
def fresh_graph(db):
    """Ensure a clean test graph for each test."""
    try:
        drop_graph(db.connection, TEST_GRAPH)
    except Exception:
        pass
    ensure_graph(db.connection, TEST_GRAPH)
    db.set_graph(TEST_GRAPH)
    return db
