value_proxy.rs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. use microrm::prelude::*;
  2. use test_log::test;
  3. mod common;
  4. #[derive(PartialEq, Clone, Debug, Value, serde::Serialize, serde::Deserialize)]
  5. enum PersonState {
  6. Unborn,
  7. Alive,
  8. Dead,
  9. Undead,
  10. }
  11. #[derive(Entity)]
  12. struct Person {
  13. #[key]
  14. name: String,
  15. state: PersonState,
  16. }
  17. #[derive(Default, Schema)]
  18. struct Census {
  19. people: microrm::IDMap<Person>,
  20. }
  21. #[test]
  22. fn test_insertion() {
  23. let (pool, db): (_, Census) = common::open_test_db!();
  24. let mut lease = pool.acquire().unwrap();
  25. db.people
  26. .insert(
  27. &mut lease,
  28. Person {
  29. name: String::from("name 1"),
  30. state: PersonState::Alive,
  31. },
  32. )
  33. .unwrap();
  34. db.people
  35. .insert(
  36. &mut lease,
  37. Person {
  38. name: String::from("name 2"),
  39. state: PersonState::Dead,
  40. },
  41. )
  42. .unwrap();
  43. }
  44. #[test]
  45. fn test_retrieval() {
  46. let (pool, db): (_, Census) = common::open_test_db!();
  47. let mut lease = pool.acquire().unwrap();
  48. let id = db
  49. .people
  50. .insert(
  51. &mut lease,
  52. Person {
  53. name: String::from("name 1"),
  54. state: PersonState::Alive,
  55. },
  56. )
  57. .unwrap();
  58. assert_eq!(
  59. db.people
  60. .by_id(&mut lease, id)
  61. .ok()
  62. .flatten()
  63. .unwrap()
  64. .state,
  65. PersonState::Alive
  66. );
  67. }
  68. #[test]
  69. fn test_search() {
  70. let (pool, db): (_, Census) = common::open_test_db!();
  71. let mut lease = pool.acquire().unwrap();
  72. db.people
  73. .insert(
  74. &mut lease,
  75. Person {
  76. name: String::from("name 1"),
  77. state: PersonState::Alive,
  78. },
  79. )
  80. .unwrap();
  81. assert_eq!(
  82. db.people
  83. .with(Person::State, PersonState::Undead)
  84. .count(&mut lease)
  85. .unwrap(),
  86. 0
  87. );
  88. assert_eq!(
  89. db.people
  90. .with(Person::State, PersonState::Alive)
  91. .count(&mut lease)
  92. .unwrap(),
  93. 1
  94. );
  95. }