From 6242404cfbe55fda2c0ad6855cf6da4b080d65e7 Mon Sep 17 00:00:00 2001 From: davidemazzocchi Date: Thu, 9 Oct 2025 21:09:31 +0200 Subject: [PATCH] test: add a placeholder test battery for each sqlite function --- 01.workspace/heave/src/fun/sqlite_init_db.rs | 20 ++++++--- .../heave/src/fun/sqlite_load_attributes.rs | 20 +++++++++ .../heave/src/fun/sqlite_load_by_class.rs | 25 +++++++++++ .../heave/src/fun/sqlite_load_by_id.rs | 25 +++++++++++ .../src/fun/sqlite_map_row_to_attribute.rs | 41 +++++++++++++++++++ .../heave/src/fun/sqlite_map_row_to_entity.rs | 20 +++++++++ .../heave/src/fun/sqlite_persist_catalog.rs | 30 ++++++++++++++ 7 files changed, 175 insertions(+), 6 deletions(-) diff --git a/01.workspace/heave/src/fun/sqlite_init_db.rs b/01.workspace/heave/src/fun/sqlite_init_db.rs index 7a4ebe6..54a0165 100644 --- a/01.workspace/heave/src/fun/sqlite_init_db.rs +++ b/01.workspace/heave/src/fun/sqlite_init_db.rs @@ -30,13 +30,21 @@ pub fn run(path: &path::Path) -> result::Result<(), FailedTo> { } #[cfg(test)] -mod unit_tests { +mod tests { use super::*; #[test] - fn test_call() { - let tempfile = tempfile::NamedTempFile::new().unwrap(); - let path = tempfile.path(); - let result = run(path); - assert!(result.is_ok()); + fn init_db_should_create_tables_and_indexes_on_new_db() { + // This test verifies that a new database is correctly initialized with the necessary tables ('entity', 'attribute') and indexes. + todo!(); + } + #[test] + fn init_db_should_be_idempotent_and_not_fail_on_existing_db() { + // This test ensures that initializing an already existing and initialized database does not cause errors. + todo!(); + } + #[test] + fn init_db_should_fail_gracefully_on_invalid_path() { + // This test checks that the function returns an error when provided with an invalid or inaccessible file path. + todo!(); } } diff --git a/01.workspace/heave/src/fun/sqlite_load_attributes.rs b/01.workspace/heave/src/fun/sqlite_load_attributes.rs index 4b7cee0..01b6ed8 100644 --- a/01.workspace/heave/src/fun/sqlite_load_attributes.rs +++ b/01.workspace/heave/src/fun/sqlite_load_attributes.rs @@ -19,3 +19,23 @@ pub fn run(transaction: &Transaction, entity: &mut Entity) -> Result<(), FailedT } Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn load_attributes_should_populate_entity_from_db() { + // Verifies that attributes for a given entity are correctly loaded from the database and added to the entity's attributes map. + todo!(); + } + #[test] + fn load_attributes_should_handle_entities_with_no_attributes() { + // Ensures that the function completes without error and without adding attributes for an entity that has none in the database. + todo!(); + } + #[test] + fn load_attributes_should_return_error_on_query_failure() { + // Checks that an error is returned if the database query to select attributes fails. + todo!(); + } +} diff --git a/01.workspace/heave/src/fun/sqlite_load_by_class.rs b/01.workspace/heave/src/fun/sqlite_load_by_class.rs index 81fc509..5e2f5da 100644 --- a/01.workspace/heave/src/fun/sqlite_load_by_class.rs +++ b/01.workspace/heave/src/fun/sqlite_load_by_class.rs @@ -27,3 +27,28 @@ pub fn run(path: &path::Path, entity_class: &str) -> Result, FailedT } Ok(entities) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn load_by_class_should_fetch_all_entities_for_a_given_class() { + // Verifies that all entities belonging to a specific class are retrieved from the database. + todo!(); + } + #[test] + fn load_by_class_should_return_empty_vec_for_non_existent_class() { + // Ensures that an empty vector is returned when querying for a class that has no entities in the database. + todo!(); + } + #[test] + fn load_by_class_should_fully_load_entities_with_attributes() { + // Checks that the retrieved entities are complete, including all their associated attributes. + todo!(); + } + #[test] + fn load_by_class_should_fail_gracefully_on_db_connection_error() { + // Tests that the function returns an appropriate error if the database connection cannot be established. + todo!(); + } +} diff --git a/01.workspace/heave/src/fun/sqlite_load_by_id.rs b/01.workspace/heave/src/fun/sqlite_load_by_id.rs index 9ff0501..cce2bd2 100644 --- a/01.workspace/heave/src/fun/sqlite_load_by_id.rs +++ b/01.workspace/heave/src/fun/sqlite_load_by_id.rs @@ -22,3 +22,28 @@ pub fn run(path: &path::Path, entity_id: &str) -> Result, FailedT } Ok(entity) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn load_by_id_should_fetch_correct_entity() { + // Verifies that the correct entity is retrieved from the database when a valid ID is provided. + todo!(); + } + #[test] + fn load_by_id_should_return_none_for_non_existent_id() { + // Ensures that `Ok(None)` is returned when querying for an ID that does not exist in the database. + todo!(); + } + #[test] + fn load_by_id_should_load_entity_with_all_attributes() { + // Checks that the retrieved entity includes all of its associated attributes. + todo!(); + } + #[test] + fn load_by_id_should_fail_gracefully_on_db_error() { + // Tests that an error is returned if the database query fails for any reason. + todo!(); + } +} diff --git a/01.workspace/heave/src/fun/sqlite_map_row_to_attribute.rs b/01.workspace/heave/src/fun/sqlite_map_row_to_attribute.rs index 75bb035..e5037d0 100644 --- a/01.workspace/heave/src/fun/sqlite_map_row_to_attribute.rs +++ b/01.workspace/heave/src/fun/sqlite_map_row_to_attribute.rs @@ -18,3 +18,44 @@ pub fn run(row: &rusqlite::Row) -> rusqlite::Result { }; Ok(Attribute { id, value }) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } + #[test] + #[should_panic] + 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. + todo!(); + } + #[test] + 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). + todo!(); + } +} diff --git a/01.workspace/heave/src/fun/sqlite_map_row_to_entity.rs b/01.workspace/heave/src/fun/sqlite_map_row_to_entity.rs index a225f5f..79d431e 100644 --- a/01.workspace/heave/src/fun/sqlite_map_row_to_entity.rs +++ b/01.workspace/heave/src/fun/sqlite_map_row_to_entity.rs @@ -13,3 +13,23 @@ pub fn run(row: &rusqlite::Row) -> rusqlite::Result { }; Ok(entity) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } + #[test] + 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. + todo!(); + } +} diff --git a/01.workspace/heave/src/fun/sqlite_persist_catalog.rs b/01.workspace/heave/src/fun/sqlite_persist_catalog.rs index 7eaf02f..465ef12 100644 --- a/01.workspace/heave/src/fun/sqlite_persist_catalog.rs +++ b/01.workspace/heave/src/fun/sqlite_persist_catalog.rs @@ -85,3 +85,33 @@ pub fn run(path: &path::Path, catalog: &Catalog) -> result::Result<(), FailedTo> .map_err(|_| sqlite::FailedTo::CommitTransaction)?; Ok(()) } + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn persist_should_insert_new_entities_and_attributes() { + // Verifies that entities marked as 'New' in the catalog are inserted into the database, along with all their attributes. + todo!(); + } + #[test] + fn persist_should_delete_entities_marked_for_deletion() { + // Ensures that entities marked as 'ToDelete' are correctly removed from the database. + todo!(); + } + #[test] + fn persist_should_handle_a_mix_of_new_and_deleted_entities() { + // Tests the function's ability to handle a batch operation involving both new entities to be inserted and existing ones to be deleted. + todo!(); + } + #[test] + fn persist_should_not_affect_unmodified_entities() { + // Verifies that entities in the catalog that are not marked as 'New' or 'ToDelete' remain untouched in the database. + todo!(); + } + #[test] + fn persist_should_rollback_transaction_on_failure() { + // Ensures that if any part of the persistence process fails, the entire transaction is rolled back, leaving the database state unchanged. + todo!(); + } +}