123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- use microrm::prelude::*;
- use microrm_macros::index_cols;
- use test_log::test;
- use microrm::schema::index::Index;
- mod common;
- #[derive(Entity)]
- struct TwoWay {
- up: String,
- left: String,
- right: String,
- }
- #[derive(Default, Schema)]
- struct TWDB {
- entries: microrm::IDMap<TwoWay>,
- left_index: Index<TwoWay, index_cols![TwoWay::left]>,
- right_index: Index<TwoWay, index_cols![TwoWay::right]>,
- }
- #[test]
- fn db_construction() {
- let (_, _db): (_, TWDB) = common::open_test_db!();
- }
- #[test]
- fn lookup() {
- let (pool, db): (_, TWDB) = common::open_test_db!();
- let mut lease = pool.acquire().unwrap();
- let e1 = db
- .entries
- .insert_and_return(
- &mut lease,
- TwoWay {
- up: "up1".into(),
- left: "left1".into(),
- right: "right1".into(),
- },
- )
- .expect("couldn't insert entry");
- let e2 = db
- .entries
- .insert_and_return(
- &mut lease,
- TwoWay {
- up: "up2".into(),
- left: "left2".into(),
- right: "right2".into(),
- },
- )
- .expect("couldn't insert entry");
- assert!(db
- .entries
- .indexed(&db.left_index, "left3")
- .get(&mut lease)
- .expect("couldn't query left index")
- .is_none());
- assert_eq!(
- db.entries
- .indexed(&db.left_index, "left2")
- .get(&mut lease)
- .expect("couldn't query left index")
- .as_ref(),
- Some(&e2)
- );
- assert_eq!(
- db.entries
- .indexed(&db.left_index, "left1")
- .get(&mut lease)
- .expect("couldn't query left index")
- .as_ref(),
- Some(&e1)
- );
- assert!(db
- .entries
- .indexed(&db.right_index, "right3")
- .get(&mut lease)
- .expect("couldn't query right index")
- .is_none());
- assert_eq!(
- db.entries
- .indexed(&db.right_index, "right2")
- .get(&mut lease)
- .expect("couldn't query right index")
- .as_ref(),
- Some(&e2)
- );
- assert_eq!(
- db.entries
- .indexed(&db.right_index, "right1")
- .get(&mut lease)
- .expect("couldn't query right index")
- .as_ref(),
- Some(&e1)
- );
- }
|