#![allow(unused)] use microrm::prelude::*; use microrm::schema::{IDMap, Schema}; use test_log::test; mod common; #[derive(Entity)] struct Role { title: String, permissions: String, } #[derive(Entity)] struct Person { #[key] name: String, roles: microrm::RelationMap, } #[derive(Default, Schema)] struct PeopleDB { people: IDMap, } #[test] fn open_test() { let (_pool, _db): (_, PeopleDB) = common::open_test_db!(); } #[test] fn check_for_inserted() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let name_string = "name_here".to_string(); // check that it isn't in the database before we insert it assert!(db .people .keyed(&name_string) .get(&mut lease) .ok() .flatten() .is_none()); db.people .insert( &mut lease, Person { name: name_string.clone(), roles: Default::default(), }, ) .expect("failed to insert"); // check that it is in the database after we insert it assert!(db .people .keyed(&name_string) .get(&mut lease) .ok() .flatten() .is_some()); } #[test] fn check_relation_query_construction() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let name_string = "name_here".to_string(); db.people .insert( &mut lease, Person { name: name_string.clone(), roles: Default::default(), }, ) .expect("couldn't insert test person"); let person = db .people .keyed(&name_string) .get(&mut lease) .ok() .flatten() .expect("couldn't re-get test person entity"); person .roles .get(&mut lease) .expect("couldn't get related role entity"); } #[test] fn check_relation_insertion() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let name_string = "name_here".to_string(); db.people .insert( &mut lease, Person { name: name_string.clone(), roles: Default::default(), }, ) .expect("couldn't insert test person"); let person = db .people .keyed(&name_string) .get(&mut lease) .ok() .flatten() .expect("couldn't re-get test person entity"); person.roles.insert( &mut lease, Role { title: "title A".to_string(), permissions: "permissions A".to_string(), }, ); } #[test] fn delete_test() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let id = db .people .insert( &mut lease, Person { name: "person_name".to_string(), roles: Default::default(), }, ) .expect("couldn't insert test person"); assert!(db .people .by_id(&mut lease, id) .expect("couldn't query db") .is_some()); db.people .with( ::IDPart::default(), &id, ) .delete(&mut lease); assert!(db .people .by_id(&mut lease, id) .expect("couldn't query db") .is_none()); } #[test] fn remove_test() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let id = db .people .insert( &mut lease, Person { name: "person_name".to_string(), roles: Default::default(), }, ) .expect("couldn't insert test person"); assert!(db .people .by_id(&mut lease, id) .expect("couldn't query db") .is_some()); let removed = db .people .with( ::IDPart::default(), &id, ) .remove(&mut lease) .expect("couldn't execute removal query"); assert_eq!(removed.len(), 1); assert!(db .people .by_id(&mut lease, id) .expect("couldn't query db") .is_none()); } #[test] fn update_test() { let (pool, db): (_, PeopleDB) = common::open_test_db!(); let mut lease = pool.acquire().unwrap(); let mut stored = db .people .insert_and_return( &mut lease, Person { name: "person_name".to_string(), roles: Default::default(), }, ) .expect("couldn't insert test person"); assert_eq!( db.people .with(Person::Name, "person_name") .count(&mut lease) .expect("couldn't execute count query"), 1 ); stored.name = "another_person_name".into(); stored.sync(&mut lease); assert_eq!( db.people .with(Person::Name, "person_name") .count(&mut lease) .expect("couldn't execute count query"), 0 ); }