"""Unit tests for sql_query_service."""

import pytest

from app.modules.dev_tools.services.sql_query_service import (
    QueryValidationError,
    validate_query,
)


@pytest.mark.unit
@pytest.mark.dev
class TestValidateQuery:
    """Tests for the validate_query function."""

    def test_select_allowed(self):
        validate_query("SELECT * FROM users")

    def test_empty_query_rejected(self):
        with pytest.raises(QueryValidationError, match="empty"):
            validate_query("")

    def test_insert_rejected(self):
        with pytest.raises(QueryValidationError, match="INSERT"):
            validate_query("INSERT INTO users (email) VALUES ('a@b.com')")

    def test_update_rejected(self):
        with pytest.raises(QueryValidationError, match="UPDATE"):
            validate_query("UPDATE users SET email = 'x' WHERE id = 1")

    def test_delete_rejected(self):
        with pytest.raises(QueryValidationError, match="DELETE"):
            validate_query("DELETE FROM users WHERE id = 1")

    def test_drop_rejected(self):
        with pytest.raises(QueryValidationError, match="DROP"):
            validate_query("DROP TABLE users")

    def test_alter_rejected(self):
        with pytest.raises(QueryValidationError, match="ALTER"):
            validate_query("ALTER TABLE users ADD COLUMN foo TEXT")

    def test_truncate_rejected(self):
        with pytest.raises(QueryValidationError, match="TRUNCATE"):
            validate_query("TRUNCATE users")

    def test_comment_hiding_rejected(self):
        """Forbidden keywords hidden inside comments are stripped first."""
        validate_query("SELECT 1 -- DROP TABLE users")

    def test_block_comment_hiding_rejected(self):
        validate_query("SELECT /* DELETE FROM users */ 1")

    def test_select_with_where(self):
        validate_query("SELECT id, email FROM users WHERE is_active = true")
