#[cfg(feature = "env")]
mod tests {
use std::{collections::HashSet, ops::Deref, sync::LazyLock};
use adbc_core::{
error::{Error, Result},
options::AdbcVersion,
Connection as _, Statement as _,
};
use adbc_snowflake::{connection, database, driver, Connection, Database, Driver, Statement};
use arrow_array::{cast::AsArray, types::Decimal128Type};
const ADBC_VERSION: AdbcVersion = AdbcVersion::V110;
static DRIVER: LazyLock<Result<Driver>> = LazyLock::new(|| {
driver::Builder::from_env()?
.with_adbc_version(ADBC_VERSION)
.try_load()
});
static DATABASE: LazyLock<Result<Database>> =
LazyLock::new(|| database::Builder::from_env()?.build(&mut DRIVER.deref().clone()?));
static CONNECTION: LazyLock<Result<Connection>> =
LazyLock::new(|| connection::Builder::from_env()?.build(&DATABASE.deref().clone()?));
fn with_database(func: impl FnOnce(Database) -> Result<()>) -> Result<()> {
DATABASE.deref().clone().and_then(func)
}
fn with_connection(func: impl FnOnce(Connection) -> Result<()>) -> Result<()> {
CONNECTION.deref().clone().and_then(func)
}
fn with_empty_statement(func: impl FnOnce(Statement) -> Result<()>) -> Result<()> {
with_connection(|mut connection| connection.new_statement().and_then(func))
}
#[test_with::env(ADBC_SNOWFLAKE_TESTS)]
fn database_get_info() -> Result<()> {
with_database(|mut database| {
assert_eq!(database.vendor_name(), Ok("Snowflake".to_owned()));
assert!(database
.vendor_version()
.is_ok_and(|version| version.starts_with("v")));
assert!(database.vendor_arrow_version().is_ok());
assert_eq!(database.vendor_sql(), Ok(true));
assert_eq!(database.vendor_substrait(), Ok(false));
assert_eq!(
database.driver_name(),
Ok("ADBC Snowflake Driver - Go".to_owned())
);
assert!(database.driver_version().is_ok());
assert!(database
.driver_arrow_version()
.is_ok_and(|version| version.starts_with("v")));
assert_eq!(database.adbc_version(), Ok(ADBC_VERSION));
Ok(())
})
}
#[test_with::env(ADBC_SNOWFLAKE_TESTS)]
fn statement_execute() -> Result<()> {
with_empty_statement(|mut statement| {
statement.set_sql_query("SELECT 21 + 21")?;
let batch = statement
.execute()?
.next()
.expect("a record batch")
.map_err(Error::from)?;
assert_eq!(
batch.column(0).as_primitive::<Decimal128Type>().value(0),
42
);
Ok(())
})
}
#[test_with::env(ADBC_SNOWFLAKE_TESTS)]
fn statement_execute_schema() -> Result<()> {
with_empty_statement(|mut statement| {
statement.set_sql_query("SHOW WAREHOUSES")?;
let schema = statement.execute_schema()?;
let field_names = schema
.fields()
.into_iter()
.map(|field| field.name().as_ref())
.collect::<HashSet<_>>();
let expected_field_names = [
"name",
"state",
"type",
"size",
"running",
"queued",
"is_default",
"is_current",
"auto_suspend",
"auto_resume",
"available",
"provisioning",
"quiescing",
"other",
"created_on",
"resumed_on",
"updated_on",
"owner",
"comment",
"resource_monitor",
"actives",
"pendings",
"failed",
"suspended",
"uuid",
"budget",
"owner_role_type",
]
.into_iter()
.collect::<HashSet<_>>();
assert_eq!(
expected_field_names
.difference(&field_names)
.collect::<Vec<_>>(),
Vec::<&&str>::default()
);
Ok(())
})
}
}