test: add tests to sqlite_map_row_to_entity function

This commit is contained in:
2025-10-16 15:56:53 +02:00
parent f9d2f1c4c0
commit 02178c018c

View File

@@ -17,19 +17,37 @@ pub fn run(row: &rusqlite::Row) -> rusqlite::Result<Entity> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use rusqlite::{Connection, Result};
fn get_entity_from_query(conn: &Connection, query: &str) -> Result<Entity, rusqlite::Error> {
let mut stmt = conn.prepare(query).unwrap();
stmt.query_row([], run)
}
#[test] #[test]
fn map_row_to_entity_should_correctly_map_valid_row() { fn map_row_to_entity_should_correctly_map_valid_row() {
// Verifies that a valid database row is correctly mapped to an Entity struct with all fields populated. // Verifies that a valid database row is correctly mapped to an Entity struct with all fields populated.
todo!(); let conn = Connection::open_in_memory().unwrap();
let entity = get_entity_from_query(&conn, "SELECT 'e1', 'c1', 12345").unwrap();
assert_eq!(entity.id, "e1");
assert_eq!(entity.class, "c1");
assert_eq!(entity.ref_date, Some(12345));
assert_eq!(entity.state, EntityState::Loaded);
assert!(entity.attributes.is_empty());
} }
#[test] #[test]
fn map_row_to_entity_should_handle_null_ref_date() { fn map_row_to_entity_should_handle_null_ref_date() {
// Ensures that a row with a NULL 'ref_date' is successfully mapped to an Entity with 'ref_date' as None. // Ensures that a row with a NULL 'ref_date' is successfully mapped to an Entity with 'ref_date' as None.
todo!(); let conn = Connection::open_in_memory().unwrap();
let entity = get_entity_from_query(&conn, "SELECT 'e2', 'c2', NULL").unwrap();
assert_eq!(entity.id, "e2");
assert_eq!(entity.class, "c2");
assert_eq!(entity.ref_date, None);
} }
#[test] #[test]
fn map_row_to_entity_should_return_error_on_type_mismatch() { fn map_row_to_entity_should_return_error_on_type_mismatch() {
// Checks that a rusqlite::Error is returned if a column's type does not match the expected schema. // Checks that a rusqlite::Error is returned if a column's type does not match the expected schema.
todo!(); let conn = Connection::open_in_memory().unwrap();
// Trying to get an integer as a string for the class should fail.
let result = get_entity_from_query(&conn, "SELECT 'e3', 123, NULL");
assert!(result.is_err());
} }
} }