meilisearch/meilisearch-http/tests/index.rs

810 lines
27 KiB
Rust
Raw Permalink Normal View History

2020-07-01 21:58:37 +08:00
use actix_web::http::StatusCode;
2020-07-08 20:31:33 +08:00
use assert_json_diff::assert_json_eq;
use serde_json::{json, Value};
2020-01-17 00:44:34 +08:00
mod common;
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_with_name() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"name": "movies",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
assert_eq!(r1_uid.len(), 8);
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 2 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value[0]["name"].as_str().unwrap();
let r2_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, r1_name);
assert_eq!(r2_uid.len(), r1_uid.len());
assert_eq!(r2_created_at.len(), r1_created_at.len());
assert_eq!(r2_updated_at.len(), r1_updated_at.len());
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_with_uid() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"uid": "movies",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-06-01 17:06:57 +08:00
let (res1_value, status_code) = server.create_index(body.clone()).await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
assert_eq!(r1_uid, "movies");
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
2020-06-01 17:06:57 +08:00
// 1.5 verify that error is thrown when trying to create the same index
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 400);
2020-07-08 20:31:33 +08:00
assert_eq!(
response["errorCode"].as_str().unwrap(),
"index_already_exists"
);
2020-06-01 17:06:57 +08:00
2020-01-17 00:44:34 +08:00
// 2 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value[0]["name"].as_str().unwrap();
let r2_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, r1_name);
assert_eq!(r2_uid, r1_uid);
assert_eq!(r2_created_at.len(), r1_created_at.len());
assert_eq!(r2_updated_at.len(), r1_updated_at.len());
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_with_name_and_uid() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"name": "Films",
"uid": "fr_movies",
2020-03-04 21:18:07 +08:00
});
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "Films");
assert_eq!(r1_uid, "fr_movies");
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 2 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value[0]["name"].as_str().unwrap();
let r2_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, r1_name);
assert_eq!(r2_uid, r1_uid);
assert_eq!(r2_created_at.len(), r1_created_at.len());
assert_eq!(r2_updated_at.len(), r1_updated_at.len());
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn rename_index() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"name": "movies",
2020-03-04 21:18:07 +08:00
"uid": "movies",
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
2020-03-04 21:18:07 +08:00
assert_eq!(r1_uid.len(), 6);
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 2 - Update an index name
let body = json!({
"name": "TV Shows",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.update_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
assert_eq!(res2_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value["name"].as_str().unwrap();
let r2_uid = res2_value["uid"].as_str().unwrap();
let r2_created_at = res2_value["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, "TV Shows");
assert_eq!(r2_uid, r1_uid);
assert_eq!(r2_created_at, r1_created_at);
2020-02-13 17:25:37 +08:00
assert!(r2_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 3 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res3_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res3_value.as_array().unwrap().len(), 1);
assert_eq!(res3_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r3_name = res3_value[0]["name"].as_str().unwrap();
let r3_uid = res3_value[0]["uid"].as_str().unwrap();
let r3_created_at = res3_value[0]["createdAt"].as_str().unwrap();
let r3_updated_at = res3_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r3_name, r2_name);
assert_eq!(r3_uid.len(), r1_uid.len());
assert_eq!(r3_created_at.len(), r1_created_at.len());
assert_eq!(r3_updated_at.len(), r2_updated_at.len());
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn delete_index_and_recreate_it() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
2020-06-05 17:40:18 +08:00
// 0 - delete unexisting index is error
2020-06-30 23:37:13 +08:00
2020-06-05 17:40:18 +08:00
let (response, status_code) = server.delete_request("/indexes/test").await;
assert_eq!(status_code, 404);
assert_eq!(&response["errorCode"], "index_not_found");
2020-06-30 23:37:13 +08:00
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"name": "movies",
2020-03-04 21:18:07 +08:00
"uid": "movies",
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
2020-03-04 21:18:07 +08:00
assert_eq!(r1_uid.len(), 6);
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 2 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value[0]["name"].as_str().unwrap();
let r2_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, r1_name);
assert_eq!(r2_uid.len(), r1_uid.len());
assert_eq!(r2_created_at.len(), r1_created_at.len());
assert_eq!(r2_updated_at.len(), r1_updated_at.len());
// 3- Delete an index
2020-04-16 17:09:47 +08:00
let (_res2_value, status_code) = server.delete_index().await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 204);
2020-01-17 00:44:34 +08:00
// 4 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 0);
// 5 - Create a new index
let body = json!({
"name": "movies",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
assert_eq!(r1_uid.len(), 8);
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 6 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-03-04 21:18:07 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_name = res2_value[0]["name"].as_str().unwrap();
let r2_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_name, r1_name);
assert_eq!(r2_uid.len(), r1_uid.len());
assert_eq!(r2_created_at.len(), r1_created_at.len());
assert_eq!(r2_updated_at.len(), r1_updated_at.len());
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn check_multiples_indexes() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 1 - Create a new index
let body = json!({
"name": "movies",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res1_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res1_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r1_name = res1_value["name"].as_str().unwrap();
let r1_uid = res1_value["uid"].as_str().unwrap();
let r1_created_at = res1_value["createdAt"].as_str().unwrap();
let r1_updated_at = res1_value["updatedAt"].as_str().unwrap();
assert_eq!(r1_name, "movies");
assert_eq!(r1_uid.len(), 8);
2020-02-13 17:25:37 +08:00
assert!(r1_created_at.len() > 1);
assert!(r1_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 2 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res2_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res2_value.as_array().unwrap().len(), 1);
assert_eq!(res2_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r2_0_name = res2_value[0]["name"].as_str().unwrap();
let r2_0_uid = res2_value[0]["uid"].as_str().unwrap();
let r2_0_created_at = res2_value[0]["createdAt"].as_str().unwrap();
let r2_0_updated_at = res2_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(r2_0_name, r1_name);
assert_eq!(r2_0_uid.len(), r1_uid.len());
assert_eq!(r2_0_created_at.len(), r1_created_at.len());
assert_eq!(r2_0_updated_at.len(), r1_updated_at.len());
// 3 - Create a new index
let body = json!({
"name": "films",
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res3_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 201);
assert_eq!(res3_value.as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r3_name = res3_value["name"].as_str().unwrap();
let r3_uid = res3_value["uid"].as_str().unwrap();
let r3_created_at = res3_value["createdAt"].as_str().unwrap();
let r3_updated_at = res3_value["updatedAt"].as_str().unwrap();
assert_eq!(r3_name, "films");
assert_eq!(r3_uid.len(), 8);
2020-02-13 17:25:37 +08:00
assert!(r3_created_at.len() > 1);
assert!(r3_updated_at.len() > 1);
2020-01-17 00:44:34 +08:00
// 4 - Check the list of indexes
2020-04-16 17:09:47 +08:00
let (res4_value, status_code) = server.list_indexes().await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 200);
2020-01-17 00:44:34 +08:00
assert_eq!(res4_value.as_array().unwrap().len(), 2);
assert_eq!(res4_value[0].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r4_0_name = res4_value[0]["name"].as_str().unwrap();
let r4_0_uid = res4_value[0]["uid"].as_str().unwrap();
let r4_0_created_at = res4_value[0]["createdAt"].as_str().unwrap();
let r4_0_updated_at = res4_value[0]["updatedAt"].as_str().unwrap();
assert_eq!(res4_value[1].as_object().unwrap().len(), 5);
2020-01-17 00:44:34 +08:00
let r4_1_name = res4_value[1]["name"].as_str().unwrap();
let r4_1_uid = res4_value[1]["uid"].as_str().unwrap();
let r4_1_created_at = res4_value[1]["createdAt"].as_str().unwrap();
let r4_1_updated_at = res4_value[1]["updatedAt"].as_str().unwrap();
if r4_0_name == r1_name {
assert_eq!(r4_0_name, r1_name);
assert_eq!(r4_0_uid.len(), r1_uid.len());
assert_eq!(r4_0_created_at.len(), r1_created_at.len());
assert_eq!(r4_0_updated_at.len(), r1_updated_at.len());
} else {
assert_eq!(r4_0_name, r3_name);
assert_eq!(r4_0_uid.len(), r3_uid.len());
assert_eq!(r4_0_created_at.len(), r3_created_at.len());
assert_eq!(r4_0_updated_at.len(), r3_updated_at.len());
}
if r4_1_name == r1_name {
assert_eq!(r4_1_name, r1_name);
assert_eq!(r4_1_uid.len(), r1_uid.len());
assert_eq!(r4_1_created_at.len(), r1_created_at.len());
assert_eq!(r4_1_updated_at.len(), r1_updated_at.len());
} else {
assert_eq!(r4_1_name, r3_name);
assert_eq!(r4_1_uid.len(), r3_uid.len());
assert_eq!(r4_1_created_at.len(), r3_created_at.len());
assert_eq!(r4_1_updated_at.len(), r3_updated_at.len());
}
}
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_failed() {
2020-03-04 21:18:07 +08:00
let mut server = common::Server::with_uid("movies");
2020-01-17 00:44:34 +08:00
// 2 - Push index creation with empty json body
2020-03-04 21:18:07 +08:00
let body = json!({});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (res_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 400);
2020-01-17 00:44:34 +08:00
let message = res_value["message"].as_str().unwrap();
2020-05-23 00:04:23 +08:00
assert_eq!(res_value.as_object().unwrap().len(), 4);
2020-01-17 00:44:34 +08:00
assert_eq!(message, "Index creation must have an uid");
// 3 - Create a index with extra data
let body = json!({
"name": "movies",
"active": true
2020-03-04 21:18:07 +08:00
});
2020-01-17 00:44:34 +08:00
2020-04-16 17:09:47 +08:00
let (_res_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 400);
2020-01-17 00:44:34 +08:00
// 3 - Create a index with wrong data type
let body = json!({
"name": "movies",
"uid": 0
2020-03-04 21:18:07 +08:00
});
2020-04-16 17:09:47 +08:00
let (_res_value, status_code) = server.create_index(body).await;
2020-01-17 00:44:34 +08:00
assert_eq!(status_code, 400);
2020-01-17 00:44:34 +08:00
}
2020-03-04 22:58:36 +08:00
2020-03-05 18:44:30 +08:00
// Resolve issue https://github.com/meilisearch/MeiliSearch/issues/492
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_with_primary_key_and_index() {
2020-03-04 22:58:36 +08:00
let mut server = common::Server::with_uid("movies");
// 1 - Create the index
2020-03-04 22:58:36 +08:00
let body = json!({
"uid": "movies",
"primaryKey": "id",
2020-03-04 22:58:36 +08:00
});
2020-04-16 17:09:47 +08:00
let (_response, status_code) = server.create_index(body).await;
2020-03-04 22:58:36 +08:00
assert_eq!(status_code, 201);
// 2 - Add content
2020-03-04 22:58:36 +08:00
let body = json!([{
"id": 123,
"text": "The mask"
}]);
2020-04-16 17:09:47 +08:00
server.add_or_replace_multiple_documents(body.clone()).await;
2020-03-04 22:58:36 +08:00
// 3 - Retreive document
2020-04-16 17:09:47 +08:00
let (response, _status_code) = server.get_document(123).await;
2020-03-04 22:58:36 +08:00
let expect = json!({
"id": 123,
"text": "The mask"
});
assert_json_eq!(response, expect, ordered: false);
}
2020-03-05 18:44:30 +08:00
// Resolve issue https://github.com/meilisearch/MeiliSearch/issues/497
// Test when the given index uid is not valid
// Should have a 400 status code
// Should have the right error message
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_with_invalid_uid() {
2020-03-05 18:44:30 +08:00
let mut server = common::Server::with_uid("");
// 1 - Create the index with invalid uid
2020-03-05 18:44:30 +08:00
let body = json!({
"uid": "the movies"
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
2020-03-05 18:44:30 +08:00
assert_eq!(status_code, 400);
2020-03-05 18:44:30 +08:00
let message = response["message"].as_str().unwrap();
2020-05-23 00:04:23 +08:00
assert_eq!(response.as_object().unwrap().len(), 4);
2020-03-05 18:44:30 +08:00
assert_eq!(message, "Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).");
// 2 - Create the index with invalid uid
2020-03-05 18:44:30 +08:00
let body = json!({
"uid": "%$#"
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
2020-03-05 18:44:30 +08:00
assert_eq!(status_code, 400);
2020-03-05 18:44:30 +08:00
let message = response["message"].as_str().unwrap();
2020-05-23 00:04:23 +08:00
assert_eq!(response.as_object().unwrap().len(), 4);
2020-03-05 18:44:30 +08:00
assert_eq!(message, "Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).");
// 3 - Create the index with invalid uid
2020-03-05 18:44:30 +08:00
let body = json!({
"uid": "the~movies"
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
2020-03-05 18:44:30 +08:00
assert_eq!(status_code, 400);
2020-03-05 18:44:30 +08:00
let message = response["message"].as_str().unwrap();
2020-05-23 00:04:23 +08:00
assert_eq!(response.as_object().unwrap().len(), 4);
2020-03-05 18:44:30 +08:00
assert_eq!(message, "Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).");
// 4 - Create the index with invalid uid
2020-03-05 18:44:30 +08:00
let body = json!({
"uid": "🎉"
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
2020-03-05 18:44:30 +08:00
assert_eq!(status_code, 400);
2020-03-05 18:44:30 +08:00
let message = response["message"].as_str().unwrap();
2020-05-23 00:04:23 +08:00
assert_eq!(response.as_object().unwrap().len(), 4);
2020-03-05 18:44:30 +08:00
assert_eq!(message, "Index must have a valid uid; Index uid can be of type integer or string only composed of alphanumeric characters, hyphens (-) and underscores (_).");
}
// Test that it's possible to add primary_key if it's not already set on index creation
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_and_add_indentifier_after() {
let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key
let body = json!({
"uid": "movies",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
// 2 - Update the index and add an primary_key.
let body = json!({
"primaryKey": "id",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.update_index(body).await;
assert_eq!(status_code, 200);
eprintln!("response: {:#?}", response);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
// 3 - Get index to verify if the primary_key is good
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
}
// Test that it's impossible to change the primary_key
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_and_update_indentifier_after() {
let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key
let body = json!({
"uid": "movies",
"primaryKey": "id",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
// 2 - Update the index and add an primary_key.
let body = json!({
"primaryKey": "skuid",
});
2020-04-16 17:09:47 +08:00
let (_response, status_code) = server.update_index(body).await;
assert_eq!(status_code, 400);
// 3 - Get index to verify if the primary_key still the first one
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
}
// Test that schema inference work well
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_without_primary_key_and_add_document() {
let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key
let body = json!({
"uid": "movies",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
// 2 - Add a document
let body = json!([{
"id": 123,
"title": "I'm a legend",
}]);
2020-04-16 17:09:47 +08:00
server.add_or_update_multiple_documents(body).await;
// 3 - Get index to verify if the primary_key is good
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.get_index().await;
assert_eq!(status_code, 200);
assert_eq!(response["primaryKey"].as_str().unwrap(), "id");
}
// Test search with no primary_key
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn create_index_without_primary_key_and_search() {
let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key
let body = json!({
"uid": "movies",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
// 2 - Search
let query = "q=captain&limit=3";
2020-06-01 20:54:12 +08:00
let (response, status_code) = server.search_get(&query).await;
assert_eq!(status_code, 200);
assert_eq!(response["hits"].as_array().unwrap().len(), 0);
}
// Test the error message when we push an document update and impossibility to find primary key
// Test issue https://github.com/meilisearch/MeiliSearch/issues/517
2020-04-16 17:09:47 +08:00
#[actix_rt::test]
async fn check_add_documents_without_primary_key() {
let mut server = common::Server::with_uid("movies");
// 1 - Create the index with no primary_key
let body = json!({
"uid": "movies",
});
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
// 2- Add document
let body = json!([{
"title": "Test",
"comment": "comment test"
}]);
2020-04-16 17:09:47 +08:00
let (response, status_code) = server.add_or_replace_multiple_documents_sync(body).await;
2020-05-23 00:04:23 +08:00
assert_eq!(response.as_object().unwrap().len(), 4);
2020-07-06 15:56:10 +08:00
assert_eq!(response["errorCode"], "missing_primary_key");
assert_eq!(status_code, 400);
}
2020-07-01 21:58:37 +08:00
#[actix_rt::test]
async fn check_first_update_should_bring_up_processed_status_after_first_docs_addition() {
let mut server = common::Server::with_uid("movies");
let body = json!({
"uid": "movies",
});
// 1. Create Index
let (response, status_code) = server.create_index(body).await;
assert_eq!(status_code, 201);
assert_eq!(response["primaryKey"], json!(null));
2020-07-08 20:31:33 +08:00
let dataset = include_bytes!("./assets/test_set.json");
2020-07-01 21:58:37 +08:00
let body: Value = serde_json::from_slice(dataset).unwrap();
// 2. Index the documents from movies.json, present inside of assets directory
server.add_or_replace_multiple_documents(body).await;
// 3. Fetch the status of the indexing done above.
let (response, status_code) = server.get_all_updates_status().await;
// 4. Verify the fetch is successful and indexing status is 'processed'
assert_eq!(status_code, 200);
assert_eq!(response[0]["status"], "processed");
}
#[actix_rt::test]
async fn get_empty_index() {
let mut server = common::Server::with_uid("test");
let (response, _status) = server.list_indexes().await;
assert!(response.as_array().unwrap().is_empty());
}
#[actix_rt::test]
async fn create_and_list_multiple_indices() {
let mut server = common::Server::with_uid("test");
for i in 0..10 {
2020-07-08 20:31:33 +08:00
server
.create_index(json!({ "uid": format!("test{}", i) }))
.await;
2020-07-01 21:58:37 +08:00
}
let (response, _status) = server.list_indexes().await;
assert_eq!(response.as_array().unwrap().len(), 10);
}
#[actix_rt::test]
async fn get_unexisting_index_is_error() {
let mut server = common::Server::with_uid("test");
let (response, status) = server.get_index().await;
assert_eq!(status, StatusCode::NOT_FOUND);
assert_eq!(response["errorCode"], "index_not_found");
assert_eq!(response["errorType"], "invalid_request_error");
}
#[actix_rt::test]
async fn create_index_twice_is_error() {
let mut server = common::Server::with_uid("test");
server.create_index(json!({ "uid": "test" })).await;
let (response, status) = server.create_index(json!({ "uid": "test" })).await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(response["errorCode"], "index_already_exists");
assert_eq!(response["errorType"], "invalid_request_error");
}
#[actix_rt::test]
async fn badly_formatted_index_name_is_error() {
let mut server = common::Server::with_uid("$__test");
let (response, status) = server.create_index(json!({ "uid": "$__test" })).await;
assert_eq!(status, StatusCode::BAD_REQUEST);
assert_eq!(response["errorCode"], "invalid_index_uid");
assert_eq!(response["errorType"], "invalid_request_error");
}
#[actix_rt::test]
async fn correct_response_no_primary_key_index() {
let mut server = common::Server::with_uid("test");
let (response, _status) = server.create_index(json!({ "uid": "test" })).await;
assert_eq!(response["primaryKey"], Value::Null);
}
#[actix_rt::test]
async fn correct_response_with_primary_key_index() {
let mut server = common::Server::with_uid("test");
2020-07-08 20:31:33 +08:00
let (response, _status) = server
.create_index(json!({ "uid": "test", "primaryKey": "test" }))
.await;
2020-07-01 21:58:37 +08:00
assert_eq!(response["primaryKey"], "test");
}
#[actix_rt::test]
async fn udpate_unexisting_index_is_error() {
let mut server = common::Server::with_uid("test");
let (response, status) = server.update_index(json!({ "primaryKey": "foobar" })).await;
assert_eq!(status, StatusCode::NOT_FOUND);
assert_eq!(response["errorCode"], "index_not_found");
assert_eq!(response["errorType"], "invalid_request_error");
}
#[actix_rt::test]
2020-07-08 20:31:33 +08:00
async fn update_existing_primary_key_is_error() {
2020-07-01 21:58:37 +08:00
let mut server = common::Server::with_uid("test");
2020-07-08 20:31:33 +08:00
server
.create_index(json!({ "uid": "test", "primaryKey": "key" }))
.await;
2020-07-01 21:58:37 +08:00
let (response, status) = server.update_index(json!({ "primaryKey": "test2" })).await;
assert_eq!(status, StatusCode::BAD_REQUEST);
2020-07-08 20:31:33 +08:00
assert_eq!(response["errorCode"], "primary_key_already_present");
2020-07-01 21:58:37 +08:00
assert_eq!(response["errorType"], "invalid_request_error");
}
2020-10-10 10:56:44 +08:00
#[actix_rt::test]
async fn test_facets_distribution_attribute() {
let mut server = common::Server::test_server().await;
let (response, _status_code) = server.get_index_stats().await;
let expected = json!({
"isIndexing": false,
2020-10-10 14:13:27 +08:00
"numberOfDocuments":77,
"fieldsDistribution":{
"age":77,
"gender":77,
"phone":77,
"name":77,
"registered":77,
"latitude":77,
"email":77,
"tags":77,
"longitude":77,
"color":77,
"address":77,
"balance":77,
"about":77,
"picture":77,
},
2020-10-10 10:56:44 +08:00
});
assert_json_eq!(expected, response, ordered: true);
2020-10-19 23:16:25 +08:00
}