From 4cde1ab98e0e954e67dad41d49d28c885831f893 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Fri, 29 Nov 2024 22:01:16 -0500 Subject: [PATCH 1/2] update unit tests for Table --- src/cargobase/table.rs | 94 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 87 insertions(+), 7 deletions(-) diff --git a/src/cargobase/table.rs b/src/cargobase/table.rs index d26bff9..cd6ee47 100644 --- a/src/cargobase/table.rs +++ b/src/cargobase/table.rs @@ -42,9 +42,12 @@ impl Table { #[cfg(test)] mod tests { - use super::super::Column; + use super::super::{setup_temp_db, Column, Columns}; use super::*; + use serde_json::json; + use tracing_test::traced_test; + #[test] fn test_table_new() { let columns = Columns::new(vec![Column::new("name", true), Column::new("age", false)]); @@ -53,13 +56,90 @@ mod tests { assert_eq!(table.columns, columns); } + // #[test] + // fn test_table_set_file_name() { + // let columns = Columns::new(vec![Column::new("name", true), Column::new("age", false)]); + // let table = Table::new("users".to_string(), columns.clone()); + // // table.set_file_name("db.json".to_string()); + // assert_eq!(table.name, "users"); + // } + #[test] - fn test_table_set_file_name() { - let columns = Columns::new(vec![Column::new("name", true), Column::new("age", false)]); - let table = Table::new("users".to_string(), columns.clone()); - // table.set_file_name("db.json".to_string()); - assert_eq!(table.name, "users"); + fn test_table_add_row_single() { + let mut db = setup_temp_db(); + let mut table = Table::new( + "TestTable".to_string(), + Columns::new(vec![Column::new("id", true), Column::new("name", true)]), + ); + db.add_table(&mut table).unwrap(); + + let row_data = json!({"id": "1", "name": "John Doe"}); + table.add_row(&mut db, row_data); + + assert_eq!(db.tables[0].rows.len(), 1); + assert_eq!( + db.tables[0].rows[0].data, + json!({"id": "1", "name": "John Doe"}) + ); } - // test add row... + #[test] + fn test_table_add_row_array() { + let mut db = setup_temp_db(); + let mut table = Table::new( + "TestTable".to_string(), + Columns::new(vec![Column::new("id", true), Column::new("name", true)]), + ); + db.add_table(&mut table).unwrap(); + + let row_data = json!([ + {"id": "1", "name": "John Doe"}, + {"id": "2", "name": "Jane Doe"} + ]); + table.add_row(&mut db, row_data); + + assert_eq!(db.tables[0].rows.len(), 2); + assert_eq!( + db.tables[0].rows[0].data, + json!({"id": "1", "name": "John Doe"}) + ); + assert_eq!( + db.tables[0].rows[1].data, + json!({"id": "2", "name": "Jane Doe"}) + ); + } + + #[traced_test] + #[test] + fn test_table_add_row_table_not_found() { + let mut db = setup_temp_db(); + let mut table = Table::new( + "NonExistentTable".to_string(), + Columns::new(vec![Column::new("id", true), Column::new("name", true)]), + ); + + let row_data = json!({"id": "1", "name": "John Doe"}); + table.add_row(&mut db, row_data); + + assert!(logs_contain("Table NonExistentTable not found")); + assert_eq!(db.tables.len(), 1); // Original table remains unchanged + } + + #[traced_test] + #[test] + fn test_table_add_row_save_failure() { + let mut db = setup_temp_db(); + let mut table = Table::new( + "TestTable".to_string(), + Columns::new(vec![Column::new("id", true), Column::new("name", true)]), + ); + db.add_table(&mut table).unwrap(); + + // Simulate failure in saving + db.file_name = "/invalid/path.json".to_string(); + let row_data = json!({"id": "1", "name": "John Doe"}); + table.add_row(&mut db, row_data); + + assert!(logs_contain("Failed to save to file")); + } } From 4b0ca5a82419cc1fb554498abaa866ce36a2fa54 Mon Sep 17 00:00:00 2001 From: giuseppe-g-gelardi Date: Fri, 29 Nov 2024 22:54:17 -0500 Subject: [PATCH 2/2] add test cases for query and table --- TestUpdateAndDelete.json | 65 +++++++++++++++++++ src/cargobase/query.rs | 132 ++++++++++++++++++++++++++++++++++----- src/cargobase/table.rs | 8 --- 3 files changed, 182 insertions(+), 23 deletions(-) create mode 100644 TestUpdateAndDelete.json diff --git a/TestUpdateAndDelete.json b/TestUpdateAndDelete.json new file mode 100644 index 0000000..72f498a --- /dev/null +++ b/TestUpdateAndDelete.json @@ -0,0 +1,65 @@ +{ + "name": "TestUpdateAndDelete", + "file_name": "TestUpdateAndDelete.json", + "tables": [ + { + "name": "TestTracingInfo", + "rows": [ + { + "_id": "6efca8a7-3ac5-473f-afa9-7add8365388e", + "data": { + "email": "jondoe@email.com", + "id": "58409c6a-2129-4528-a94f-71685fdfa3c3", + "name": "Jon Doe" + } + }, + { + "_id": "68403104-e228-46ee-9308-9a2013e6c0f8", + "data": { + "email": "janedoe@email.com", + "id": "4bf896a0-f807-47a3-b2fe-6a5453f21578", + "name": "Jane Doe" + } + }, + { + "_id": "8cdefe82-bd2f-4ea5-bacf-ce060df7f794", + "data": { + "email": null, + "id": "c0c0200d-7dc9-4881-9839-b83f6cf4f82a", + "name": "alice cooper" + } + }, + { + "_id": "a2c340db-6f29-42c5-be6c-460cb6877dbe", + "data": { + "email": "jondoe@email.com", + "id": "bf962a49-7a58-43e1-bd63-cd810050194c", + "name": "Jon Doe" + } + }, + { + "_id": "9d271fa4-6a11-43ff-87fb-b8bc0bc18a72", + "data": { + "email": "janedoe@email.com", + "id": "1c6af75d-124c-4db0-b0cf-8c199dedf246", + "name": "Jane Doe" + } + } + ], + "columns": [ + { + "name": "id", + "required": true + }, + { + "name": "name", + "required": true + }, + { + "name": "email", + "required": true + } + ] + } + ] +} \ No newline at end of file diff --git a/src/cargobase/query.rs b/src/cargobase/query.rs index 6f2177f..6a06f79 100644 --- a/src/cargobase/query.rs +++ b/src/cargobase/query.rs @@ -267,21 +267,6 @@ mod tests { assert_eq!(updated_query.table_name, Some("TestTable".to_string())); } - #[test] - fn test_query_to() { - let query = Query { - db_file_name: "test_db.json".to_string(), - table_name: None, - operation: Operation::Create, - update_data: None, - row_data: None, - }; - - // let updated_query = query.to("TestTable"); - let updated_query = query.from("TestTable"); - assert_eq!(updated_query.table_name, Some("TestTable".to_string())); - } - #[test] fn test_query_data() { let query = Query { @@ -368,4 +353,121 @@ mod tests { let updated_query = query.set(data.clone()); assert_eq!(updated_query.update_data, Some(data)); } + + #[test] + fn test_query_where_eq_no_match() { + let mut db = setup_temp_db(); + + let test_data = TestData { + id: "1".to_string(), + name: "Alice".to_string(), + }; + + db.add_row() + .from("TestTable") + .data_from_struct(test_data) + .execute_add() + .expect("Failed to add row"); + + let result: Option = db + .get_rows() + .from("TestTable") + .where_eq("id", "999") + .unwrap(); + assert!(result.is_none(), "Expected no matching record"); + } + + #[test] + fn test_query_where_eq_match() { + let mut db = setup_temp_db(); + + let test_data = TestData { + id: "1".to_string(), + name: "Alice".to_string(), + }; + + db.add_row() + .from("TestTable") + .data_from_struct(test_data.clone()) + .execute_add() + .expect("Failed to add row"); + + let result: Option = db.get_rows().from("TestTable").where_eq("id", "1").unwrap(); + assert_eq!(result, Some(test_data), "Expected matching record"); + } + + #[test] + fn test_query_execute_update() { + let mut db = setup_temp_db(); + + let test_data = TestData { + id: "1".to_string(), + name: "Alice".to_string(), + }; + + db.add_row() + .from("TestTable") + .data_from_struct(test_data.clone()) + .execute_add() + .expect("Failed to add row"); + + let update_data = json!({ "name": "Updated Alice" }); + + let result = db + .update_row() + .from("TestTable") + .data(update_data) + .where_eq::("id", "1") + .unwrap(); + + assert!( + result.is_some(), + "Expected update to return the updated record" + ); + assert_eq!( + result.unwrap().name, + "Updated Alice", + "Name was not updated" + ); + } + + #[test] + fn test_query_execute_delete() { + let mut db = setup_temp_db(); + + let test_data = TestData { + id: "1".to_string(), + name: "Alice".to_string(), + }; + + db.add_row() + .from("TestTable") + .data_from_struct(test_data.clone()) + .execute_add() + .expect("Failed to add row"); + + let result = db + .get_single() + .from("TestTable") + .where_eq::("id", "1") + .unwrap(); + + assert!(result.is_some(), "Expected record to exist before deletion"); + + let _ = db + .delete_single() + .from("TestTable") + .where_eq::("id", "1") + .unwrap(); + + let rows: Vec = db.get_rows().from("TestTable").all(); + let deleted_record = db + .get_single() + .from("TestTable") + .where_eq::("id", "1") + .unwrap(); + + assert!(deleted_record.is_none(), "Expected record to be deleted"); + assert!(rows.is_empty(), "Expected all records to be deleted"); + } } diff --git a/src/cargobase/table.rs b/src/cargobase/table.rs index cd6ee47..3fb4571 100644 --- a/src/cargobase/table.rs +++ b/src/cargobase/table.rs @@ -56,14 +56,6 @@ mod tests { assert_eq!(table.columns, columns); } - // #[test] - // fn test_table_set_file_name() { - // let columns = Columns::new(vec![Column::new("name", true), Column::new("age", false)]); - // let table = Table::new("users".to_string(), columns.clone()); - // // table.set_file_name("db.json".to_string()); - // assert_eq!(table.name, "users"); - // } - #[test] fn test_table_add_row_single() { let mut db = setup_temp_db();