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