Przeglądaj źródła

Add tests and minor QoL additions.

Kestrel 1 miesiąc temu
rodzic
commit
e7035ff29a
4 zmienionych plików z 85 dodań i 7 usunięć
  1. 1 1
      microrm/src/lib.rs
  2. 0 6
      microrm/src/query.rs
  3. 17 0
      microrm/src/schema.rs
  4. 67 0
      microrm/tests/query_equiv.rs

+ 1 - 1
microrm/src/lib.rs

@@ -236,7 +236,7 @@ pub mod cli;
 pub mod prelude {
     pub use crate::query::{Insertable, Queryable, RelationInterface};
     pub use crate::schema::{relation::Relation, Database, Serializable};
-    pub use microrm_macros::{Database, Entity};
+    pub use microrm_macros::{Database, Entity, Value};
 }
 
 // ----------------------------------------------------------------------

+ 0 - 6
microrm/src/query.rs

@@ -703,9 +703,3 @@ impl<'a, E: Entity, EPL: EntityPartList<Entity = E>> Queryable for &'a Index<E,
         &self.conn
     }
 }
-
-#[cfg(test)]
-mod query_build_test {
-    #[test]
-    fn simple_construction() {}
-}

+ 17 - 0
microrm/src/schema.rs

@@ -109,6 +109,16 @@ impl<T: Entity> std::ops::DerefMut for Stored<T> {
     }
 }
 
+impl<T: Entity + Clone> Clone for Stored<T> {
+    fn clone(&self) -> Self {
+        Self {
+            db: self.db.clone(),
+            id: self.id,
+            wrap: self.wrap.clone(),
+        }
+    }
+}
+
 impl<T: Entity> PartialEq for Stored<T> {
     fn eq(&self, other: &Self) -> bool {
         self.id == other.id
@@ -125,6 +135,13 @@ pub struct Serialized<T: serde::Serialize + serde::de::DeserializeOwned + Clone>
     wrapped: T,
 }
 
+impl<T: serde::Serialize + serde::de::DeserializeOwned + Clone> Serialized<T> {
+    /// Returns the object stored inside this representational wrapper.
+    pub fn wrapped(self) -> T {
+        self.wrapped
+    }
+}
+
 impl<T: serde::Serialize + serde::de::DeserializeOwned + Default + Clone> Default
     for Serialized<T>
 {

+ 67 - 0
microrm/tests/query_equiv.rs

@@ -19,10 +19,26 @@ struct DoubleItem {
     v: usize,
 }
 
+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Value)]
+enum ExampleEnum {
+    Value1,
+    Value2,
+    Value3,
+}
+
+#[derive(Entity)]
+struct SerializedItem {
+    #[key]
+    discrim: usize,
+    #[key]
+    e: ExampleEnum,
+}
+
 #[derive(Database)]
 struct ItemDB {
     single_items: microrm::IDMap<SingleItem>,
     double_items: microrm::IDMap<DoubleItem>,
+    serialized_items: microrm::IDMap<SerializedItem>,
 }
 
 #[test]
@@ -117,6 +133,57 @@ fn double_item() {
         .is_none());
 }
 
+#[test]
+fn serialized_test() {
+    let db: ItemDB = common::open_test_db!();
+
+    db.serialized_items
+        .insert(SerializedItem {
+            discrim: 0,
+            e: ExampleEnum::Value1.into(),
+        })
+        .expect("couldn't insert test item");
+
+    db.serialized_items
+        .insert(SerializedItem {
+            discrim: 1,
+            e: ExampleEnum::Value1.into(),
+        })
+        .expect("couldn't insert test item");
+
+    db.serialized_items
+        .insert(SerializedItem {
+            discrim: 1,
+            e: ExampleEnum::Value2.into(),
+        })
+        .expect("couldn't insert test item");
+
+    assert!(db
+        .serialized_items
+        .with(SerializedItem::E, ExampleEnum::Value1)
+        .keyed((1, ExampleEnum::Value1))
+        .first()
+        .get()
+        .expect("couldn't query db")
+        .is_some());
+    assert!(db
+        .serialized_items
+        .with(SerializedItem::E, ExampleEnum::Value1)
+        .keyed((1024, ExampleEnum::Value1))
+        .first()
+        .get()
+        .expect("couldn't query db")
+        .is_none());
+    assert!(db
+        .serialized_items
+        .with(SerializedItem::E, ExampleEnum::Value1)
+        .keyed((0, ExampleEnum::Value2))
+        .first()
+        .get()
+        .expect("couldn't query db")
+        .is_none());
+}
+
 #[test]
 fn with_test() {
     let db: ItemDB = common::open_test_db!();