use microrm::prelude::*; use test_log::test; mod common; struct AuthorBookRelation; impl microrm::Relation for AuthorBookRelation { type Domain = Author; type Range = Book; const NAME: &'static str = "AuthorBook"; const INJECTIVE: bool = true; } #[derive(Entity)] struct Author { #[key] name: String, works: microrm::RelationDomain, } #[derive(Entity)] struct Book { #[key] title: String, creator: microrm::RelationRange, } #[derive(Default, Schema)] struct ABDB { authors: microrm::IDMap, books: microrm::IDMap, } #[test] fn schema_creation() { let (_pool, _db): (_, ABDB) = common::open_test_db!(); } #[test] fn single_connection() { let (pool, db): (_, ABDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let a1 = db .authors .insert_and_return( &mut lease, Author { name: "Homer".to_string(), works: Default::default(), }, ) .expect("couldn't insert author"); let a2 = db .authors .insert_and_return( &mut lease, Author { name: "Virgil".to_string(), works: Default::default(), }, ) .expect("couldn't insert author"); let b1_id = db .books .insert( &mut lease, Book { title: "Odyssey".to_string(), creator: Default::default(), }, ) .expect("couldn't insert book"); let b2_id = db .books .insert( &mut lease, Book { title: "Aeneid".to_string(), creator: Default::default(), }, ) .expect("couldn't insert book"); a1.works .connect_to(&mut lease, b1_id) .expect("couldn't connect a1 and b1"); a2.works .connect_to(&mut lease, b2_id) .expect("couldn't connect a2 and b2"); // we can't claim that Homer wrote the Aeneid because it's an injective relationship and // in this model, only one Author can claim the Book as their work match a1.works.connect_to(&mut lease, b2_id) { Err(microrm::Error::ConstraintViolation(_)) => { // all good }, Err(_) => { panic!("Unexpected error while testing injective connection"); }, Ok(_) => { panic!("Unexpected success while testing injective connection"); }, } }