Skip to content

Commit

Permalink
Merge pull request #3 from giuseppe-g-gelardi/testing
Browse files Browse the repository at this point in the history
adding more test cases
  • Loading branch information
giuseppe-g-gelardi authored Nov 30, 2024
2 parents 6345997 + 4b0ca5a commit f52de5c
Show file tree
Hide file tree
Showing 3 changed files with 261 additions and 22 deletions.
65 changes: 65 additions & 0 deletions TestUpdateAndDelete.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"name": "TestUpdateAndDelete",
"file_name": "TestUpdateAndDelete.json",
"tables": [
{
"name": "TestTracingInfo",
"rows": [
{
"_id": "6efca8a7-3ac5-473f-afa9-7add8365388e",
"data": {
"email": "[email protected]",
"id": "58409c6a-2129-4528-a94f-71685fdfa3c3",
"name": "Jon Doe"
}
},
{
"_id": "68403104-e228-46ee-9308-9a2013e6c0f8",
"data": {
"email": "[email protected]",
"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": "[email protected]",
"id": "bf962a49-7a58-43e1-bd63-cd810050194c",
"name": "Jon Doe"
}
},
{
"_id": "9d271fa4-6a11-43ff-87fb-b8bc0bc18a72",
"data": {
"email": "[email protected]",
"id": "1c6af75d-124c-4db0-b0cf-8c199dedf246",
"name": "Jane Doe"
}
}
],
"columns": [
{
"name": "id",
"required": true
},
{
"name": "name",
"required": true
},
{
"name": "email",
"required": true
}
]
}
]
}
132 changes: 117 additions & 15 deletions src/cargobase/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<TestData> = 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<TestData> = 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::<TestData>("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::<TestData>("id", "1")
.unwrap();

assert!(result.is_some(), "Expected record to exist before deletion");

let _ = db
.delete_single()
.from("TestTable")
.where_eq::<TestData>("id", "1")
.unwrap();

let rows: Vec<TestData> = db.get_rows().from("TestTable").all();
let deleted_record = db
.get_single()
.from("TestTable")
.where_eq::<TestData>("id", "1")
.unwrap();

assert!(deleted_record.is_none(), "Expected record to be deleted");
assert!(rows.is_empty(), "Expected all records to be deleted");
}
}
86 changes: 79 additions & 7 deletions src/cargobase/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]);
Expand All @@ -54,12 +57,81 @@ mod tests {
}

#[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]
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
}

// test add row...
#[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"));
}
}

0 comments on commit f52de5c

Please sign in to comment.