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

70 lines
2.2 KiB
Rust
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

use super::types;
use crate::repo::ingredient::{GetIngredientOpts, IngredientRepo};
#[derive(Default)]
pub struct RequestOpts {
lang: Option<types::Lang>,
}
pub fn execute(repo: &impl IngredientRepo, opts: RequestOpts) -> Vec<types::Ingredient> {
repo.get_ingredients(GetIngredientOpts { lang: opts.lang })
}
#[cfg(test)]
mod tests {
use super::*;
use crate::domain::ingredient::types::Lang;
#[test]
fn should_return_all_ingredients() {
let repo = crate::repo::ingredient::InMemoryIngredientRepo::new();
let res = execute(&repo, RequestOpts::default());
match res.as_slice() {
[apple, orange, salt, sugar] => {
assert_eq!(apple.key, String::from("apple"));
assert_eq!(apple.lang, Lang::Rus);
assert_eq!(apple.name, String::from("Яблоко"));
assert_eq!(orange.key, String::from("orange"));
assert_eq!(orange.lang, Lang::Rus);
assert_eq!(orange.name, String::from("Апельсин"));
assert_eq!(salt.key, String::from("salt"));
assert_eq!(salt.lang, Lang::Rus);
assert_eq!(salt.name, String::from("Соль"));
assert_eq!(sugar.key, String::from("sugar"));
assert_eq!(sugar.lang, Lang::Rus);
assert_eq!(sugar.name, String::from("Сахар"));
}
_ => unimplemented!(),
}
}
#[test]
fn should_return_filtered_by_lang() {
let repo = crate::repo::ingredient::InMemoryIngredientRepo::new();
let res = execute(
&repo,
RequestOpts {
lang: Some(Lang::Eng),
..RequestOpts::default()
},
);
match res.as_slice() {
[apple, salt] => {
assert_eq!(apple.key, String::from("apple"));
assert_eq!(apple.lang, Lang::Eng);
assert_eq!(apple.name, String::from("Apple"));
assert_eq!(salt.key, String::from("salt"));
assert_eq!(salt.lang, Lang::Eng);
assert_eq!(salt.name, String::from("Salt"));
}
_ => unimplemented!(),
}
}
}