79 lines
2.7 KiB
Rust
79 lines
2.7 KiB
Rust
use crate::{domain::Context, repo::recipe::RecipeRepo};
|
||
|
||
use super::types;
|
||
|
||
pub fn execute(repo: &impl RecipeRepo, ctx: &Context) -> Vec<types::Recipe> {
|
||
repo.get_recipes(ctx)
|
||
}
|
||
|
||
#[cfg(test)]
|
||
mod tests {
|
||
use super::*;
|
||
use crate::{
|
||
domain::{recipe::types::RecipeIngredientMeasure, Lang},
|
||
repo::recipe::InMemoryRecipeRepo,
|
||
};
|
||
|
||
#[test]
|
||
fn should_return_all_recipes() {
|
||
let repo = InMemoryRecipeRepo::new();
|
||
let res = execute(&repo, &Context::default());
|
||
|
||
match res.as_slice() {
|
||
[salad] => {
|
||
assert_eq!(salad.key, String::from("fruit_salad"));
|
||
assert_eq!(salad.lang, Lang::Rus);
|
||
assert_eq!(salad.name, String::from("Фруктовый салат"));
|
||
assert_eq!(
|
||
salad.instructions,
|
||
vec![
|
||
"Нарезать бананы кружочками",
|
||
"Нарезать яблоки и апельсины кубиками",
|
||
"Все ингредиенты перемешать",
|
||
]
|
||
);
|
||
|
||
match salad.ingredients.as_slice() {
|
||
[banana, apple, orange] => {
|
||
assert_eq!(banana.ingredient.key, "banana");
|
||
assert_eq!(banana.measure, RecipeIngredientMeasure::Gram(150));
|
||
assert_eq!(apple.ingredient.key, "apple");
|
||
assert_eq!(apple.measure, RecipeIngredientMeasure::Gram(150));
|
||
assert_eq!(orange.ingredient.key, "orange");
|
||
assert_eq!(orange.measure, RecipeIngredientMeasure::Gram(150));
|
||
}
|
||
_ => unreachable!(),
|
||
}
|
||
}
|
||
_ => unreachable!(),
|
||
}
|
||
}
|
||
|
||
#[test]
|
||
fn should_not_return_recipes_if_ingredients_not_exist_in_db() {
|
||
let mut repo = InMemoryRecipeRepo::new().with_no_ingredients_found();
|
||
let ctx = Context::default();
|
||
let res = execute(&repo, &ctx);
|
||
|
||
match res.as_slice() {
|
||
[salad] => {
|
||
assert_eq!(salad.key, String::from("fruit_salad"));
|
||
}
|
||
_ => unreachable!(),
|
||
}
|
||
|
||
if let Some(rec) = repo.recipes.get_mut(1) {
|
||
rec.ingredients.pop(); // remove wheat flour from ingredients
|
||
}
|
||
|
||
let res = execute(&repo, &ctx);
|
||
match res.as_slice() {
|
||
[salad, no_found] => {
|
||
assert_eq!(salad.key, String::from("fruit_salad"));
|
||
assert_eq!(no_found.key, String::from("no_ingredients_found"));
|
||
}
|
||
_ => unreachable!(),
|
||
}
|
||
}
|
||
}
|