2020-01-29 00:45:57 +08:00
|
|
|
use assert_json_diff::assert_json_eq;
|
|
|
|
use serde_json::json;
|
|
|
|
|
|
|
|
mod common;
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn write_all_and_delete() {
|
2020-05-30 23:12:23 +08:00
|
|
|
let mut server = common::Server::test_server().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 2 - Send the settings
|
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
let body = json!([
|
2020-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
2020-02-27 21:31:08 +08:00
|
|
|
"wordsPosition",
|
2020-02-27 01:47:03 +08:00
|
|
|
"exactness",
|
2020-05-30 23:12:23 +08:00
|
|
|
"desc(registered)",
|
|
|
|
"desc(age)",
|
2020-02-10 23:51:17 +08:00
|
|
|
]);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.update_ranking_rules(body.clone()).await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 3 - Get all settings and compare to the previous one
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (response, _status_code) = server.get_ranking_rules().await;
|
2020-02-03 05:59:19 +08:00
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
assert_json_eq!(body, response, ordered: false);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 4 - Delete all settings
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.delete_ranking_rules().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 5 - Get all settings and check if they are empty
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (response, _status_code) = server.get_ranking_rules().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-03-05 22:18:19 +08:00
|
|
|
let expected = json!([
|
2020-03-02 21:34:29 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
|
|
|
"wordsPosition",
|
|
|
|
"exactness"
|
2020-02-27 01:47:03 +08:00
|
|
|
]);
|
2020-02-03 05:59:19 +08:00
|
|
|
|
2020-03-05 22:18:19 +08:00
|
|
|
assert_json_eq!(expected, response, ordered: false);
|
2020-01-29 00:45:57 +08:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn write_all_and_update() {
|
2020-05-30 23:12:23 +08:00
|
|
|
let mut server = common::Server::test_server().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 2 - Send the settings
|
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
let body = json!([
|
2020-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
2020-02-27 21:31:08 +08:00
|
|
|
"wordsPosition",
|
2020-02-27 01:47:03 +08:00
|
|
|
"exactness",
|
2020-05-30 23:12:23 +08:00
|
|
|
"desc(registered)",
|
|
|
|
"desc(age)",
|
2020-02-10 23:51:17 +08:00
|
|
|
]);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.update_ranking_rules(body.clone()).await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 3 - Get all settings and compare to the previous one
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (response, _status_code) = server.get_ranking_rules().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
assert_json_eq!(body, response, ordered: false);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 4 - Update all settings
|
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
let body = json!([
|
2020-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
2020-02-27 21:31:08 +08:00
|
|
|
"wordsPosition",
|
2020-02-27 01:47:03 +08:00
|
|
|
"exactness",
|
2020-05-30 23:12:23 +08:00
|
|
|
"desc(registered)",
|
2020-02-10 23:51:17 +08:00
|
|
|
]);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.update_ranking_rules(body).await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
|
|
|
// 5 - Get all settings and check if the content is the same of (4)
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (response, _status_code) = server.get_ranking_rules().await;
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
let expected = json!([
|
2020-02-27 01:47:03 +08:00
|
|
|
"typo",
|
|
|
|
"words",
|
|
|
|
"proximity",
|
|
|
|
"attribute",
|
2020-02-27 21:31:08 +08:00
|
|
|
"wordsPosition",
|
2020-02-27 01:47:03 +08:00
|
|
|
"exactness",
|
2020-05-30 23:12:23 +08:00
|
|
|
"desc(registered)",
|
2020-02-10 23:51:17 +08:00
|
|
|
]);
|
2020-01-29 00:45:57 +08:00
|
|
|
|
2020-03-04 21:18:07 +08:00
|
|
|
assert_json_eq!(expected, response, ordered: false);
|
2020-01-29 00:45:57 +08:00
|
|
|
}
|
2020-03-05 22:18:19 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn send_undefined_rule() {
|
2020-05-30 23:12:23 +08:00
|
|
|
let mut server = common::Server::with_uid("test");
|
2020-03-05 22:18:19 +08:00
|
|
|
let body = json!({
|
2020-05-30 23:12:23 +08:00
|
|
|
"uid": "test",
|
2020-03-10 01:40:49 +08:00
|
|
|
"primaryKey": "id",
|
2020-03-05 22:18:19 +08:00
|
|
|
});
|
2020-04-16 17:09:47 +08:00
|
|
|
server.create_index(body).await;
|
2020-03-05 22:18:19 +08:00
|
|
|
|
2020-03-10 18:29:56 +08:00
|
|
|
let body = json!(["typos",]);
|
2020-03-05 22:18:19 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (_response, status_code) = server.update_ranking_rules_sync(body).await;
|
2020-03-05 22:18:19 +08:00
|
|
|
assert_eq!(status_code, 400);
|
|
|
|
}
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn send_malformed_custom_rule() {
|
2020-05-30 23:12:23 +08:00
|
|
|
let mut server = common::Server::with_uid("test");
|
2020-03-05 22:18:19 +08:00
|
|
|
let body = json!({
|
2020-05-30 23:12:23 +08:00
|
|
|
"uid": "test",
|
2020-03-10 01:40:49 +08:00
|
|
|
"primaryKey": "id",
|
2020-03-05 22:18:19 +08:00
|
|
|
});
|
2020-04-16 17:09:47 +08:00
|
|
|
server.create_index(body).await;
|
2020-03-05 22:18:19 +08:00
|
|
|
|
2020-03-10 18:29:56 +08:00
|
|
|
let body = json!(["dsc(truc)",]);
|
2020-03-05 22:18:19 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (_response, status_code) = server.update_ranking_rules_sync(body).await;
|
2020-03-05 22:18:19 +08:00
|
|
|
assert_eq!(status_code, 400);
|
|
|
|
}
|
2020-03-16 19:17:51 +08:00
|
|
|
|
|
|
|
// Test issue https://github.com/meilisearch/MeiliSearch/issues/521
|
2020-04-16 17:09:47 +08:00
|
|
|
#[actix_rt::test]
|
|
|
|
async fn write_custom_ranking_and_index_documents() {
|
2020-05-30 23:12:23 +08:00
|
|
|
let mut server = common::Server::with_uid("test");
|
2020-03-16 19:17:51 +08:00
|
|
|
let body = json!({
|
2020-05-30 23:12:23 +08:00
|
|
|
"uid": "test",
|
2020-03-16 19:17:51 +08:00
|
|
|
"primaryKey": "id",
|
|
|
|
});
|
2020-04-16 17:09:47 +08:00
|
|
|
server.create_index(body).await;
|
2020-03-16 19:17:51 +08:00
|
|
|
|
|
|
|
// 1 - Add ranking rules with one custom ranking on a string
|
|
|
|
|
2020-05-30 23:12:23 +08:00
|
|
|
let body = json!(["asc(name)", "typo"]);
|
2020-03-16 19:17:51 +08:00
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.update_ranking_rules(body).await;
|
2020-03-16 19:17:51 +08:00
|
|
|
|
|
|
|
// 2 - Add documents
|
|
|
|
|
|
|
|
let body = json!([
|
|
|
|
{
|
|
|
|
"id": 1,
|
2020-05-30 23:12:23 +08:00
|
|
|
"name": "Cherry Orr",
|
|
|
|
"color": "green"
|
2020-03-16 19:17:51 +08:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"id": 2,
|
2020-05-30 23:12:23 +08:00
|
|
|
"name": "Lucas Hess",
|
|
|
|
"color": "yellow"
|
2020-03-16 19:17:51 +08:00
|
|
|
}
|
|
|
|
]);
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
server.add_or_replace_multiple_documents(body).await;
|
2020-03-16 19:17:51 +08:00
|
|
|
|
|
|
|
// 3 - Get the first document and compare
|
|
|
|
|
|
|
|
let expected = json!({
|
|
|
|
"id": 1,
|
2020-07-10 17:37:01 +08:00
|
|
|
"name": "Cherry Orr",
|
2020-05-30 23:12:23 +08:00
|
|
|
"color": "green"
|
2020-03-16 19:17:51 +08:00
|
|
|
});
|
|
|
|
|
2020-04-16 17:09:47 +08:00
|
|
|
let (response, status_code) = server.get_document(1).await;
|
2020-03-16 19:17:51 +08:00
|
|
|
assert_eq!(status_code, 200);
|
|
|
|
|
|
|
|
assert_json_eq!(response, expected, ordered: false);
|
|
|
|
}
|