mirror of
https://github.com/eliasrenman/r-database.git
synced 2026-03-16 20:46:08 +01:00
feat: basic one to one relation
This commit is contained in:
52
src/tests/relation_test.rs
Normal file
52
src/tests/relation_test.rs
Normal file
@@ -0,0 +1,52 @@
|
||||
#[cfg(test)]
|
||||
mod relation_test {
|
||||
use std::borrow::BorrowMut;
|
||||
|
||||
use crate::database::{
|
||||
relation::{OneToOne, Relation},
|
||||
table::Table,
|
||||
Database,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn should_fail_no_relation_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));
|
||||
|
||||
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("Cats".to_string(), database.borrow_mut());
|
||||
|
||||
assert_eq!(foreign_row.is_ok(), true);
|
||||
|
||||
let foreign_row_unwrapped = foreign_row.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
foreign_row_unwrapped.get("id").unwrap().as_u64().unwrap(),
|
||||
123u64
|
||||
);
|
||||
}
|
||||
#[test]
|
||||
fn should_fail_no_table_found() {}
|
||||
#[test]
|
||||
fn should_fail_no_row_found() {}
|
||||
// Fail without table found, invalid relation
|
||||
// Fail row and table found but not row
|
||||
}
|
||||
Reference in New Issue
Block a user