2020-01-19 02:00:41 +08:00
|
|
|
use std::convert::Into;
|
2020-01-23 18:30:18 +08:00
|
|
|
use std::time::Duration;
|
2020-01-19 02:00:41 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
use assert_json_diff::assert_json_eq;
|
2020-01-19 02:00:41 +08:00
|
|
|
use async_std::io::prelude::*;
|
2020-01-23 18:30:18 +08:00
|
|
|
use async_std::task::{block_on, sleep};
|
2020-01-19 02:00:41 +08:00
|
|
|
use http_service::Body;
|
|
|
|
use serde_json::json;
|
|
|
|
use serde_json::Value;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2020-01-22 21:30:09 +08:00
|
|
|
// Process:
|
|
|
|
// - Write a full settings update
|
|
|
|
// - Delete all settings
|
|
|
|
// Check:
|
|
|
|
// - Settings are deleted, all fields are null
|
|
|
|
// - POST success repond Status Code 202
|
|
|
|
// - Get success repond Status Code 200
|
|
|
|
// - Delete success repond Status Code 202
|
2020-01-19 02:00:41 +08:00
|
|
|
#[test]
|
2020-01-20 16:52:24 +08:00
|
|
|
fn write_all_and_delete() {
|
2020-01-19 02:00:41 +08:00
|
|
|
let mut server = common::setup_server().unwrap();
|
|
|
|
|
|
|
|
// 1 - Create the index
|
|
|
|
|
|
|
|
let body = json!({
|
|
|
|
"uid": "movies",
|
2020-01-23 18:30:18 +08:00
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes();
|
2020-01-19 02:00:41 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-19 02:00:41 +08:00
|
|
|
let res = server.simulate(req).unwrap();
|
|
|
|
assert_eq!(res.status(), 201);
|
|
|
|
|
|
|
|
// 2 - Send the settings
|
|
|
|
|
|
|
|
let json = json!({
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingRules": [
|
2020-01-19 02:00:41 +08:00
|
|
|
"_typo",
|
|
|
|
"_words",
|
|
|
|
"_proximity",
|
|
|
|
"_attribute",
|
|
|
|
"_words_position",
|
|
|
|
"_exact",
|
|
|
|
"dsc(release_date)",
|
|
|
|
"dsc(rank)",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingDistinct": "movie_id",
|
2020-01-30 01:30:21 +08:00
|
|
|
"identifier": "uid",
|
|
|
|
"searchableAttributes": [
|
2020-01-19 02:00:41 +08:00
|
|
|
"uid",
|
|
|
|
"movie_id",
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"poster",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"displayedAttributes": [
|
2020-01-19 02:00:41 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"poster",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": [
|
2020-01-19 02:00:41 +08:00
|
|
|
"the",
|
|
|
|
"a",
|
|
|
|
"an",
|
|
|
|
],
|
|
|
|
"synonyms": {
|
|
|
|
"wolverine": ["xmen", "logan"],
|
|
|
|
"logan": ["wolverine"],
|
2020-01-28 01:27:42 +08:00
|
|
|
},
|
|
|
|
"indexNewFields": false,
|
2020-01-19 02:00:41 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let body = json.to_string().into_bytes();
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-19 02:00:41 +08:00
|
|
|
let res = server.simulate(req).unwrap();
|
|
|
|
assert_eq!(res.status(), 202);
|
|
|
|
|
|
|
|
block_on(sleep(Duration::from_secs(1)));
|
|
|
|
|
2020-01-19 02:30:25 +08:00
|
|
|
// 3 - Get all settings and compare to the previous one
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::get("/indexes/movies/settings")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
2020-01-19 02:30:25 +08:00
|
|
|
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
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::delete("/indexes/movies/settings")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
2020-01-19 02:30:25 +08:00
|
|
|
let res = server.simulate(req).unwrap();
|
|
|
|
assert_eq!(res.status(), 202);
|
|
|
|
|
2020-01-22 21:30:09 +08:00
|
|
|
block_on(sleep(Duration::from_secs(2)));
|
2020-01-19 02:30:25 +08:00
|
|
|
|
|
|
|
// 5 - Get all settings and check if they are empty
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::get("/indexes/movies/settings")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
2020-01-19 02:00:41 +08:00
|
|
|
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();
|
|
|
|
|
2020-01-19 02:30:25 +08:00
|
|
|
let json = json!({
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingRules": null,
|
|
|
|
"rankingDistinct": null,
|
2020-01-30 01:30:21 +08:00
|
|
|
"identifier": null,
|
|
|
|
"searchableAttributes": null,
|
|
|
|
"displayedAttributes": null,
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": null,
|
2020-01-19 02:30:25 +08:00
|
|
|
"synonyms": null,
|
2020-01-28 01:27:42 +08:00
|
|
|
"indexNewFields": null,
|
2020-01-19 02:30:25 +08:00
|
|
|
});
|
2020-01-19 02:00:41 +08:00
|
|
|
|
|
|
|
assert_json_eq!(json, res_value, ordered: false);
|
|
|
|
}
|
2020-01-20 16:52:24 +08:00
|
|
|
|
2020-01-22 21:30:09 +08:00
|
|
|
// Process:
|
|
|
|
// - Write a full setting update
|
|
|
|
// - Rewrite an other settings confirmation
|
|
|
|
// Check:
|
|
|
|
// - Settings are overwrited
|
|
|
|
// - Forgotten attributes are deleted
|
|
|
|
// - Null attributes are deleted
|
|
|
|
// - Empty attribute are deleted
|
2020-01-20 16:52:24 +08:00
|
|
|
#[test]
|
|
|
|
fn write_all_and_update() {
|
|
|
|
let mut server = common::setup_server().unwrap();
|
|
|
|
|
|
|
|
// 1 - Create the index
|
|
|
|
|
|
|
|
let body = json!({
|
|
|
|
"uid": "movies",
|
2020-01-23 18:30:18 +08:00
|
|
|
})
|
|
|
|
.to_string()
|
|
|
|
.into_bytes();
|
2020-01-20 16:52:24 +08:00
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-20 16:52:24 +08:00
|
|
|
let res = server.simulate(req).unwrap();
|
|
|
|
assert_eq!(res.status(), 201);
|
|
|
|
|
|
|
|
// 2 - Send the settings
|
|
|
|
|
|
|
|
let json = json!({
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingRules": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"_typo",
|
|
|
|
"_words",
|
|
|
|
"_proximity",
|
|
|
|
"_attribute",
|
|
|
|
"_words_position",
|
|
|
|
"_exact",
|
|
|
|
"dsc(release_date)",
|
|
|
|
"dsc(rank)",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingDistinct": "movie_id",
|
2020-01-30 01:30:21 +08:00
|
|
|
"identifier": "uid",
|
|
|
|
"searchableAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"uid",
|
|
|
|
"movie_id",
|
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"poster",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"displayedAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"poster",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"the",
|
|
|
|
"a",
|
|
|
|
"an",
|
|
|
|
],
|
|
|
|
"synonyms": {
|
|
|
|
"wolverine": ["xmen", "logan"],
|
|
|
|
"logan": ["wolverine"],
|
2020-01-28 01:27:42 +08:00
|
|
|
},
|
|
|
|
"indexNewFields": false,
|
2020-01-20 16:52:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let body = json.to_string().into_bytes();
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
|
|
|
.body(Body::from(body))
|
|
|
|
.unwrap();
|
2020-01-20 16:52:24 +08:00
|
|
|
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
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::get("/indexes/movies/settings")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
2020-01-20 16:52:24 +08:00
|
|
|
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 19:03:48 +08:00
|
|
|
"rankingRules": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"_typo",
|
|
|
|
"_words",
|
|
|
|
"_proximity",
|
|
|
|
"_attribute",
|
|
|
|
"_words_position",
|
|
|
|
"_exact",
|
|
|
|
"dsc(release_date)",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"identifier": "uid",
|
|
|
|
"searchableAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"uid",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"displayedAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
"poster",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": [
|
2020-01-20 16:52:24 +08:00
|
|
|
],
|
|
|
|
"synonyms": {
|
|
|
|
"wolverine": ["xmen", "logan"],
|
|
|
|
"logan": ["wolverine", "xmen"],
|
2020-01-28 01:27:42 +08:00
|
|
|
},
|
|
|
|
"indexNewFields": false,
|
2020-01-20 16:52:24 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
let body_update = json_update.to_string().into_bytes();
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::post("/indexes/movies/settings")
|
|
|
|
.body(Body::from(body_update))
|
|
|
|
.unwrap();
|
2020-01-20 16:52:24 +08:00
|
|
|
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)
|
|
|
|
|
2020-01-23 18:30:18 +08:00
|
|
|
let req = http::Request::get("/indexes/movies/settings")
|
|
|
|
.body(Body::empty())
|
|
|
|
.unwrap();
|
2020-01-20 16:52:24 +08:00
|
|
|
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 19:03:48 +08:00
|
|
|
"rankingRules": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"_typo",
|
|
|
|
"_words",
|
|
|
|
"_proximity",
|
|
|
|
"_attribute",
|
|
|
|
"_words_position",
|
|
|
|
"_exact",
|
|
|
|
"dsc(release_date)",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"rankingDistinct": null,
|
2020-01-30 01:30:21 +08:00
|
|
|
"identifier": "uid",
|
|
|
|
"searchableAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"uid",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"displayedAttributes": [
|
2020-01-20 16:52:24 +08:00
|
|
|
"title",
|
|
|
|
"description",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
"poster",
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": null,
|
2020-01-20 16:52:24 +08:00
|
|
|
"synonyms": {
|
|
|
|
"wolverine": ["xmen", "logan"],
|
|
|
|
"logan": ["wolverine", "xmen"],
|
2020-01-27 15:52:36 +08:00
|
|
|
},
|
2020-01-28 01:27:42 +08:00
|
|
|
"indexNewFields": false
|
2020-01-20 16:52:24 +08:00
|
|
|
});
|
|
|
|
|
2020-01-21 19:03:48 +08:00
|
|
|
assert_json_eq!(res_expected, res_value, ordered: false);
|
2020-01-20 16:52:24 +08:00
|
|
|
}
|