index.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use microrm::prelude::*;
  2. use microrm_macros::index_cols;
  3. use test_log::test;
  4. use microrm::schema::index::Index;
  5. mod common;
  6. #[derive(Entity)]
  7. struct TwoWay {
  8. up: String,
  9. left: String,
  10. right: String,
  11. }
  12. #[derive(Default, Schema)]
  13. struct TWDB {
  14. entries: microrm::IDMap<TwoWay>,
  15. left_index: Index<TwoWay, index_cols![TwoWay::left]>,
  16. right_index: Index<TwoWay, index_cols![TwoWay::right]>,
  17. }
  18. #[test]
  19. fn db_construction() {
  20. let (_, _db): (_, TWDB) = common::open_test_db!();
  21. }
  22. #[test]
  23. fn lookup() {
  24. let (pool, db): (_, TWDB) = common::open_test_db!();
  25. let mut lease = pool.acquire().unwrap();
  26. let e1 = db
  27. .entries
  28. .insert_and_return(
  29. &mut lease,
  30. TwoWay {
  31. up: "up1".into(),
  32. left: "left1".into(),
  33. right: "right1".into(),
  34. },
  35. )
  36. .expect("couldn't insert entry");
  37. let e2 = db
  38. .entries
  39. .insert_and_return(
  40. &mut lease,
  41. TwoWay {
  42. up: "up2".into(),
  43. left: "left2".into(),
  44. right: "right2".into(),
  45. },
  46. )
  47. .expect("couldn't insert entry");
  48. assert!(db
  49. .entries
  50. .indexed(&db.left_index, "left3")
  51. .get(&mut lease)
  52. .expect("couldn't query left index")
  53. .is_none());
  54. assert_eq!(
  55. db.entries
  56. .indexed(&db.left_index, "left2")
  57. .get(&mut lease)
  58. .expect("couldn't query left index")
  59. .as_ref(),
  60. Some(&e2)
  61. );
  62. assert_eq!(
  63. db.entries
  64. .indexed(&db.left_index, "left1")
  65. .get(&mut lease)
  66. .expect("couldn't query left index")
  67. .as_ref(),
  68. Some(&e1)
  69. );
  70. assert!(db
  71. .entries
  72. .indexed(&db.right_index, "right3")
  73. .get(&mut lease)
  74. .expect("couldn't query right index")
  75. .is_none());
  76. assert_eq!(
  77. db.entries
  78. .indexed(&db.right_index, "right2")
  79. .get(&mut lease)
  80. .expect("couldn't query right index")
  81. .as_ref(),
  82. Some(&e2)
  83. );
  84. assert_eq!(
  85. db.entries
  86. .indexed(&db.right_index, "right1")
  87. .get(&mut lease)
  88. .expect("couldn't query right index")
  89. .as_ref(),
  90. Some(&e1)
  91. );
  92. }