Parcourir la source

Add test for Value derivation macro/DatumProxy trait.

Kestrel il y a 1 semaine
Parent
commit
469efbee52
1 fichiers modifiés avec 108 ajouts et 0 suppressions
  1. 108 0
      microrm/tests/value_proxy.rs

+ 108 - 0
microrm/tests/value_proxy.rs

@@ -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
+    );
+}