//! Input validation tests

use gdelt::db::duckdb::validation;

#[test]
fn test_validate_date_valid_dashed() {
    let result = validation::validate_date("2024-01-15");
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 20240115);
}

#[test]
fn test_validate_date_valid_plain() {
    let result = validation::validate_date("20240115");
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), 20240115);
}

#[test]
fn test_validate_date_invalid_short() {
    let result = validation::validate_date("2024");
    assert!(result.is_err());
}

#[test]
fn test_validate_date_invalid_chars() {
    let result = validation::validate_date("2024-XX-YY");
    assert!(result.is_err());
}

#[test]
fn test_validate_country_code_valid_2() {
    let result = validation::validate_country_code("US");
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "US");
}

#[test]
fn test_validate_country_code_valid_3() {
    let result = validation::validate_country_code("usa");
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "USA");
}

#[test]
fn test_validate_country_code_invalid_short() {
    let result = validation::validate_country_code("U");
    assert!(result.is_err());
}

#[test]
fn test_validate_country_code_invalid_long() {
    let result = validation::validate_country_code("USAA");
    assert!(result.is_err());
}

#[test]
fn test_validate_country_code_invalid_nonalpha() {
    let result = validation::validate_country_code("U1");
    assert!(result.is_err());
}

#[test]
fn test_validate_actor_code_valid() {
    let result = validation::validate_actor_code("GOV");
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), "GOV");
}

#[test]
fn test_validate_actor_code_sql_injection() {
    let result = validation::validate_actor_code("'; DROP TABLE events; --");
    assert!(result.is_err());
}

#[test]
fn test_validate_actor_code_too_long() {
    let code = "A".repeat(100);
    let result = validation::validate_actor_code(&code);
    assert!(result.is_err());
}

#[test]
fn test_validate_theme_valid() {
    let result = validation::validate_theme("TAX_FNCACT");
    assert!(result.is_ok());
}

#[test]
fn test_validate_theme_sql_injection() {
    let result = validation::validate_theme("'; DROP TABLE gkg; --");
    assert!(result.is_err());
}

#[test]
fn test_validate_pattern_valid() {
    let result = validation::validate_pattern("test");
    assert!(result.is_ok());
}

#[test]
fn test_validate_pattern_valid_with_spaces() {
    let result = validation::validate_pattern("John Smith");
    assert!(result.is_ok());
}

#[test]
fn test_validate_pattern_sql_injection() {
    let result = validation::validate_pattern("test'; DROP TABLE --");
    assert!(result.is_err());
}

#[test]
fn test_validate_pattern_too_long() {
    let pattern = "a".repeat(300);
    let result = validation::validate_pattern(&pattern);
    assert!(result.is_err());
}

#[test]
fn test_validate_entity_name_valid() {
    let result = validation::validate_entity_name("John Smith");
    assert!(result.is_ok());
}

#[test]
fn test_validate_entity_name_with_apostrophe() {
    let result = validation::validate_entity_name("O'Connor");
    assert!(result.is_ok());
}

#[test]
fn test_validate_entity_name_sql_injection() {
    let result = validation::validate_entity_name("test'; DROP TABLE gkg; --");
    assert!(result.is_err());
}

#[test]
fn test_sql_injection_prevention_country() {
    let malicious = "US'; DELETE FROM events; --";
    assert!(validation::validate_country_code(malicious).is_err());
}

#[test]
fn test_sql_injection_prevention_actor() {
    let malicious = "USA OR 1=1 --";
    assert!(validation::validate_actor_code(malicious).is_err());
}

#[test]
fn test_sql_injection_prevention_theme() {
    let malicious = "ECONOMY'; UPDATE gkg SET --";
    assert!(validation::validate_theme(malicious).is_err());
}

#[test]
fn test_sql_injection_prevention_pattern() {
    let malicious = "test%'; TRUNCATE TABLE events; --";
    assert!(validation::validate_pattern(malicious).is_err());
}
