use microrm::prelude::*;
use test_log::test;
mod common;
#[derive(PartialEq, Clone, Copy, Debug, Value, serde::Serialize, serde::Deserialize)]
enum PersonState {
Unborn,
Alive,
Dead,
Undead,
}
#[derive(Entity)]
struct Person {
#[key]
name: String,
state: PersonState,
}
#[derive(Schema)]
struct Census {
people: microrm::IDMap<Person>,
}
#[test]
fn test_insertion() {
let (pool, db): (_, Census) = common::open_test_db!();
let mut txn = pool.start().unwrap();
db.people
.insert(
&mut txn,
Person {
name: String::from("name 1"),
state: PersonState::Alive,
},
)
.unwrap();
db.people
.insert(
&mut txn,
Person {
name: String::from("name 2"),
state: PersonState::Dead,
},
)
.unwrap();
}
#[test]
fn test_retrieval() {
let (pool, db): (_, Census) = common::open_test_db!();
let mut txn = pool.start().unwrap();
let id = db
.people
.insert(
&mut txn,
Person {
name: String::from("name 1"),
state: PersonState::Alive,
},
)
.unwrap();
assert_eq!(
db.people.by_id(&mut txn, id).ok().flatten().unwrap().state,
PersonState::Alive
);
}
#[test]
fn test_search() {
let (pool, db): (_, Census) = common::open_test_db!();
let mut txn = pool.start().unwrap();
db.people
.insert(
&mut txn,
Person {
name: String::from("name 1"),
state: PersonState::Alive,
},
)
.unwrap();
assert_eq!(
db.people
.with(Person::State, PersonState::Undead)
.count(&mut txn)
.unwrap(),
0
);
assert_eq!(
db.people
.with(Person::State, PersonState::Alive)
.count(&mut txn)
.unwrap(),
1
);
}