fkey.rs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use microrm::prelude::*;
  2. use test_log::test;
  3. mod common;
  4. #[derive(Entity)]
  5. struct Child {
  6. #[key]
  7. name: String,
  8. parent: ParentID,
  9. }
  10. #[derive(Entity)]
  11. struct Parent {
  12. name: String,
  13. }
  14. #[derive(Default, Schema)]
  15. struct PeopleDB {
  16. children: microrm::IDMap<Child>,
  17. parents: microrm::IDMap<Parent>,
  18. }
  19. #[test]
  20. fn simple_query() {
  21. let (pool, db): (_, PeopleDB) = common::open_test_db!();
  22. let mut lease = pool.acquire().unwrap();
  23. let pid = db
  24. .parents
  25. .insert(
  26. &mut lease,
  27. Parent {
  28. name: "parent1".to_string(),
  29. },
  30. )
  31. .unwrap();
  32. let _c1id = db
  33. .children
  34. .insert(
  35. &mut lease,
  36. Child {
  37. name: "child1".to_string(),
  38. parent: pid,
  39. },
  40. )
  41. .unwrap();
  42. assert_eq!(
  43. db.children
  44. .foreign(Child::Parent)
  45. .get_ids(&mut lease)
  46. .expect("couldn't run query"),
  47. vec![pid]
  48. );
  49. assert_eq!(
  50. db.children
  51. .keyed("child1")
  52. .foreign(Child::Parent)
  53. .get_ids(&mut lease)
  54. .expect("couldn't run query"),
  55. Some(pid)
  56. );
  57. }