diff --git a/src/database/relation.rs b/src/database/relation.rs index 3a02d0f..1c9b201 100644 --- a/src/database/relation.rs +++ b/src/database/relation.rs @@ -1,10 +1,8 @@ -use std::{borrow::BorrowMut, cell::RefCell, ops::DerefMut}; - use super::{row::Row, table::Table, Database}; use serde::{Deserialize, Serialize}; use serde_json::Value; -#[derive(Clone, Serialize, Deserialize)] +#[derive(Clone, Serialize, Deserialize, Debug)] pub struct Relation { pub table_name: String, variation: String, @@ -46,18 +44,24 @@ impl OneToOne { } pub fn get(&self, table_name: String, database: &mut Database) -> Result { - let relation: Relation = { + let relation_result: Result<&Relation, String> = { // Get table - let table: &mut Table = database.get_table(table_name).unwrap(); + let table: &mut Table = match database.get_table(table_name) { + Ok(relation) => relation, + Err(error) => return Err(error), + }; // Fetch the Relation - table.get_relation(&self.relation_name).unwrap().clone() + table.get_relation(&self.relation_name) }; - // Fetch row from foreign table - let foreign_row = relation.get_foreign_row(database, self.foreign_id)?; - // Now you can continue using 'relation' or anything else - Ok(foreign_row) + let relation: Relation = match relation_result { + Ok(relation) => relation.clone(), + Err(error) => return Err(error), + }; + + // Fetch row from foreign table + relation.get_foreign_row(database, self.foreign_id) } pub fn from_value(value: Value) -> OneToOne { diff --git a/src/tests/relation_test.rs b/src/tests/relation_test.rs index e5a6dbb..bda2463 100644 --- a/src/tests/relation_test.rs +++ b/src/tests/relation_test.rs @@ -9,7 +9,7 @@ mod relation_test { }; #[test] - fn should_fail_no_relation_found() { + fn should_succed_relationship_found() { let cat_relations = hashmap!["cat_food" => Relation::new("Food".to_string(), "one_to_one".to_string())]; let cat_table: Table = Table::new("Cats", "id", None, Some(cat_relations)); @@ -42,11 +42,85 @@ mod relation_test { foreign_row_unwrapped.get("id").unwrap().as_u64().unwrap(), 123u64 ); + print!( + "Printing foreign row {}\n", + serde_json::to_string(&foreign_row_unwrapped).unwrap() + ); + } + + #[test] + fn should_fail_no_relation_found() { + let cat_table: Table = Table::new("Cats", "id", None, None); + + let food_table: Table = Table::new("Food", "id", None, None); + + let mut database: Database = Database::new(vec![cat_table, food_table]); + + _ = database + .get_table("Food".to_string()) + .unwrap() + .insert_row(row!["id" => 123, "name" => "Dry Feed"]); + let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]); + + let cat_table = database.get_table("Cats".to_string()).unwrap(); + let cat_1 = cat_table.find_by_pk(1u64).unwrap(); + + let cat_1_food = cat_1.get("food").unwrap(); + + let relation = OneToOne::from_value(cat_1_food.to_owned()); + assert_eq!(relation.get_id(), 123u64); + + let foreign_row = relation.get("Food".to_string(), database.borrow_mut()); + + assert_eq!(foreign_row.is_err(), true); + } + + #[test] + fn should_fail_no_table_found() { + let cat_table: Table = Table::new("Cats", "id", None, None); + + let mut database: Database = Database::new(vec![cat_table]); + + let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]); + + let cat_table = database.get_table("Cats".to_string()).unwrap(); + let cat_1 = cat_table.find_by_pk(1u64).unwrap(); + + let cat_1_food = cat_1.get("food").unwrap(); + + let relation = OneToOne::from_value(cat_1_food.to_owned()); + assert_eq!(relation.get_id(), 123u64); + + let foreign_row = relation.get("Food".to_string(), database.borrow_mut()); + print!( + "Printing foreign row error {}\n", + &foreign_row.unwrap_err().as_str() + ); } #[test] - fn should_fail_no_table_found() {} - #[test] - fn should_fail_no_row_found() {} + fn should_fail_no_row_found() { + let cat_table: Table = Table::new("Cats", "id", None, None); + + let food_table: Table = Table::new("Food", "id", None, None); + + let mut database: Database = Database::new(vec![cat_table, food_table]); + + let _ = database.get_table("Cats".to_string()).unwrap().insert_row(row!["id" => 1, "name" => "Ozzy", "breed" => "mixed", "food" => OneToOne::new(123u64, "cat_food".to_string())]); + + let cat_table = database.get_table("Cats".to_string()).unwrap(); + let cat_1 = cat_table.find_by_pk(1u64).unwrap(); + + let cat_1_food = cat_1.get("food").unwrap(); + + let relation = OneToOne::from_value(cat_1_food.to_owned()); + assert_eq!(relation.get_id(), 123u64); + + let foreign_row = relation.get("Cats".to_string(), database.borrow_mut()); + print!( + "Printing foreign row error {}\n", + &foreign_row.unwrap_err().as_str() + ); + } // Fail without table found, invalid relation // Fail row and table found but not row }