MeiliSearch/meilisearch-http/tests/settings.rs

281 lines
7.3 KiB
Rust
Raw Normal View History

2020-01-18 19:00:41 +01:00
use std::time::Duration;
use std::convert::Into;
use async_std::task::{block_on, sleep};
use async_std::io::prelude::*;
use http_service::Body;
use serde_json::json;
use serde_json::Value;
use assert_json_diff::assert_json_eq;
mod common;
#[test]
2020-01-20 09:52:24 +01:00
fn write_all_and_delete() {
2020-01-18 19:00:41 +01:00
let mut server = common::setup_server().unwrap();
// 1 - Create the index
let body = json!({
"uid": "movies",
}).to_string().into_bytes();
let req = http::Request::post("/indexes").body(Body::from(body)).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings
let json = json!({
2020-01-21 12:03:48 +01:00
"rankingRules": [
2020-01-18 19:00:41 +01:00
"_typo",
"_words",
"_proximity",
"_attribute",
"_words_position",
"_exact",
"dsc(release_date)",
"dsc(rank)",
],
2020-01-21 12:03:48 +01:00
"rankingDistinct": "movie_id",
"attributeIdentifier": "uid",
"attributesSearchable": [
2020-01-18 19:00:41 +01:00
"uid",
"movie_id",
"title",
"description",
"poster",
"release_date",
"rank",
],
2020-01-21 12:03:48 +01:00
"attributesDisplayed": [
2020-01-18 19:00:41 +01:00
"title",
"description",
"poster",
"release_date",
"rank",
],
2020-01-21 12:03:48 +01:00
"stopWords": [
2020-01-18 19:00:41 +01:00
"the",
"a",
"an",
],
"synonyms": {
"wolverine": ["xmen", "logan"],
"logan": ["wolverine"],
}
});
let body = json.to_string().into_bytes();
let req = http::Request::post("/indexes/movies/settings").body(Body::from(body)).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Delete all settings
let req = http::Request::delete("/indexes/movies/settings").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 5 - Get all settings and check if they are empty
2020-01-18 19:00:41 +01:00
let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let json = json!({
2020-01-21 12:03:48 +01:00
"rankingRules": null,
"rankingDistinct": null,
"attributeIdentifier": null,
"attributesSearchable": null,
"attributesDisplayed": null,
"stopWords": null,
"synonyms": null,
});
2020-01-18 19:00:41 +01:00
assert_json_eq!(json, res_value, ordered: false);
}
2020-01-20 09:52:24 +01:00
#[test]
fn write_all_and_update() {
let mut server = common::setup_server().unwrap();
// 1 - Create the index
let body = json!({
"uid": "movies",
}).to_string().into_bytes();
let req = http::Request::post("/indexes").body(Body::from(body)).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
// 2 - Send the settings
let json = json!({
2020-01-21 12:03:48 +01:00
"rankingRules": [
2020-01-20 09:52:24 +01:00
"_typo",
"_words",
"_proximity",
"_attribute",
"_words_position",
"_exact",
"dsc(release_date)",
"dsc(rank)",
],
2020-01-21 12:03:48 +01:00
"rankingDistinct": "movie_id",
"attributeIdentifier": "uid",
"attributesSearchable": [
2020-01-20 09:52:24 +01:00
"uid",
"movie_id",
"title",
"description",
"poster",
"release_date",
"rank",
],
2020-01-21 12:03:48 +01:00
"attributesDisplayed": [
2020-01-20 09:52:24 +01:00
"title",
"description",
"poster",
"release_date",
"rank",
],
2020-01-21 12:03:48 +01:00
"stopWords": [
2020-01-20 09:52:24 +01:00
"the",
"a",
"an",
],
"synonyms": {
"wolverine": ["xmen", "logan"],
"logan": ["wolverine"],
}
});
let body = json.to_string().into_bytes();
let req = http::Request::post("/indexes/movies/settings").body(Body::from(body)).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 3 - Get all settings and compare to the previous one
let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(json, res_value, ordered: false);
// 4 - Update all settings
let json_update = json!({
2020-01-21 12:03:48 +01:00
"rankingRules": [
2020-01-20 09:52:24 +01:00
"_typo",
"_words",
"_proximity",
"_attribute",
"_words_position",
"_exact",
"dsc(release_date)",
],
2020-01-21 12:03:48 +01:00
"attributeIdentifier": "uid",
"attributesSearchable": [
2020-01-20 09:52:24 +01:00
"title",
"description",
"uid",
],
2020-01-21 12:03:48 +01:00
"attributesDisplayed": [
2020-01-20 09:52:24 +01:00
"title",
"description",
"release_date",
"rank",
"poster",
],
2020-01-21 12:03:48 +01:00
"stopWords": [
2020-01-20 09:52:24 +01:00
],
"synonyms": {
"wolverine": ["xmen", "logan"],
"logan": ["wolverine", "xmen"],
}
});
let body_update = json_update.to_string().into_bytes();
let req = http::Request::post("/indexes/movies/settings").body(Body::from(body_update)).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(1)));
// 5 - Get all settings and check if the content is the same of (4)
let req = http::Request::get("/indexes/movies/settings").body(Body::empty()).unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 200);
let mut buf = Vec::new();
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let res_value: Value = serde_json::from_slice(&buf).unwrap();
let res_expected = json!({
2020-01-21 12:03:48 +01:00
"rankingRules": [
2020-01-20 09:52:24 +01:00
"_typo",
"_words",
"_proximity",
"_attribute",
"_words_position",
"_exact",
"dsc(release_date)",
],
2020-01-21 12:03:48 +01:00
"rankingDistinct": null,
"attributeIdentifier": "uid",
"attributesSearchable": [
2020-01-20 09:52:24 +01:00
"title",
"description",
"uid",
],
2020-01-21 12:03:48 +01:00
"attributesDisplayed": [
2020-01-20 09:52:24 +01:00
"title",
"description",
"release_date",
"rank",
"poster",
],
2020-01-21 12:03:48 +01:00
"stopWords": null,
2020-01-20 09:52:24 +01:00
"synonyms": {
"wolverine": ["xmen", "logan"],
"logan": ["wolverine", "xmen"],
}
});
2020-01-21 12:03:48 +01:00
assert_json_eq!(res_expected, res_value, ordered: false);
2020-01-20 09:52:24 +01:00
}