test: add tests to sqlite_map_row_to_attribute function

This commit is contained in:
2025-10-16 15:54:10 +02:00
parent 3095a78c89
commit f9d2f1c4c0

View File

@@ -22,40 +22,74 @@ pub fn run(row: &rusqlite::Row) -> rusqlite::Result<Attribute> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use rusqlite::{Connection, Result};
fn get_row_from_query(conn: &Connection, query: &str) -> Result<Attribute, rusqlite::Error> {
let mut stmt = conn.prepare(query).unwrap();
stmt.query_row([], run)
}
#[test] #[test]
fn map_row_to_attribute_with_signed_int_value() { fn map_row_to_attribute_with_signed_int_value() {
// Verifies that a database row with a signed integer value is correctly mapped to an Attribute with a Value::SignedInt. // Verifies that a database row with a signed integer value is correctly mapped to an Attribute with a Value::SignedInt.
todo!(); let conn = Connection::open_in_memory().unwrap();
let attr =
get_row_from_query(&conn, "SELECT 'a1', 'e1', -42, NULL, NULL, NULL, NULL").unwrap();
assert_eq!(attr.id, "a1");
assert_eq!(attr.value, Value::SignedInt(-42));
} }
#[test] #[test]
fn map_row_to_attribute_with_unsigned_int_value() { fn map_row_to_attribute_with_unsigned_int_value() {
// Verifies that a database row with an unsigned integer value is correctly mapped to an Attribute with a Value::UnsignedInt. // Verifies that a database row with an unsigned integer value is correctly mapped to an Attribute with a Value::UnsignedInt.
todo!(); let conn = Connection::open_in_memory().unwrap();
let attr =
get_row_from_query(&conn, "SELECT 'a2', 'e1', NULL, 42, NULL, NULL, NULL").unwrap();
assert_eq!(attr.id, "a2");
assert_eq!(attr.value, Value::UnsignedInt(42));
} }
#[test] #[test]
fn map_row_to_attribute_with_real_value() { fn map_row_to_attribute_with_real_value() {
// Verifies that a database row with a real (float) value is correctly mapped to an Attribute with a Value::Real. // Verifies that a database row with a real (float) value is correctly mapped to an Attribute with a Value::Real.
todo!(); let conn = Connection::open_in_memory().unwrap();
let attr = get_row_from_query(&conn, "SELECT 'a3', 'e1', NULL, NULL, 12.2345, NULL, NULL")
.unwrap();
assert_eq!(attr.id, "a3");
assert_eq!(attr.value, Value::Real(12.2345));
} }
#[test] #[test]
fn map_row_to_attribute_with_text_value() { fn map_row_to_attribute_with_text_value() {
// Verifies that a database row with a text value is correctly mapped to an Attribute with a Value::Text. // Verifies that a database row with a text value is correctly mapped to an Attribute with a Value::Text.
todo!(); let conn = Connection::open_in_memory().unwrap();
let attr = get_row_from_query(&conn, "SELECT 'a4', 'e1', NULL, NULL, NULL, 'hello', NULL")
.unwrap();
assert_eq!(attr.id, "a4");
assert_eq!(attr.value, Value::Text("hello".to_string()));
} }
#[test] #[test]
fn map_row_to_attribute_with_bool_value() { fn map_row_to_attribute_with_bool_value() {
// Verifies that a database row with a boolean value is correctly mapped to an Attribute with a Value::Bool. // Verifies that a database row with a boolean value is correctly mapped to an Attribute with a Value::Bool.
todo!(); let conn = Connection::open_in_memory().unwrap();
let attr =
get_row_from_query(&conn, "SELECT 'a5', 'e1', NULL, NULL, NULL, NULL, 1").unwrap();
assert_eq!(attr.id, "a5");
assert_eq!(attr.value, Value::Bool(true));
} }
#[test] #[test]
#[should_panic] #[should_panic]
fn map_row_to_attribute_should_panic_on_multiple_values() { fn map_row_to_attribute_should_panic_on_multiple_values() {
// Ensures that the function panics if a row contains data in more than one 'value' column, which indicates data corruption. // Ensures that the function panics if a row contains data in more than one 'value' column, which indicates data corruption.
todo!(); let conn = Connection::open_in_memory().unwrap();
// This should panic because both signed_int and text have values.
get_row_from_query(&conn, "SELECT 'a6', 'e1', -42, NULL, NULL, 'hello', NULL").unwrap();
} }
#[test] #[test]
fn map_row_to_attribute_should_return_error_on_type_mismatch() { fn map_row_to_attribute_should_return_error_on_type_mismatch() {
// Checks that a rusqlite::Error is returned if a column's type does not match the expected schema (e.g., text in an integer column). // Checks that a rusqlite::Error is returned if a column's type does not match the expected schema (e.g., text in an integer column).
todo!(); let conn = Connection::open_in_memory().unwrap();
// Trying to get a text value as an integer should fail.
let result = get_row_from_query(
&conn,
// The value 'not a number' is in the signed_int column position.
"SELECT 'a7', 'e1', 'not a number', NULL, NULL, NULL, NULL",
);
assert!(result.is_err());
} }
} }