recipes/api/src/domain/recipe/fetch_list.rs

78 lines
2.6 KiB
Rust
Raw Normal View History

2022-05-14 00:45:31 +03:00
use crate::repo::recipe::RecipeRepo;
use super::types;
pub fn execute(repo: &impl RecipeRepo) -> Vec<types::Recipe> {
repo.get_recipes()
}
2022-05-14 16:59:14 +03:00
#[cfg(test)]
mod tests {
use super::*;
use crate::{
domain::{misc_types::Lang, recipe::types::RecipeIngredientMeasure},
repo::recipe::InMemoryRecipeRepo,
};
#[test]
fn should_return_all_recipes() {
let repo = InMemoryRecipeRepo::new();
let res = execute(&repo);
match res.as_slice() {
[salad] => {
2022-05-14 16:59:14 +03:00
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!(),
}
}
2022-05-14 16:59:14 +03:00
#[test]
fn should_not_return_recipes_if_ingredients_not_exist_in_db() {
let mut repo = InMemoryRecipeRepo::new().with_no_ingredients_found();
let res = execute(&repo);
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);
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"));
2022-05-14 16:59:14 +03:00
}
_ => unreachable!(),
}
}
}