use hyperdb_api::{ArrowInserter, Catalog, SqlType, TableDefinition};
mod common;
use common::TestConnection;
#[test]
fn test_arrow_inserter_new() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_arrow")
.add_required_column("id", SqlType::int())
.add_nullable_column("value", SqlType::double());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let inserter = ArrowInserter::new(&test.connection, &table_def);
assert!(inserter.is_ok(), "ArrowInserter::new should succeed");
let inserter = inserter.unwrap();
assert!(!inserter.has_data());
assert_eq!(inserter.total_bytes(), 0);
assert_eq!(inserter.chunk_count(), 0);
inserter.cancel();
}
#[test]
fn test_arrow_inserter_empty_table_def() {
let test = TestConnection::new().expect("Failed to create test connection");
let empty_def = TableDefinition::new("empty");
let inserter = ArrowInserter::new(&test.connection, &empty_def);
assert!(
inserter.is_err(),
"ArrowInserter::new should fail with empty table definition"
);
}
#[test]
fn test_arrow_inserter_from_table() {
let test = TestConnection::new().expect("Failed to create test connection");
test.connection
.execute_command(
"CREATE TABLE existing_table (
id INTEGER NOT NULL,
name TEXT,
value DOUBLE PRECISION
)",
)
.expect("Failed to create table");
let inserter = ArrowInserter::from_table(&test.connection, "existing_table");
assert!(
inserter.is_ok(),
"ArrowInserter::from_table should succeed for existing table"
);
inserter.unwrap().cancel();
}
#[test]
fn test_arrow_inserter_from_nonexistent_table() {
let test = TestConnection::new().expect("Failed to create test connection");
let inserter = ArrowInserter::from_table(&test.connection, "nonexistent_table");
assert!(
inserter.is_err(),
"ArrowInserter::from_table should fail for non-existent table"
);
}
#[test]
fn test_arrow_inserter_insert_record_batches_without_schema_fails() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def =
TableDefinition::new("test_arrow_schema").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let mut inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
let fake_batch_data = vec![1, 2, 3, 4]; let result = inserter.insert_record_batches(&fake_batch_data);
assert!(
result.is_err(),
"insert_record_batches should fail without prior insert_data"
);
let err_msg = result.unwrap_err().to_string();
assert!(
err_msg.contains("No Arrow schema has been sent"),
"Error should mention schema not sent: {err_msg}"
);
inserter.cancel();
}
#[test]
fn test_arrow_inserter_empty_data_is_noop() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_empty").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let mut inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
let result = inserter.insert_data(&[]);
assert!(result.is_ok(), "insert_data with empty data should succeed");
assert!(!inserter.has_data());
assert_eq!(inserter.chunk_count(), 0);
inserter.cancel();
}
#[test]
fn test_arrow_inserter_execute_without_data() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_no_data").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
let rows = inserter.execute().expect("execute should succeed");
assert_eq!(rows, 0, "execute without data should return 0 rows");
let count = test
.execute_scalar_i64("SELECT COUNT(*) FROM test_no_data")
.expect("Failed to count");
assert_eq!(count, 0);
}
#[test]
fn test_arrow_inserter_status_methods() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_status").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
assert!(!inserter.has_data());
assert_eq!(inserter.total_bytes(), 0);
assert_eq!(inserter.chunk_count(), 0);
inserter.cancel();
}
#[test]
fn test_arrow_inserter_cancel_no_data() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_cancel").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
let inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
inserter.cancel();
let count = test
.execute_scalar_i64("SELECT COUNT(*) FROM test_cancel")
.expect("Connection should still work after cancel");
assert_eq!(count, 0);
}
#[test]
fn test_arrow_inserter_drop_without_execute() {
let test = TestConnection::new().expect("Failed to create test connection");
let table_def = TableDefinition::new("test_drop").add_required_column("id", SqlType::int());
Catalog::new(&test.connection)
.create_table(&table_def)
.expect("Failed to create table");
{
let _inserter =
ArrowInserter::new(&test.connection, &table_def).expect("Failed to create inserter");
}
let count = test
.execute_scalar_i64("SELECT COUNT(*) FROM test_drop")
.expect("Connection should still work after inserter drop");
assert_eq!(count, 0);
}