238: Fix settings subroutes get r=MarinPostma a=MarinPostma

Fix #225 

Co-authored-by: marin postma <postma.marin@protonmail.com>
This commit is contained in:
bors[bot] 2021-06-23 15:45:50 +00:00 committed by GitHub
commit b676b10cfe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 17 deletions

View File

@ -7,7 +7,7 @@ use crate::{error::ResponseError, index::Unchecked};
#[macro_export] #[macro_export]
macro_rules! make_setting_route { macro_rules! make_setting_route {
($route:literal, $type:ty, $attr:ident) => { ($route:literal, $type:ty, $attr:ident, $camelcase_attr:literal) => {
mod $attr { mod $attr {
use actix_web::{web, HttpResponse}; use actix_web::{web, HttpResponse};
@ -51,7 +51,9 @@ macro_rules! make_setting_route {
index_uid: actix_web::web::Path<String>, index_uid: actix_web::web::Path<String>,
) -> std::result::Result<HttpResponse, ResponseError> { ) -> std::result::Result<HttpResponse, ResponseError> {
let settings = data.settings(index_uid.into_inner()).await?; let settings = data.settings(index_uid.into_inner()).await?;
Ok(HttpResponse::Ok().json(settings.$attr)) let mut json = serde_json::json!(&settings);
let val = json[$camelcase_attr].take();
Ok(HttpResponse::Ok().json(val))
} }
} }
}; };
@ -60,43 +62,50 @@ macro_rules! make_setting_route {
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/filterable-attributes", "/indexes/{index_uid}/settings/filterable-attributes",
std::collections::HashSet<String>, std::collections::HashSet<String>,
filterable_attributes filterable_attributes,
"filterableAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/displayed-attributes", "/indexes/{index_uid}/settings/displayed-attributes",
Vec<String>, Vec<String>,
displayed_attributes displayed_attributes,
"displayedAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/searchable-attributes", "/indexes/{index_uid}/settings/searchable-attributes",
Vec<String>, Vec<String>,
searchable_attributes searchable_attributes,
"searchableAttributes"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/stop-words", "/indexes/{index_uid}/settings/stop-words",
std::collections::BTreeSet<String>, std::collections::BTreeSet<String>,
stop_words stop_words,
"stopWords"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/synonyms", "/indexes/{index_uid}/settings/synonyms",
std::collections::BTreeMap<String, Vec<String>>, std::collections::BTreeMap<String, Vec<String>>,
synonyms synonyms,
"synonyms"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/distinct-attribute", "/indexes/{index_uid}/settings/distinct-attribute",
String, String,
distinct_attribute distinct_attribute,
"distinctAttribute"
); );
make_setting_route!( make_setting_route!(
"/indexes/{index_uid}/settings/ranking-rules", "/indexes/{index_uid}/settings/ranking-rules",
Vec<String>, Vec<String>,
ranking_rules ranking_rules,
"rankingRules"
); );
macro_rules! create_services { macro_rules! create_services {

View File

@ -1,5 +1,21 @@
use std::collections::HashMap;
use once_cell::sync::Lazy;
use serde_json::{Value, json};
use crate::common::Server; use crate::common::Server;
use serde_json::json;
static DEFAULT_SETTINGS_VALUES: Lazy<HashMap<&'static str, Value>> = Lazy::new(|| {
let mut map = HashMap::new();
map.insert("displayed_attributes", json!(["*"]));
map.insert("searchable_attributes", json!(["*"]));
map.insert("filterable_attributes", json!([]));
map.insert("distinct_attribute", json!(Value::Null));
map.insert("ranking_rules", json!(["words", "typo", "proximity", "attribute", "exactness"]));
map.insert("stop_words", json!([]));
map.insert("synonyms", json!({}));
map
});
#[actix_rt::test] #[actix_rt::test]
async fn get_settings_unexisting_index() { async fn get_settings_unexisting_index() {
@ -142,6 +158,7 @@ macro_rules! test_setting_routes {
$( $(
mod $setting { mod $setting {
use crate::common::Server; use crate::common::Server;
use super::DEFAULT_SETTINGS_VALUES;
#[actix_rt::test] #[actix_rt::test]
async fn get_unexisting_index() { async fn get_unexisting_index() {
@ -177,8 +194,25 @@ macro_rules! test_setting_routes {
.chars() .chars()
.map(|c| if c == '_' { '-' } else { c }) .map(|c| if c == '_' { '-' } else { c })
.collect::<String>()); .collect::<String>());
let (_response, code) = server.service.delete(url).await; let (response, code) = server.service.delete(url).await;
assert_eq!(code, 404); assert_eq!(code, 404, "{}", response);
}
#[actix_rt::test]
async fn get_default() {
let server = Server::new().await;
let index = server.index("test");
let (response, code) = index.create(None).await;
assert_eq!(code, 200, "{}", response);
let url = format!("/indexes/test/settings/{}",
stringify!($setting)
.chars()
.map(|c| if c == '_' { '-' } else { c })
.collect::<String>());
let (response, code) = server.service.get(url).await;
assert_eq!(code, 200, "{}", response);
let expected = DEFAULT_SETTINGS_VALUES.get(stringify!($setting)).unwrap();
assert_eq!(expected, &response);
} }
} }
)* )*
@ -189,6 +223,8 @@ test_setting_routes!(
filterable_attributes, filterable_attributes,
displayed_attributes, displayed_attributes,
searchable_attributes, searchable_attributes,
distinct_attribute,
stop_words, stop_words,
ranking_rules,
synonyms synonyms
); );

View File

@ -54,11 +54,8 @@ async fn stats() {
assert_eq!(code, 202, "{}", response); assert_eq!(code, 202, "{}", response);
assert_eq!(response["updateId"], 0); assert_eq!(response["updateId"], 0);
let (response, code) = server.stats().await; let response = index.wait_update_id(0).await;
println!("response: {}", response);
assert_eq!(code, 200, "{}", response);
index.wait_update_id(0).await;
let (response, code) = server.stats().await; let (response, code) = server.stats().await;