import pytest

from zbt.commands.inherit_stage_one import (
    EXCLUDED_TABLES,
    SPECIAL_RELATION_TABLES,
    build_upsert_sql,
    quote_identifier,
)


def test_stage_runtime_tables_are_not_inherited() -> None:
    assert {
        "alembic_version",
        "auth_sessions",
        "async_tasks",
        "outbox_events",
        "analytics_sync_offsets",
    } <= EXCLUDED_TABLES
    assert {
        "admin_user_roles",
        "role_permissions",
    } == SPECIAL_RELATION_TABLES


def test_build_upsert_sql_uses_common_columns_and_primary_key() -> None:
    statement = build_upsert_sql(
        "insurance_s1_core",
        "insurance_s2_core",
        "products",
        ["id", "product_code", "name"],
        ["id"],
    )

    assert "INSERT INTO `insurance_s2_core`.`products`" in statement
    assert "FROM `insurance_s1_core`.`products`" in statement
    assert "`product_code` = VALUES(`product_code`)" in statement
    assert "`id` = VALUES(`id`)" not in statement


def test_quote_identifier_rejects_unsafe_input() -> None:
    with pytest.raises(ValueError):
        quote_identifier("products; DROP DATABASE")
