mutual.rs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. use microrm::prelude::*;
  2. use test_log::test;
  3. mod common;
  4. struct CR;
  5. impl Relation for CR {
  6. type Domain = Customer;
  7. type Range = Receipt;
  8. const NAME: &'static str = "CR";
  9. }
  10. #[derive(Entity)]
  11. struct Customer {
  12. name: String,
  13. receipts: microrm::RelationDomain<CR>,
  14. }
  15. #[derive(Entity)]
  16. struct Receipt {
  17. value: usize,
  18. customers: microrm::RelationRange<CR>,
  19. }
  20. #[derive(Default, Schema)]
  21. struct ReceiptDB {
  22. customers: microrm::IDMap<Customer>,
  23. receipts: microrm::IDMap<Receipt>,
  24. }
  25. #[test]
  26. fn check_schema_creation() {
  27. let (_pool, _db): (_, ReceiptDB) = common::open_test_db!();
  28. }
  29. #[test]
  30. fn simple_operations() {
  31. let (pool, db): (_, ReceiptDB) = common::open_test_db!();
  32. let mut lease = pool.acquire().unwrap();
  33. let ca = db
  34. .customers
  35. .insert(
  36. &mut lease,
  37. Customer {
  38. name: "customer A".to_string(),
  39. receipts: Default::default(),
  40. },
  41. )
  42. .expect("couldn't insert customer record");
  43. let ra = db
  44. .receipts
  45. .insert(
  46. &mut lease,
  47. Receipt {
  48. value: 32usize,
  49. customers: Default::default(),
  50. },
  51. )
  52. .expect("couldn't insert receipt record");
  53. let rb = db
  54. .receipts
  55. .insert(
  56. &mut lease,
  57. Receipt {
  58. value: 64usize,
  59. customers: Default::default(),
  60. },
  61. )
  62. .expect("couldn't insert receipt record");
  63. let e_ca = db
  64. .customers
  65. .by_id(&mut lease, ca)
  66. .expect("couldn't retrieve customer record")
  67. .expect("no customer record");
  68. log::trace!(
  69. "connecting customer a (ID {:?}) to receipt a (ID {:?})",
  70. ca,
  71. ra
  72. );
  73. e_ca.receipts
  74. .connect_to(&mut lease, ra)
  75. .expect("couldn't relationiate customer with receipt a");
  76. log::trace!(
  77. "connecting customer a (ID {:?}) to receipt b (ID {:?})",
  78. ca,
  79. rb
  80. );
  81. e_ca.receipts
  82. .connect_to(&mut lease, rb)
  83. .expect("couldn't relationiate customer with receipt b");
  84. log::trace!("connected!");
  85. // technically this can fail if sqlite gives ra and rb back in the opposite order, which is
  86. // valid behaviour
  87. assert_eq!(
  88. e_ca.receipts
  89. .get(&mut lease)
  90. .expect("couldn't get receipts related with customer")
  91. .into_iter()
  92. .map(|x| x.id())
  93. .collect::<Vec<_>>(),
  94. vec![ra, rb]
  95. );
  96. // check that the reverse direction was also added
  97. let e_ra = db
  98. .receipts
  99. .by_id(&mut lease, ra)
  100. .expect("couldn't retreieve receipt record")
  101. .expect("no receipt record");
  102. assert_eq!(
  103. e_ra.customers
  104. .get(&mut lease)
  105. .expect("couldn't get related customers")
  106. .into_iter()
  107. .map(|x| x.id())
  108. .collect::<Vec<_>>(),
  109. vec![ca]
  110. );
  111. }