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-02-13 00:00:14 +08:00
|
|
|
"identifier": "id",
|
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-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"words_position",
|
|
|
|
"exactness",
|
2020-01-19 02:00:41 +08:00
|
|
|
"dsc(release_date)",
|
|
|
|
"dsc(rank)",
|
|
|
|
],
|
2020-02-26 23:05:02 +08:00
|
|
|
"distinctAttribute": "movie_id",
|
2020-01-30 01:30:21 +08:00
|
|
|
"searchableAttributes": [
|
2020-02-03 05:59:19 +08:00
|
|
|
"id",
|
2020-01-19 02:00:41 +08:00
|
|
|
"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
|
|
|
},
|
2020-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": 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-02-27 01:47:03 +08:00
|
|
|
"rankingRules": [
|
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"words_position",
|
|
|
|
"exactness"
|
|
|
|
],
|
2020-02-26 23:05:02 +08:00
|
|
|
"distinctAttribute": null,
|
2020-02-27 01:47:03 +08:00
|
|
|
"searchableAttributes": [
|
|
|
|
"id",
|
|
|
|
"release_date",
|
|
|
|
"poster",
|
|
|
|
"description",
|
|
|
|
"title",
|
|
|
|
"movie_id",
|
|
|
|
"rank"
|
|
|
|
],
|
|
|
|
"displayedAttributes": [
|
|
|
|
"movie_id",
|
|
|
|
"description",
|
|
|
|
"poster",
|
|
|
|
"id",
|
|
|
|
"release_date",
|
|
|
|
"rank",
|
|
|
|
"title"
|
|
|
|
],
|
2020-01-21 19:03:48 +08:00
|
|
|
"stopWords": null,
|
2020-01-19 02:30:25 +08:00
|
|
|
"synonyms": null,
|
2020-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": true,
|
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-02-13 00:00:14 +08:00
|
|
|
"identifier": "id",
|
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-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"words_position",
|
|
|
|
"exactness",
|
2020-01-20 16:52:24 +08:00
|
|
|
"dsc(release_date)",
|
|
|
|
"dsc(rank)",
|
|
|
|
],
|
2020-02-26 23:05:02 +08:00
|
|
|
"distinctAttribute": "movie_id",
|
2020-01-30 01:30:21 +08:00
|
|
|
"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
|
|
|
},
|
2020-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": 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-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"words_position",
|
|
|
|
"exactness",
|
2020-01-20 16:52:24 +08:00
|
|
|
"dsc(release_date)",
|
|
|
|
],
|
2020-01-30 01:30:21 +08:00
|
|
|
"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
|
|
|
},
|
2020-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": 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-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"words_position",
|
|
|
|
"exactness",
|
2020-01-20 16:52:24 +08:00
|
|
|
"dsc(release_date)",
|
|
|
|
],
|
2020-02-26 23:05:02 +08:00
|
|
|
"distinctAttribute": null,
|
2020-01-30 01:30:21 +08:00
|
|
|
"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-02-26 23:05:02 +08:00
|
|
|
"acceptNewFields": 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
|
|
|
}
|