derive.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. #![allow(unused)]
  2. use microrm::prelude::*;
  3. use microrm::schema::{Database, IDMap};
  4. use test_log::test;
  5. mod common;
  6. #[derive(Entity)]
  7. struct Role {
  8. title: String,
  9. permissions: String,
  10. }
  11. #[derive(Entity)]
  12. struct Person {
  13. #[key]
  14. name: String,
  15. roles: microrm::RelationMap<Role>,
  16. }
  17. #[derive(Database)]
  18. struct PeopleDB {
  19. people: IDMap<Person>,
  20. }
  21. #[test]
  22. fn open_test() {
  23. PeopleDB::open_path(":memory:");
  24. }
  25. #[test]
  26. fn check_for_inserted() {
  27. let db = common::open_test_db::<PeopleDB>("derive_check_for_inserted");
  28. let name_string = "name_here".to_string();
  29. // check that it isn't in the database before we insert it
  30. assert!(db.people.keyed(&name_string).get().ok().flatten().is_none());
  31. db.people
  32. .insert(Person {
  33. name: name_string.clone(),
  34. roles: Default::default(),
  35. })
  36. .expect("failed to insert");
  37. // check that it is in the database after we insert it
  38. assert!(db.people.keyed(&name_string).get().ok().flatten().is_some());
  39. }
  40. #[test]
  41. fn check_relation_query_construction() {
  42. let db = common::open_test_db::<PeopleDB>("derive_check_relation_query_construction");
  43. let name_string = "name_here".to_string();
  44. db.people
  45. .insert(Person {
  46. name: name_string.clone(),
  47. roles: Default::default(),
  48. })
  49. .expect("couldn't insert test person");
  50. let person = db
  51. .people
  52. .keyed(&name_string)
  53. .get()
  54. .ok()
  55. .flatten()
  56. .expect("couldn't re-get test person entity");
  57. person
  58. .roles
  59. .get()
  60. .expect("couldn't get related role entity");
  61. }
  62. #[test]
  63. fn check_relation_insertion() {
  64. let db = common::open_test_db::<PeopleDB>("derive_check_relation_insertion");
  65. let name_string = "name_here".to_string();
  66. db.people
  67. .insert(Person {
  68. name: name_string.clone(),
  69. roles: Default::default(),
  70. })
  71. .expect("couldn't insert test person");
  72. let person = db
  73. .people
  74. .keyed(&name_string)
  75. .get()
  76. .ok()
  77. .flatten()
  78. .expect("couldn't re-get test person entity");
  79. person.roles.insert(Role {
  80. title: "title A".to_string(),
  81. permissions: "permissions A".to_string(),
  82. });
  83. }
  84. #[test]
  85. fn delete_test() {
  86. let db = common::open_test_db::<PeopleDB>("derive_delete_test");
  87. let id = db
  88. .people
  89. .insert(Person {
  90. name: "person_name".to_string(),
  91. roles: Default::default(),
  92. })
  93. .expect("couldn't insert test person");
  94. assert!(db.people.by_id(id).expect("couldn't query db").is_some());
  95. db.people
  96. .with(
  97. <Person as microrm::schema::entity::Entity>::IDPart::default(),
  98. &id,
  99. )
  100. .delete();
  101. assert!(db.people.by_id(id).expect("couldn't query db").is_none());
  102. }
  103. #[test]
  104. fn update_test() {
  105. let db = common::open_test_db::<PeopleDB>("derive_update_test");
  106. let mut stored = db
  107. .people
  108. .insert_and_return(Person {
  109. name: "person_name".to_string(),
  110. roles: Default::default(),
  111. })
  112. .expect("couldn't insert test person");
  113. assert_eq!(
  114. db.people
  115. .with(Person::Name, "person_name")
  116. .count()
  117. .expect("couldn't execute count query"),
  118. 1
  119. );
  120. stored.name = "another_person_name".into();
  121. stored.sync();
  122. assert_eq!(
  123. db.people
  124. .with(Person::Name, "person_name")
  125. .count()
  126. .expect("couldn't execute count query"),
  127. 0
  128. );
  129. }