test: add tests to sqlite_load_by_id function
This commit is contained in:
@@ -26,24 +26,81 @@ pub fn run(path: &path::Path, entity_id: &str) -> Result<Option<Entity>, FailedT
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
use crate::{fun, str::value::Value};
|
||||||
|
use std::{fs, path::Path};
|
||||||
|
fn setup_db(db_path: &Path) {
|
||||||
|
let _ = fs::remove_file(db_path);
|
||||||
|
fun::sqlite_init_db::run(db_path).unwrap();
|
||||||
|
let conn = Connection::open(db_path).unwrap();
|
||||||
|
conn.execute("INSERT INTO entity (id, class) VALUES ('e1', 'c1')", [])
|
||||||
|
.unwrap();
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO attribute (id, entity_id, value_text) VALUES ('a1', 'e1', 'v1')",
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
conn.execute(
|
||||||
|
"INSERT INTO attribute (id, entity_id, value_int) VALUES ('a2', 'e1', 100)",
|
||||||
|
[],
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
conn.execute("INSERT INTO entity (id, class) VALUES ('e2', 'c2')", [])
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn load_by_id_should_fetch_correct_entity() {
|
fn load_by_id_should_fetch_correct_entity() {
|
||||||
// Verifies that the correct entity is retrieved from the database when a valid ID is provided.
|
// Verifies that the correct entity is retrieved from the database when a valid ID is provided.
|
||||||
todo!();
|
let db_path = Path::new("test_fetch_by_id.db");
|
||||||
|
setup_db(db_path);
|
||||||
|
let result = run(db_path, "e1");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
let entity_opt = result.unwrap();
|
||||||
|
assert!(entity_opt.is_some());
|
||||||
|
let entity = entity_opt.unwrap();
|
||||||
|
assert_eq!(entity.id, "e1");
|
||||||
|
assert_eq!(entity.class, "c1");
|
||||||
|
fs::remove_file(db_path).unwrap();
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn load_by_id_should_return_none_for_non_existent_id() {
|
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.
|
// Ensures that `Ok(None)` is returned when querying for an ID that does not exist in the database.
|
||||||
todo!();
|
let db_path = Path::new("test_non_existent_id.db");
|
||||||
|
setup_db(db_path);
|
||||||
|
let result = run(db_path, "non_existent");
|
||||||
|
assert!(result.is_ok());
|
||||||
|
let entity_opt = result.unwrap();
|
||||||
|
assert!(entity_opt.is_none());
|
||||||
|
fs::remove_file(db_path).unwrap();
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn load_by_id_should_load_entity_with_all_attributes() {
|
fn load_by_id_should_load_entity_with_all_attributes() {
|
||||||
// Checks that the retrieved entity includes all of its associated attributes.
|
// Checks that the retrieved entity includes all of its associated attributes.
|
||||||
todo!();
|
let db_path = Path::new("test_load_with_attributes.db");
|
||||||
|
setup_db(db_path);
|
||||||
|
let entity = run(db_path, "e1").unwrap().unwrap();
|
||||||
|
assert_eq!(entity.attributes.len(), 2);
|
||||||
|
let attr1 = entity.attributes.get("a1").unwrap();
|
||||||
|
assert_eq!(attr1.value, Value::Text("v1".to_string()));
|
||||||
|
let attr2 = entity.attributes.get("a2").unwrap();
|
||||||
|
assert_eq!(attr2.value, Value::SignedInt(100));
|
||||||
|
fs::remove_file(db_path).unwrap();
|
||||||
}
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn load_by_id_should_fail_gracefully_on_db_error() {
|
fn load_by_id_should_fail_gracefully_on_db_error() {
|
||||||
// Tests that an error is returned if the database query fails for any reason.
|
// Tests that an error is returned if the database query fails for any reason.
|
||||||
todo!();
|
// Test case 1: Invalid path
|
||||||
|
let invalid_path = Path::new("/");
|
||||||
|
let result = run(invalid_path, "any_id");
|
||||||
|
assert!(result.is_err());
|
||||||
|
// Test case 2: Query error
|
||||||
|
let db_path = Path::new("test_db_error.db");
|
||||||
|
let _ = fs::remove_file(db_path);
|
||||||
|
fun::sqlite_init_db::run(db_path).unwrap();
|
||||||
|
let conn = Connection::open(db_path).unwrap();
|
||||||
|
conn.execute("DROP TABLE entity", []).unwrap();
|
||||||
|
drop(conn); // Close connection before `run` tries to open it.
|
||||||
|
let result = run(db_path, "any_id");
|
||||||
|
assert!(result.is_err());
|
||||||
|
fs::remove_file(db_path).unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user