Browse Source

Fix bug with reading Entity instances with bools.

Kestrel 2 years ago
parent
commit
bf811c7b47
3 changed files with 46 additions and 10 deletions
  1. 1 1
      Cargo.lock
  2. 43 7
      microrm/src/lib.rs
  3. 2 2
      microrm/src/model/modelable.rs

+ 1 - 1
Cargo.lock

@@ -566,7 +566,7 @@ dependencies = [
 
 [[package]]
 name = "microrm"
-version = "0.3.1"
+version = "0.3.2"
 dependencies = [
  "async-std",
  "base64",

+ 43 - 7
microrm/src/lib.rs

@@ -444,14 +444,50 @@ mod delete_test {
     }
 }
 
-/*
 #[cfg(test)]
-mod query_macro_test {
+mod datatypes {
+    use crate::prelude::*;
+
+    #[derive(crate::Entity,serde::Serialize,serde::Deserialize,PartialEq,Debug)]
+    #[microrm_internal]
+    pub struct ValueStore {
+        pub b: bool,
+        pub i_8: i8,
+        pub u_8: u8,
+        pub i_16: i16,
+        pub u_16: u16,
+        pub i_32: i32,
+        pub u_32: u32,
+        pub i_64: i64,
+        pub u_64: u64,
+        pub s: String,
+        pub f_64: f64,
+    }
+
+
     #[test]
-    fn simple_select() {
-        microrm_macros::query!{
-            SELECT A,B,C FROM Something WHERE ID = ?
-        }
+    fn store_load_datatypes() {
+        let schema = crate::Schema::new().entity::<ValueStore>();
+        let db = crate::DB::new_in_memory(schema).unwrap();
+
+        let test_values = ValueStore {
+            b: false,
+            i_8: 42i8,
+            u_8: 142u8,
+            i_16: 320i16,
+            u_16: 20000u16,
+            i_32: 1i32 << 20,
+            u_32: 3u32 << 30,
+            i_64: 1i64 << 40,
+            u_64: 3u64 << 62,
+            s: "this is a test".to_string(),
+            f_64: 23.140692632779263f64
+        };
+
+        let id = db.query_interface().add(&test_values).expect("failed to add ValueStore");
+
+        let all = db.query_interface().get().by_id(&id).all().expect("failed to get by id");
+        assert_eq!(all.len(), 1);
+        assert_eq!(all[0].as_ref(), &test_values);
     }
 }
-*/

+ 2 - 2
microrm/src/model/modelable.rs

@@ -60,11 +60,11 @@ impl Modelable for bool {
         let val = if self == &true { 1i64 } else { 0i64 };
         val.bind(stmt, col)
     }
-    fn build_from(_stmt: &sqlite::Statement, _col_offset: usize) -> sqlite::Result<(Self, usize)>
+    fn build_from(stmt: &sqlite::Statement, col_offset: usize) -> sqlite::Result<(Self, usize)>
     where
         Self: Sized,
     {
-        unreachable!("sqlite only gives Strings back, not &strs!");
+        stmt.read(col_offset).map(|x: i64| (x != 0,1))
     }
     fn column_type() -> &'static str
     where