use microrm::prelude::*; use test_log::test; mod common; #[derive(Default, Entity)] struct Base { name: String, targets: microrm::RelationMap, } #[derive(Default, Entity)] struct Target { name: String, indirect_targets: microrm::RelationMap, } #[derive(Default, Entity)] struct IndirectTarget { name: String, } #[derive(Default, Schema)] struct JoinDB { bases: microrm::IDMap, targets: microrm::IDMap, indirect: microrm::IDMap, } #[test] fn simple_ops() { let (pool, db): (_, JoinDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let b1id = db .bases .insert( &mut lease, Base { name: "base1".to_string(), targets: Default::default(), }, ) .expect("couldn't insert base"); let b2id = db .bases .insert( &mut lease, Base { name: "base2".to_string(), targets: Default::default(), }, ) .expect("couldn't insert base"); let _b3id = db .bases .insert( &mut lease, Base { name: "base3".to_string(), targets: Default::default(), }, ) .expect("couldn't insert base"); let t1id = db .targets .insert( &mut lease, Target { name: "target1".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); let t2id = db .targets .insert( &mut lease, Target { name: "target2".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); let t3id = db .targets .insert( &mut lease, Target { name: "target3".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); let it1id = db .indirect .insert( &mut lease, IndirectTarget { name: "itarget1".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); let _it2id = db .indirect .insert( &mut lease, IndirectTarget { name: "itarget2".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); let _it3id = db .indirect .insert( &mut lease, IndirectTarget { name: "itarget3".to_string(), ..Default::default() }, ) .expect("couldn't insert target"); log::trace!("looking up base by ID {:?}", b1id); let b1 = db .bases .by_id(&mut lease, b1id) .expect("couldn't get base") .expect("couldn't get base"); b1.targets .connect_to(&mut lease, t1id) .expect("couldn't connect b1 to t1id"); b1.targets .connect_to(&mut lease, t2id) .expect("couldn't connect b1 to t2id"); let b2 = db .bases .by_id(&mut lease, b2id) .expect("couldn't get base") .expect("couldn't get base"); b2.targets .connect_to(&mut lease, t2id) .expect("couldn't connect b2 to t2id"); b2.targets .connect_to(&mut lease, t3id) .expect("couldn't connect b2 to t3id"); let t1 = db .targets .by_id(&mut lease, t2id) .expect("couldn't get target") .expect("couldn't get target"); t1.indirect_targets .connect_to(&mut lease, it1id) .expect("couldn't connect t1 to it1id"); assert_eq!( db.bases .join(Base::Targets) .get(&mut lease) .expect("couldn't get joined results") .len(), 3 ); let double_join = db .bases .join(Base::Targets) .join(Target::IndirectTargets) .get(&mut lease) .expect("couldn't get double-joined results"); assert_eq!(double_join.len(), 1); let double_join_count = db .bases .join(Base::Targets) .join(Target::IndirectTargets) .count(&mut lease) .expect("couldn't count double-joined results"); assert_eq!(double_join_count, 1); }