sal-postgresclient 0.1.0

SAL PostgreSQL Client - PostgreSQL client wrapper with connection management and Rhai integration
Documentation
use rhai::{Engine, EvalAltResult};
use sal_postgresclient::rhai::*;

#[test]
fn test_rhai_function_registration() {
    let mut engine = Engine::new();

    // Register PostgreSQL functions
    let result = register_postgresclient_module(&mut engine);
    assert!(result.is_ok());

    // Test that functions are registered by trying to call them
    // We expect these to fail with PostgreSQL errors since no server is running,
    // but they should be callable (not undefined function errors)

    let test_script = r#"
        // Test function availability by calling them
        try { pg_connect(); } catch(e) { }
        try { pg_ping(); } catch(e) { }
        try { pg_reset(); } catch(e) { }
        try { pg_execute("SELECT 1"); } catch(e) { }
        try { pg_query("SELECT 1"); } catch(e) { }
        try { pg_query_one("SELECT 1"); } catch(e) { }
        try { pg_install("test", "15", 5432, "user", "pass"); } catch(e) { }
        try { pg_create_database("test", "db"); } catch(e) { }
        try { pg_execute_sql("test", "db", "SELECT 1"); } catch(e) { }
        try { pg_is_running("test"); } catch(e) { }

        true
    "#;

    let result: Result<bool, Box<EvalAltResult>> = engine.eval(test_script);
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), true);
}

#[test]
fn test_pg_connect_without_server() {
    // Test pg_connect when no PostgreSQL server is available
    // This should return an error since no server is running
    let result = pg_connect();

    // We expect this to fail since no PostgreSQL server is configured
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_ping_without_server() {
    // Test pg_ping when no PostgreSQL server is available
    let result = pg_ping();

    // We expect this to fail since no server is running
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_reset_without_server() {
    // Test pg_reset when no PostgreSQL server is available
    let result = pg_reset();

    // This might succeed or fail depending on the implementation
    // We just check that it doesn't panic
    match result {
        Ok(_) => {
            // Reset succeeded
        }
        Err(err) => {
            // Reset failed, which is expected without a server
            let error_msg = format!("{}", err);
            assert!(error_msg.contains("PostgreSQL error"));
        }
    }
}

#[test]
fn test_pg_execute_without_server() {
    // Test pg_execute when no PostgreSQL server is available
    let result = pg_execute("SELECT 1");

    // We expect this to fail since no server is running
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_query_without_server() {
    // Test pg_query when no PostgreSQL server is available
    let result = pg_query("SELECT 1");

    // We expect this to fail since no server is running
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_query_one_without_server() {
    // Test pg_query_one when no PostgreSQL server is available
    let result = pg_query_one("SELECT 1");

    // We expect this to fail since no server is running
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_install_without_nerdctl() {
    // Test pg_install when nerdctl is not available
    let result = pg_install("test-postgres", "15", 5433, "testuser", "testpass");

    // We expect this to fail since nerdctl is likely not available
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL installer error"));
    }
}

#[test]
fn test_pg_create_database_without_container() {
    // Test pg_create_database when container is not running
    let result = pg_create_database("nonexistent-container", "testdb");

    // We expect this to fail since the container doesn't exist
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_execute_sql_without_container() {
    // Test pg_execute_sql when container is not running
    let result = pg_execute_sql("nonexistent-container", "testdb", "SELECT 1");

    // We expect this to fail since the container doesn't exist
    assert!(result.is_err());

    if let Err(err) = result {
        let error_msg = format!("{}", err);
        assert!(error_msg.contains("PostgreSQL error"));
    }
}

#[test]
fn test_pg_is_running_without_container() {
    // Test pg_is_running when container is not running
    let result = pg_is_running("nonexistent-container");

    // This should return false since the container doesn't exist
    assert!(result.is_ok());
    assert_eq!(result.unwrap(), false);
}

#[test]
fn test_rhai_script_execution() {
    let mut engine = Engine::new();

    // Register PostgreSQL functions
    register_postgresclient_module(&mut engine).unwrap();

    // Test a simple script that calls PostgreSQL functions
    let script = r#"
        // Test function availability by trying to call them
        let results = #{};

        try {
            pg_connect();
            results.connect = true;
        } catch(e) {
            results.connect = true; // Function exists, just failed to connect
        }

        try {
            pg_ping();
            results.ping = true;
        } catch(e) {
            results.ping = true; // Function exists, just failed to ping
        }

        try {
            pg_reset();
            results.reset = true;
        } catch(e) {
            results.reset = true; // Function exists, just failed to reset
        }

        try {
            pg_execute("SELECT 1");
            results.execute = true;
        } catch(e) {
            results.execute = true; // Function exists, just failed to execute
        }

        try {
            pg_query("SELECT 1");
            results.query = true;
        } catch(e) {
            results.query = true; // Function exists, just failed to query
        }

        try {
            pg_query_one("SELECT 1");
            results.query_one = true;
        } catch(e) {
            results.query_one = true; // Function exists, just failed to query
        }

        try {
            pg_install("test", "15", 5432, "user", "pass");
            results.install = true;
        } catch(e) {
            results.install = true; // Function exists, just failed to install
        }

        try {
            pg_create_database("test", "db");
            results.create_db = true;
        } catch(e) {
            results.create_db = true; // Function exists, just failed to create
        }

        try {
            pg_execute_sql("test", "db", "SELECT 1");
            results.execute_sql = true;
        } catch(e) {
            results.execute_sql = true; // Function exists, just failed to execute
        }

        try {
            pg_is_running("test");
            results.is_running = true;
        } catch(e) {
            results.is_running = true; // Function exists, just failed to check
        }

        results;
    "#;

    let result: Result<rhai::Map, Box<EvalAltResult>> = engine.eval(script);
    if let Err(ref e) = result {
        println!("Script execution error: {}", e);
    }
    assert!(result.is_ok());

    let map = result.unwrap();
    assert_eq!(map.get("connect").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("ping").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("reset").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("execute").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("query").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("query_one").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("install").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("create_db").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("execute_sql").unwrap().as_bool().unwrap(), true);
    assert_eq!(map.get("is_running").unwrap().as_bool().unwrap(), true);
}