microrm 0.6.3

Lightweight ORM using sqlite as a backend
Documentation
#![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<Role>,
}

#[derive(Schema)]
struct PeopleDB {
    people: IDMap<Person>,
}

#[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 txn = pool.start().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 txn)
        .ok()
        .flatten()
        .is_none());

    db.people
        .insert(
            &mut txn,
            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 txn)
        .ok()
        .flatten()
        .is_some());
}

#[test]
fn check_relation_query_construction() {
    let (pool, db): (_, PeopleDB) = common::open_test_db!();
    let mut txn = pool.start().unwrap();

    let name_string = "name_here".to_string();
    db.people
        .insert(
            &mut txn,
            Person {
                name: name_string.clone(),
                roles: Default::default(),
            },
        )
        .expect("couldn't insert test person");

    let person = db
        .people
        .keyed(&name_string)
        .get(&mut txn)
        .ok()
        .flatten()
        .expect("couldn't re-get test person entity");

    person
        .roles
        .get(&mut txn)
        .expect("couldn't get related role entity");
}

#[test]
fn check_relation_insertion() {
    let (pool, db): (_, PeopleDB) = common::open_test_db!();
    let mut txn = pool.start().unwrap();

    let name_string = "name_here".to_string();
    db.people
        .insert(
            &mut txn,
            Person {
                name: name_string.clone(),
                roles: Default::default(),
            },
        )
        .expect("couldn't insert test person");

    let person = db
        .people
        .keyed(&name_string)
        .get(&mut txn)
        .ok()
        .flatten()
        .expect("couldn't re-get test person entity");

    person.roles.insert(
        &mut txn,
        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 txn = pool.start().unwrap();

    let id = db
        .people
        .insert(
            &mut txn,
            Person {
                name: "person_name".to_string(),
                roles: Default::default(),
            },
        )
        .expect("couldn't insert test person");
    assert!(db
        .people
        .by_id(&mut txn, id)
        .expect("couldn't query db")
        .is_some());

    db.people
        .with(
            <Person as microrm::schema::entity::Entity>::IDPart::default(),
            &id,
        )
        .delete(&mut txn);

    assert!(db
        .people
        .by_id(&mut txn, id)
        .expect("couldn't query db")
        .is_none());
}

#[test]
fn remove_test() {
    let (pool, db): (_, PeopleDB) = common::open_test_db!();
    let mut txn = pool.start().unwrap();

    let id = db
        .people
        .insert(
            &mut txn,
            Person {
                name: "person_name".to_string(),
                roles: Default::default(),
            },
        )
        .expect("couldn't insert test person");
    assert!(db
        .people
        .by_id(&mut txn, id)
        .expect("couldn't query db")
        .is_some());

    let removed = db
        .people
        .with(
            <Person as microrm::schema::entity::Entity>::IDPart::default(),
            &id,
        )
        .remove(&mut txn)
        .expect("couldn't execute removal query");

    assert_eq!(removed.len(), 1);

    assert!(db
        .people
        .by_id(&mut txn, id)
        .expect("couldn't query db")
        .is_none());
}

#[test]
fn update_test() {
    let (pool, db): (_, PeopleDB) = common::open_test_db!();
    let mut txn = pool.start().unwrap();

    let mut stored = db
        .people
        .insert_and_return(
            &mut txn,
            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 txn)
            .expect("couldn't execute count query"),
        1
    );

    stored.name = "another_person_name".into();
    stored.sync(&mut txn);

    assert_eq!(
        db.people
            .with(Person::Name, "person_name")
            .count(&mut txn)
            .expect("couldn't execute count query"),
        0
    );
}