|
@@ -0,0 +1,108 @@
|
|
|
+use microrm::prelude::*;
|
|
|
+use test_log::test;
|
|
|
+
|
|
|
+mod common;
|
|
|
+
|
|
|
+#[derive(PartialEq, Clone, Debug, Value, serde::Serialize, serde::Deserialize)]
|
|
|
+enum PersonState {
|
|
|
+ Unborn,
|
|
|
+ Alive,
|
|
|
+ Dead,
|
|
|
+ Undead,
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Entity)]
|
|
|
+struct Person {
|
|
|
+ #[key]
|
|
|
+ name: String,
|
|
|
+ state: PersonState,
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Default, Schema)]
|
|
|
+struct Census {
|
|
|
+ people: microrm::IDMap<Person>,
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn test_insertion() {
|
|
|
+ let (pool, db): (_, Census) = common::open_test_db!();
|
|
|
+ let mut lease = pool.acquire().unwrap();
|
|
|
+
|
|
|
+ db.people
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ Person {
|
|
|
+ name: String::from("name 1"),
|
|
|
+ state: PersonState::Alive,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ db.people
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ Person {
|
|
|
+ name: String::from("name 2"),
|
|
|
+ state: PersonState::Dead,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .unwrap();
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn test_retrieval() {
|
|
|
+ let (pool, db): (_, Census) = common::open_test_db!();
|
|
|
+ let mut lease = pool.acquire().unwrap();
|
|
|
+
|
|
|
+ let id = db
|
|
|
+ .people
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ Person {
|
|
|
+ name: String::from("name 1"),
|
|
|
+ state: PersonState::Alive,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ db.people
|
|
|
+ .by_id(&mut lease, id)
|
|
|
+ .ok()
|
|
|
+ .flatten()
|
|
|
+ .unwrap()
|
|
|
+ .state,
|
|
|
+ PersonState::Alive
|
|
|
+ );
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn test_search() {
|
|
|
+ let (pool, db): (_, Census) = common::open_test_db!();
|
|
|
+ let mut lease = pool.acquire().unwrap();
|
|
|
+
|
|
|
+ db.people
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ Person {
|
|
|
+ name: String::from("name 1"),
|
|
|
+ state: PersonState::Alive,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .unwrap();
|
|
|
+
|
|
|
+ assert_eq!(
|
|
|
+ db.people
|
|
|
+ .with(Person::State, PersonState::Undead)
|
|
|
+ .count(&mut lease)
|
|
|
+ .unwrap(),
|
|
|
+ 0
|
|
|
+ );
|
|
|
+ assert_eq!(
|
|
|
+ db.people
|
|
|
+ .with(Person::State, PersonState::Alive)
|
|
|
+ .count(&mut lease)
|
|
|
+ .unwrap(),
|
|
|
+ 1
|
|
|
+ );
|
|
|
+}
|