use super::*;

#[test]
fn normalizes_keys_to_ascii_lowercase() {
    assert_eq!(normalized_metadata_option_key("Data_Path"), "data_path");
}

#[cfg(any(feature = "sqlite", feature = "duckdb"))]
#[test]
fn renders_numbered_or_is_null_sql() {
    let sql = render_set_metadata_option(
        "ducklake_metadata",
        MetadataPlaceholderStyle::Numbered,
        NullSafeEquality::OrIsNull,
    );
    assert_eq!(
        sql.delete,
        "DELETE FROM ducklake_metadata \
         WHERE lower(key) = lower(?1) \
           AND ((scope IS NULL AND ?2 IS NULL) OR scope = ?2) \
           AND ((scope_id IS NULL AND ?3 IS NULL) OR scope_id = ?3)"
    );
    assert_eq!(
        sql.insert,
        "INSERT INTO ducklake_metadata(key, value, scope, scope_id) \
         VALUES (?1, ?2, ?3, ?4)"
    );
}

#[cfg(feature = "postgres")]
#[test]
fn renders_dollar_is_not_distinct_from_sql() {
    let sql = render_set_metadata_option(
        "\"meta\".ducklake_metadata",
        MetadataPlaceholderStyle::Dollar,
        NullSafeEquality::IsNotDistinctFrom,
    );
    assert_eq!(
        sql.delete,
        "DELETE FROM \"meta\".ducklake_metadata \
         WHERE lower(key) = lower($1) \
           AND scope IS NOT DISTINCT FROM $2 \
           AND scope_id IS NOT DISTINCT FROM $3"
    );
    assert_eq!(
        sql.insert,
        "INSERT INTO \"meta\".ducklake_metadata(key, value, scope, scope_id) \
         VALUES ($1, $2, $3, $4)"
    );
}

#[cfg(feature = "quack")]
#[test]
fn renders_literal_sql_with_escaping() {
    let sql = render_set_metadata_option_literals(
        "My'Key",
        "va'lue",
        &MetadataOptionScope {
            scope: Some("table".to_string()),
            scope_id: Some(42),
        },
    );
    assert_eq!(
        sql.delete,
        "DELETE FROM ducklake_metadata \
         WHERE lower(key) = lower('My''Key') \
           AND scope IS NOT DISTINCT FROM 'table' \
           AND scope_id IS NOT DISTINCT FROM 42"
    );
    assert_eq!(
        sql.insert,
        "INSERT INTO ducklake_metadata(key, value, scope, scope_id) \
         VALUES ('my''key', 'va''lue', 'table', 42)"
    );
}

#[cfg(feature = "quack")]
#[test]
fn renders_null_literals_for_global_scope() {
    let sql =
        render_set_metadata_option_literals("data_path", "s3://x", &MetadataOptionScope::global());
    assert_eq!(
        sql.delete,
        "DELETE FROM ducklake_metadata \
         WHERE lower(key) = lower('data_path') \
           AND scope IS NOT DISTINCT FROM NULL \
           AND scope_id IS NOT DISTINCT FROM NULL"
    );
    assert_eq!(
        sql.insert,
        "INSERT INTO ducklake_metadata(key, value, scope, scope_id) \
         VALUES ('data_path', 's3://x', NULL, NULL)"
    );
}
