123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155 |
- #![allow(unused)]
- use microrm::prelude::*;
- use microrm::schema::{Database, IDMap};
- use test_log::test;
- mod common;
- #[derive(Entity)]
- struct Role {
- title: String,
- permissions: String,
- }
- #[derive(Entity)]
- struct Person {
- #[key]
- name: String,
- roles: microrm::RelationMap<Role>,
- }
- #[derive(Database)]
- struct PeopleDB {
- people: IDMap<Person>,
- }
- #[test]
- fn open_test() {
- PeopleDB::open_path(":memory:");
- }
- #[test]
- fn check_for_inserted() {
- let db = common::open_test_db::<PeopleDB>("derive_check_for_inserted");
- 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().ok().flatten().is_none());
- db.people
- .insert(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().ok().flatten().is_some());
- }
- #[test]
- fn check_relation_query_construction() {
- let db = common::open_test_db::<PeopleDB>("derive_check_relation_query_construction");
- let name_string = "name_here".to_string();
- db.people
- .insert(Person {
- name: name_string.clone(),
- roles: Default::default(),
- })
- .expect("couldn't insert test person");
- let person = db
- .people
- .keyed(&name_string)
- .get()
- .ok()
- .flatten()
- .expect("couldn't re-get test person entity");
- person
- .roles
- .get()
- .expect("couldn't get related role entity");
- }
- #[test]
- fn check_relation_insertion() {
- let db = common::open_test_db::<PeopleDB>("derive_check_relation_insertion");
- let name_string = "name_here".to_string();
- db.people
- .insert(Person {
- name: name_string.clone(),
- roles: Default::default(),
- })
- .expect("couldn't insert test person");
- let person = db
- .people
- .keyed(&name_string)
- .get()
- .ok()
- .flatten()
- .expect("couldn't re-get test person entity");
- person.roles.insert(Role {
- title: "title A".to_string(),
- permissions: "permissions A".to_string(),
- });
- }
- #[test]
- fn delete_test() {
- let db = common::open_test_db::<PeopleDB>("derive_delete_test");
- let id = db
- .people
- .insert(Person {
- name: "person_name".to_string(),
- roles: Default::default(),
- })
- .expect("couldn't insert test person");
- assert!(db.people.by_id(id).expect("couldn't query db").is_some());
- db.people
- .with(
- <Person as microrm::schema::entity::Entity>::IDPart::default(),
- &id,
- )
- .delete();
- assert!(db.people.by_id(id).expect("couldn't query db").is_none());
- }
- #[test]
- fn update_test() {
- let db = common::open_test_db::<PeopleDB>("derive_update_test");
- let mut stored = db
- .people
- .insert_and_return(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()
- .expect("couldn't execute count query"),
- 1
- );
- stored.name = "another_person_name".into();
- stored.sync();
- assert_eq!(
- db.people
- .with(Person::Name, "person_name")
- .count()
- .expect("couldn't execute count query"),
- 0
- );
- }
|