2022-05-09 17:52:22 +03:00
|
|
|
|
use super::types;
|
2022-05-14 00:00:42 +03:00
|
|
|
|
use crate::domain::misc_types;
|
2022-05-12 00:16:25 +03:00
|
|
|
|
use crate::repo::ingredient::IngredientRepo;
|
2022-05-09 17:52:22 +03:00
|
|
|
|
|
2022-05-12 00:01:34 +03:00
|
|
|
|
#[derive(Default, Debug)]
|
2022-05-10 00:56:08 +03:00
|
|
|
|
pub struct RequestOpts {
|
2022-05-14 00:00:42 +03:00
|
|
|
|
pub lang: Option<misc_types::Lang>,
|
2022-05-12 00:16:25 +03:00
|
|
|
|
pub keys: Option<Vec<String>>,
|
2022-05-10 00:56:08 +03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn execute(repo: &impl IngredientRepo, opts: RequestOpts) -> Vec<types::Ingredient> {
|
2022-05-12 00:16:25 +03:00
|
|
|
|
repo.get_ingredients(opts.into())
|
2022-05-09 17:52:22 +03:00
|
|
|
|
}
|
2022-05-10 00:09:47 +03:00
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2022-05-14 00:00:42 +03:00
|
|
|
|
use crate::domain::misc_types::Lang;
|
2022-05-10 00:09:47 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn should_return_all_ingredients() {
|
|
|
|
|
let repo = crate::repo::ingredient::InMemoryIngredientRepo::new();
|
2022-05-10 00:56:08 +03:00
|
|
|
|
let res = execute(&repo, RequestOpts::default());
|
2022-05-10 00:09:47 +03:00
|
|
|
|
|
|
|
|
|
match res.as_slice() {
|
2022-05-10 00:56:08 +03:00
|
|
|
|
[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]
|
2022-05-12 21:59:09 +03:00
|
|
|
|
fn should_return_preferred_lang_with_fallback() {
|
2022-05-10 00:56:08 +03:00
|
|
|
|
let repo = crate::repo::ingredient::InMemoryIngredientRepo::new();
|
|
|
|
|
let res = execute(
|
|
|
|
|
&repo,
|
|
|
|
|
RequestOpts {
|
|
|
|
|
lang: Some(Lang::Eng),
|
|
|
|
|
..RequestOpts::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match res.as_slice() {
|
2022-05-12 21:59:09 +03:00
|
|
|
|
[apple, orange, salt, sugar] => {
|
2022-05-10 00:56:08 +03:00
|
|
|
|
assert_eq!(apple.key, String::from("apple"));
|
|
|
|
|
assert_eq!(apple.lang, Lang::Eng);
|
|
|
|
|
assert_eq!(apple.name, String::from("Apple"));
|
|
|
|
|
|
2022-05-12 21:59:09 +03:00
|
|
|
|
assert_eq!(orange.key, String::from("orange"));
|
|
|
|
|
assert_eq!(orange.lang, Lang::Rus);
|
|
|
|
|
assert_eq!(orange.name, String::from("Апельсин"));
|
|
|
|
|
|
2022-05-10 00:56:08 +03:00
|
|
|
|
assert_eq!(salt.key, String::from("salt"));
|
|
|
|
|
assert_eq!(salt.lang, Lang::Eng);
|
|
|
|
|
assert_eq!(salt.name, String::from("Salt"));
|
2022-05-12 21:59:09 +03:00
|
|
|
|
|
|
|
|
|
assert_eq!(sugar.key, String::from("sugar"));
|
|
|
|
|
assert_eq!(sugar.lang, Lang::Rus);
|
|
|
|
|
assert_eq!(sugar.name, String::from("Сахар"));
|
2022-05-10 00:09:47 +03:00
|
|
|
|
}
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-12 00:16:25 +03:00
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
|
fn should_return_filtered_by_keys() {
|
|
|
|
|
let repo = crate::repo::ingredient::InMemoryIngredientRepo::new();
|
|
|
|
|
let res = execute(
|
|
|
|
|
&repo,
|
|
|
|
|
RequestOpts {
|
|
|
|
|
keys: Some(vec![String::from("apple")]),
|
|
|
|
|
..RequestOpts::default()
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
match res.as_slice() {
|
|
|
|
|
[apple] => {
|
|
|
|
|
assert_eq!(apple.key, String::from("apple"));
|
|
|
|
|
assert_eq!(apple.lang, Lang::Rus);
|
|
|
|
|
assert_eq!(apple.name, String::from("Яблоко"));
|
|
|
|
|
}
|
|
|
|
|
_ => unimplemented!(),
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-10 00:09:47 +03:00
|
|
|
|
}
|