Change the settings tests and macros to avoid oversights

This commit is contained in:
ManyTheFish 2024-11-28 11:34:35 +01:00
parent a2f64f6552
commit 68c4717e21
2 changed files with 274 additions and 275 deletions

View File

@ -17,6 +17,26 @@ use crate::extractors::authentication::GuardedData;
use crate::routes::{get_task_id, is_dry_run, SummarizedTaskView}; use crate::routes::{get_task_id, is_dry_run, SummarizedTaskView};
use crate::Opt; use crate::Opt;
macro_rules! make_setting_routes {
($({$route:literal, $update_verb:ident, $type:ty, $err_ty:ty, $attr:ident, $camelcase_attr:literal, $analytics:ident}),*) => {
$(
make_setting_route!($route, $update_verb, $type, $err_ty, $attr, $camelcase_attr, $analytics);
)*
pub fn configure(cfg: &mut web::ServiceConfig) {
use crate::extractors::sequential_extractor::SeqHandler;
cfg.service(
web::resource("")
.route(web::patch().to(SeqHandler(update_all)))
.route(web::get().to(SeqHandler(get_all)))
.route(web::delete().to(SeqHandler(delete_all))))
$(.service($attr::resources()))*;
}
pub const ALL_SETTINGS_NAMES: &[&str] = &[$(stringify!($attr)),*];
};
}
#[macro_export] #[macro_export]
macro_rules! make_setting_route { macro_rules! make_setting_route {
($route:literal, $update_verb:ident, $type:ty, $err_ty:ty, $attr:ident, $camelcase_attr:literal, $analytics:ident) => { ($route:literal, $update_verb:ident, $type:ty, $err_ty:ty, $attr:ident, $camelcase_attr:literal, $analytics:ident) => {
@ -153,7 +173,8 @@ macro_rules! make_setting_route {
}; };
} }
make_setting_route!( make_setting_routes!(
{
"/filterable-attributes", "/filterable-attributes",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -163,9 +184,8 @@ make_setting_route!(
filterable_attributes, filterable_attributes,
"filterableAttributes", "filterableAttributes",
FilterableAttributesAnalytics FilterableAttributesAnalytics
); },
{
make_setting_route!(
"/sortable-attributes", "/sortable-attributes",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -175,9 +195,8 @@ make_setting_route!(
sortable_attributes, sortable_attributes,
"sortableAttributes", "sortableAttributes",
SortableAttributesAnalytics SortableAttributesAnalytics
); },
{
make_setting_route!(
"/displayed-attributes", "/displayed-attributes",
put, put,
Vec<String>, Vec<String>,
@ -187,9 +206,8 @@ make_setting_route!(
displayed_attributes, displayed_attributes,
"displayedAttributes", "displayedAttributes",
DisplayedAttributesAnalytics DisplayedAttributesAnalytics
); },
{
make_setting_route!(
"/typo-tolerance", "/typo-tolerance",
patch, patch,
meilisearch_types::settings::TypoSettings, meilisearch_types::settings::TypoSettings,
@ -199,9 +217,8 @@ make_setting_route!(
typo_tolerance, typo_tolerance,
"typoTolerance", "typoTolerance",
TypoToleranceAnalytics TypoToleranceAnalytics
); },
{
make_setting_route!(
"/searchable-attributes", "/searchable-attributes",
put, put,
Vec<String>, Vec<String>,
@ -211,9 +228,8 @@ make_setting_route!(
searchable_attributes, searchable_attributes,
"searchableAttributes", "searchableAttributes",
SearchableAttributesAnalytics SearchableAttributesAnalytics
); },
{
make_setting_route!(
"/stop-words", "/stop-words",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -223,9 +239,8 @@ make_setting_route!(
stop_words, stop_words,
"stopWords", "stopWords",
StopWordsAnalytics StopWordsAnalytics
); },
{
make_setting_route!(
"/non-separator-tokens", "/non-separator-tokens",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -235,9 +250,8 @@ make_setting_route!(
non_separator_tokens, non_separator_tokens,
"nonSeparatorTokens", "nonSeparatorTokens",
NonSeparatorTokensAnalytics NonSeparatorTokensAnalytics
); },
{
make_setting_route!(
"/separator-tokens", "/separator-tokens",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -247,9 +261,8 @@ make_setting_route!(
separator_tokens, separator_tokens,
"separatorTokens", "separatorTokens",
SeparatorTokensAnalytics SeparatorTokensAnalytics
); },
{
make_setting_route!(
"/dictionary", "/dictionary",
put, put,
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
@ -259,9 +272,8 @@ make_setting_route!(
dictionary, dictionary,
"dictionary", "dictionary",
DictionaryAnalytics DictionaryAnalytics
); },
{
make_setting_route!(
"/synonyms", "/synonyms",
put, put,
std::collections::BTreeMap<String, Vec<String>>, std::collections::BTreeMap<String, Vec<String>>,
@ -271,9 +283,8 @@ make_setting_route!(
synonyms, synonyms,
"synonyms", "synonyms",
SynonymsAnalytics SynonymsAnalytics
); },
{
make_setting_route!(
"/distinct-attribute", "/distinct-attribute",
put, put,
String, String,
@ -283,9 +294,8 @@ make_setting_route!(
distinct_attribute, distinct_attribute,
"distinctAttribute", "distinctAttribute",
DistinctAttributeAnalytics DistinctAttributeAnalytics
); },
{
make_setting_route!(
"/proximity-precision", "/proximity-precision",
put, put,
meilisearch_types::settings::ProximityPrecisionView, meilisearch_types::settings::ProximityPrecisionView,
@ -295,9 +305,8 @@ make_setting_route!(
proximity_precision, proximity_precision,
"proximityPrecision", "proximityPrecision",
ProximityPrecisionAnalytics ProximityPrecisionAnalytics
); },
{
make_setting_route!(
"/localized-attributes", "/localized-attributes",
put, put,
Vec<meilisearch_types::locales::LocalizedAttributesRuleView>, Vec<meilisearch_types::locales::LocalizedAttributesRuleView>,
@ -307,9 +316,8 @@ make_setting_route!(
localized_attributes, localized_attributes,
"localizedAttributes", "localizedAttributes",
LocalesAnalytics LocalesAnalytics
); },
{
make_setting_route!(
"/ranking-rules", "/ranking-rules",
put, put,
Vec<meilisearch_types::settings::RankingRuleView>, Vec<meilisearch_types::settings::RankingRuleView>,
@ -319,9 +327,8 @@ make_setting_route!(
ranking_rules, ranking_rules,
"rankingRules", "rankingRules",
RankingRulesAnalytics RankingRulesAnalytics
); },
{
make_setting_route!(
"/faceting", "/faceting",
patch, patch,
meilisearch_types::settings::FacetingSettings, meilisearch_types::settings::FacetingSettings,
@ -331,9 +338,8 @@ make_setting_route!(
faceting, faceting,
"faceting", "faceting",
FacetingAnalytics FacetingAnalytics
); },
{
make_setting_route!(
"/pagination", "/pagination",
patch, patch,
meilisearch_types::settings::PaginationSettings, meilisearch_types::settings::PaginationSettings,
@ -343,9 +349,8 @@ make_setting_route!(
pagination, pagination,
"pagination", "pagination",
PaginationAnalytics PaginationAnalytics
); },
{
make_setting_route!(
"/embedders", "/embedders",
patch, patch,
std::collections::BTreeMap<String, Setting<meilisearch_types::milli::vector::settings::EmbeddingSettings>>, std::collections::BTreeMap<String, Setting<meilisearch_types::milli::vector::settings::EmbeddingSettings>>,
@ -355,9 +360,8 @@ make_setting_route!(
embedders, embedders,
"embedders", "embedders",
EmbeddersAnalytics EmbeddersAnalytics
); },
{
make_setting_route!(
"/search-cutoff-ms", "/search-cutoff-ms",
put, put,
u64, u64,
@ -367,9 +371,8 @@ make_setting_route!(
search_cutoff_ms, search_cutoff_ms,
"searchCutoffMs", "searchCutoffMs",
SearchCutoffMsAnalytics SearchCutoffMsAnalytics
); },
{
make_setting_route!(
"/facet-search", "/facet-search",
put, put,
bool, bool,
@ -379,9 +382,8 @@ make_setting_route!(
facet_search, facet_search,
"facetSearch", "facetSearch",
FacetSearchAnalytics FacetSearchAnalytics
); },
{
make_setting_route!(
"/prefix-search", "/prefix-search",
put, put,
meilisearch_types::settings::PrefixSearchSettings, meilisearch_types::settings::PrefixSearchSettings,
@ -391,41 +393,7 @@ make_setting_route!(
prefix_search, prefix_search,
"prefixSearch", "prefixSearch",
PrefixSearchAnalytics PrefixSearchAnalytics
);
macro_rules! generate_configure {
($($mod:ident),*) => {
pub fn configure(cfg: &mut web::ServiceConfig) {
use crate::extractors::sequential_extractor::SeqHandler;
cfg.service(
web::resource("")
.route(web::patch().to(SeqHandler(update_all)))
.route(web::get().to(SeqHandler(get_all)))
.route(web::delete().to(SeqHandler(delete_all))))
$(.service($mod::resources()))*;
} }
};
}
generate_configure!(
filterable_attributes,
sortable_attributes,
displayed_attributes,
localized_attributes,
searchable_attributes,
distinct_attribute,
proximity_precision,
stop_words,
separator_tokens,
non_separator_tokens,
dictionary,
synonyms,
ranking_rules,
typo_tolerance,
pagination,
faceting,
embedders,
search_cutoff_ms
); );
pub async fn update_all( pub async fn update_all(

View File

@ -37,6 +37,23 @@ static DEFAULT_SETTINGS_VALUES: Lazy<HashMap<&'static str, Value>> = Lazy::new(|
}), }),
); );
map.insert("search_cutoff_ms", json!(null)); map.insert("search_cutoff_ms", json!(null));
map.insert("embedders", json!(null));
map.insert("facet_search", json!(true));
map.insert("prefix_search", json!("indexingTime"));
map.insert("proximity_precision", json!("byWord"));
map.insert("sortable_attributes", json!([]));
map.insert(
"typo_tolerance",
json!({
"enabled": true,
"minWordSizeForTypos": {
"oneTypo": 5,
"twoTypos": 9
},
"disableOnWords": [],
"disableOnAttributes": []
}),
);
map map
}); });
@ -343,7 +360,7 @@ async fn error_update_setting_unexisting_index_invalid_uid() {
} }
macro_rules! test_setting_routes { macro_rules! test_setting_routes {
($($setting:ident $write_method:ident), *) => { ($($setting:ident $write_method:ident,) *) => {
$( $(
mod $setting { mod $setting {
use crate::common::Server; use crate::common::Server;
@ -409,6 +426,14 @@ macro_rules! test_setting_routes {
} }
} }
)* )*
#[actix_rt::test]
async fn all_setting_tested() {
let expected = std::collections::BTreeSet::from_iter(meilisearch::routes::indexes::settings::ALL_SETTINGS_NAMES.iter());
let tested = std::collections::BTreeSet::from_iter([$(stringify!($setting)),*].iter());
let diff: Vec<_> = expected.difference(&tested).collect();
assert!(diff.is_empty(), "Not all settings were tested, please add the following settings to the `test_setting_routes!` macro: {:?}", diff);
}
}; };
} }
@ -426,7 +451,13 @@ test_setting_routes!(
synonyms put, synonyms put,
pagination patch, pagination patch,
faceting patch, faceting patch,
search_cutoff_ms put search_cutoff_ms put,
embedders patch,
facet_search put,
prefix_search put,
proximity_precision put,
sortable_attributes put,
typo_tolerance patch,
); );
#[actix_rt::test] #[actix_rt::test]