Skip to content

Commit

Permalink
query_people_count() implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
ekr-cfa committed Nov 27, 2024
1 parent 31ad03c commit 22e2938
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ pub trait ContextPeopleExt {
fn get_person_id(&self, person_id: usize) -> PersonId;
fn index_property<T: PersonProperty + 'static>(&mut self, property: T);
fn query_people<T: Query>(&self, q: T) -> Vec<PersonId>;
fn query_people_count<T: Query>(&self, q: T) -> usize;
fn match_person<T: Query>(&self, person_id: PersonId, q: T) -> bool;
}

Expand Down Expand Up @@ -863,6 +864,18 @@ impl ContextPeopleExt for Context {
accumulator.people
}

fn query_people_count<T: Query>(&self, q: T) -> usize {
// Special case the situation where nobody exists.
if self.get_data_container(PeoplePlugin).is_none() {
return 0
}

T::setup(self);
let mut accumulator = PeopleAccumulatorCount::default();
self.query_people_internal(&mut accumulator, q.get_query());
accumulator.count
}

fn match_person<T: Query>(&self, person_id: PersonId, q: T) -> bool {
T::setup(self);
// This cannot fail because someone must have been made by now.
Expand Down Expand Up @@ -1490,6 +1503,23 @@ mod test {
assert_eq!(people.len(), 0);
}

#[test]
fn query_people_count() {
let mut context = Context::new();
let _ = context
.add_person((RiskCategoryType, RiskCategory::High))
.unwrap();

assert_eq!(context.query_people_count((RiskCategoryType, RiskCategory::High)), 1);
}

#[test]
fn query_people_count_empty() {
let context = Context::new();

assert_eq!(context.query_people_count((RiskCategoryType, RiskCategory::High)), 0);
}

#[test]
fn query_people_macro_index_first() {
let mut context = Context::new();
Expand Down

0 comments on commit 22e2938

Please sign in to comment.