import os

from db_core.clients.factory import create_postgres_client, get_postgres_client
from db_core.clients.postgresql import PostgresClient


def test_create_postgres_client_from_url():
    client = create_postgres_client(url="postgresql+asyncpg://user:pass@localhost:5432/testdb")
    assert isinstance(client, PostgresClient)
    assert client.engine is not None
    assert client.engine.url.database == "testdb"


def test_create_postgres_client_from_environment(monkeypatch):
    monkeypatch.setenv("PG_USER", "envuser")
    monkeypatch.setenv("PG_PASSWORD", "envpass")
    monkeypatch.setenv("PG_HOST", "localhost")
    monkeypatch.setenv("PG_PORT", "5432")
    monkeypatch.setenv("PG_DATABASE", "envdb")

    client = create_postgres_client(env_prefix="PG")
    assert isinstance(client, PostgresClient)
    assert client.engine.url.username == "envuser"
    assert client.engine.url.database == "envdb"


def test_get_postgres_client_alias():
    client = get_postgres_client(url="postgresql+asyncpg://user:pass@localhost:5432/testdb")
    assert isinstance(client, PostgresClient)
    assert client.engine.url.database == "testdb"
