Skip to content

Commit

Permalink
Add test for recipe command, revise search
Browse files Browse the repository at this point in the history
Revised `impl From<SearchParams> for Option<Expr>` to combine
fields such that IDs are considered distinctly with OR
alongside other fields.
  • Loading branch information
sgreenbury committed Aug 27, 2024
1 parent 8bd2f5c commit 61cbee6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,17 @@ mod tests {

use super::*;

#[tokio::test]
async fn test_recipe_command() {
let recipe_command = RecipeCommand {
recipe_file: format!("{}/test_recipe.json", env!("CARGO_MANIFEST_DIR")),
output_format: OutputFormat::GeoJSON,
output_file: None,
};
let result = recipe_command.run(Config::default()).await;
assert!(result.is_ok())
}

#[test]
fn test_parse_year_range() {
assert_eq!(
Expand Down
18 changes: 16 additions & 2 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,13 @@ fn _to_optqueries_then_or<T: Into<Option<Expr>>>(queries: Vec<T>) -> Option<Expr

impl From<SearchParams> for Option<Expr> {
fn from(value: SearchParams) -> Self {
// Non-ID SearchParams handled first with AND between fields and OR within fields
let mut subexprs: Vec<Option<Expr>> = value
.text
.into_iter()
.map(|text| Some(text.into()))
.collect();
subexprs.extend([to_queries_then_or(value.metric_id)]);

if let Some(year_range) = value.year_range {
subexprs.extend([to_queries_then_or(year_range)]);
}
Expand All @@ -336,7 +337,20 @@ impl From<SearchParams> for Option<Expr> {
subexprs.extend(other_subexprs);
// Remove the Nones and unwrap the Somes
let valid_subexprs: Vec<Expr> = subexprs.into_iter().flatten().collect();
combine_exprs_with_and(valid_subexprs)

// Combine non-IDs with AND
let combined_non_id_expr = combine_exprs_with_and(valid_subexprs);

// Combine IDs provided in SearchParams with OR
let combined_id_expr = to_queries_then_or(value.metric_id);

// Combine ID and non-ID SearchParams with OR
combine_exprs_with_or(
vec![combined_non_id_expr, combined_id_expr]
.into_iter()
.flatten()
.collect::<Vec<_>>(),
)
}
}

Expand Down

0 comments on commit 61cbee6

Please sign in to comment.