use microrm::prelude::*; use test_log::test; mod common; #[derive(Entity)] struct SingleItem { #[key] s: String, v: Vec, } #[derive(Entity)] struct DoubleItem { #[key] s: String, #[key] t: String, 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(Default, Schema)] struct ItemDB { single_items: microrm::IDMap, double_items: microrm::IDMap, serialized_items: microrm::IDMap, } #[test] fn single_item() { let (pool, db): (_, ItemDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); db.single_items .insert( &mut lease, SingleItem { s: "string 1".into(), v: vec![0u8, 1u8, 2u8], }, ) .expect("couldn't insert test item"); db.single_items .insert( &mut lease, SingleItem { s: "string 2".into(), v: vec![0u8, 1u8, 2u8], }, ) .expect("couldn't insert test item"); assert!(db .single_items .keyed("string 2") .get(&mut lease) .expect("couldn't query db") .is_some()); assert!(db .single_items .keyed("string 3") .get(&mut lease) .expect("couldn't query db") .is_none()); assert!(db .single_items .keyed(String::from("string 2")) .get(&mut lease) .expect("couldn't query db") .is_some()); assert!(db .single_items .keyed(String::from("string 3")) .get(&mut lease) .expect("couldn't query db") .is_none()); } #[test] fn double_item() { let (pool, db): (_, ItemDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); db.double_items .insert( &mut lease, DoubleItem { s: "string 1".into(), t: "second string 1".into(), v: 42, }, ) .expect("couldn't insert test item"); db.double_items .insert( &mut lease, DoubleItem { s: "string 2".into(), t: "second string 2".into(), v: 6, }, ) .expect("couldn't insert test item"); assert!(db .double_items .keyed(("string 2", "second string 2")) .get(&mut lease) .expect("couldn't query db") .is_some()); assert!(db .double_items .keyed(("string 3", "second string 3")) .get(&mut lease) .expect("couldn't query db") .is_none()); assert!(db .double_items .keyed((String::from("string 2"), String::from("second string 2"))) .get(&mut lease) .expect("couldn't query db") .is_some()); assert!(db .double_items .keyed((String::from("string 3"), String::from("second string 3"))) .get(&mut lease) .expect("couldn't query db") .is_none()); } #[test] fn serialized_test() { let (pool, db): (_, ItemDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); db.serialized_items .insert( &mut lease, SerializedItem { discrim: 0, e: ExampleEnum::Value1.into(), }, ) .expect("couldn't insert test item"); db.serialized_items .insert( &mut lease, SerializedItem { discrim: 1, e: ExampleEnum::Value1.into(), }, ) .expect("couldn't insert test item"); db.serialized_items .insert( &mut lease, 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(&mut lease) .expect("couldn't query db") .is_some()); assert!(db .serialized_items .with(SerializedItem::E, ExampleEnum::Value1) .keyed((1024, ExampleEnum::Value1)) .first() .get(&mut lease) .expect("couldn't query db") .is_none()); assert!(db .serialized_items .with(SerializedItem::E, ExampleEnum::Value1) .keyed((0, ExampleEnum::Value2)) .first() .get(&mut lease) .expect("couldn't query db") .is_none()); } #[test] fn with_test() { let (pool, db): (_, ItemDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); db.single_items .insert( &mut lease, SingleItem { s: "string 1".into(), v: vec![0u8, 1u8, 2u8], }, ) .expect("couldn't insert test item"); db.single_items .insert( &mut lease, SingleItem { s: "string 2".into(), v: vec![0u8, 1u8, 2u8], }, ) .expect("couldn't insert test item"); assert_eq!( db.single_items .with(SingleItem::V, [0u8].as_slice()) .get(&mut lease) .expect("couldn't query db") .len(), 0 ); assert_eq!( db.single_items .with(SingleItem::V, [0u8, 1, 2].as_slice()) .get(&mut lease) .expect("couldn't query db") .len(), 2 ); }