From a794970b72dda55ef7af2afa2eae13f33f8e07ee Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 1 Jul 2020 15:58:37 +0200 Subject: [PATCH 1/2] additional tests for index --- meilisearch-http/tests/index.rs | 106 ++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index 136369146..0776e6d0b 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -1,4 +1,5 @@ use assert_json_diff::assert_json_eq; +use actix_web::http::StatusCode; use serde_json::json; mod common; @@ -662,3 +663,108 @@ async fn check_add_documents_without_primary_key() { assert_eq!(status_code, 400); } +#[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)); + + let dataset = include_bytes!("assets/movies.json"); + + 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 { + server.create_index(json!({ "uid": format!("test{}", i) })).await; + } + 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"); + let (response, _status) = server.create_index(json!({ "uid": "test", "primaryKey": "test" })).await; + 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] +async fn upate_existing_primary_key_is_error() { + let mut server = common::Server::with_uid("test"); + server.create_index(json!({ "uid": "test", "primaryKey": "key" })).await; + let (response, status) = server.update_index(json!({ "primaryKey": "test2" })).await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert_eq!(response["errorCode"], "bad_request"); + assert_eq!(response["errorType"], "invalid_request_error"); +} From 63260e64436e02f95d4f45767d69a8fbd7e8c388 Mon Sep 17 00:00:00 2001 From: mpostma Date: Wed, 8 Jul 2020 14:31:33 +0200 Subject: [PATCH 2/2] add tests for documents --- meilisearch-http/tests/documents_get.rs | 23 +++++++++++++++++++++ meilisearch-http/tests/index.rs | 27 ++++++++++++++++--------- 2 files changed, 41 insertions(+), 9 deletions(-) create mode 100644 meilisearch-http/tests/documents_get.rs diff --git a/meilisearch-http/tests/documents_get.rs b/meilisearch-http/tests/documents_get.rs new file mode 100644 index 000000000..35e04f494 --- /dev/null +++ b/meilisearch-http/tests/documents_get.rs @@ -0,0 +1,23 @@ +use serde_json::json; +use actix_web::http::StatusCode; + +mod common; + +#[actix_rt::test] +async fn get_documents_from_unexisting_index_is_error() { + let mut server = common::Server::with_uid("test"); + let (response, status) = server.get_all_documents().await; + assert_eq!(status, StatusCode::NOT_FOUND); + assert_eq!(response["errorCode"], "index_not_found"); + assert_eq!(response["errorType"], "invalid_request_error"); + assert_eq!(response["errorLink"], "https://docs.meilisearch.com/errors#index_not_found"); +} + +#[actix_rt::test] +async fn get_empty_documents_list() { + let mut server = common::Server::with_uid("test"); + server.create_index(json!({ "uid": "test" })).await; + let (response, status) = server.get_all_documents().await; + assert_eq!(status, StatusCode::OK); + assert!(response.as_array().unwrap().is_empty()); +} diff --git a/meilisearch-http/tests/index.rs b/meilisearch-http/tests/index.rs index 0776e6d0b..29fbed070 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -1,6 +1,6 @@ -use assert_json_diff::assert_json_eq; use actix_web::http::StatusCode; -use serde_json::json; +use assert_json_diff::assert_json_eq; +use serde_json::{json, Value}; mod common; @@ -72,7 +72,10 @@ async fn create_index_with_uid() { let (response, status_code) = server.create_index(body).await; assert_eq!(status_code, 400); - assert_eq!(response["errorCode"].as_str().unwrap(), "index_already_exists"); + assert_eq!( + response["errorCode"].as_str().unwrap(), + "index_already_exists" + ); // 2 - Check the list of indexes @@ -676,7 +679,7 @@ async fn check_first_update_should_bring_up_processed_status_after_first_docs_ad assert_eq!(status_code, 201); assert_eq!(response["primaryKey"], json!(null)); - let dataset = include_bytes!("assets/movies.json"); + let dataset = include_bytes!("./assets/test_set.json"); let body: Value = serde_json::from_slice(dataset).unwrap(); @@ -702,7 +705,9 @@ async fn get_empty_index() { async fn create_and_list_multiple_indices() { let mut server = common::Server::with_uid("test"); for i in 0..10 { - server.create_index(json!({ "uid": format!("test{}", i) })).await; + server + .create_index(json!({ "uid": format!("test{}", i) })) + .await; } let (response, _status) = server.list_indexes().await; assert_eq!(response.as_array().unwrap().len(), 10); @@ -746,7 +751,9 @@ async fn correct_response_no_primary_key_index() { #[actix_rt::test] async fn correct_response_with_primary_key_index() { let mut server = common::Server::with_uid("test"); - let (response, _status) = server.create_index(json!({ "uid": "test", "primaryKey": "test" })).await; + let (response, _status) = server + .create_index(json!({ "uid": "test", "primaryKey": "test" })) + .await; assert_eq!(response["primaryKey"], "test"); } @@ -760,11 +767,13 @@ async fn udpate_unexisting_index_is_error() { } #[actix_rt::test] -async fn upate_existing_primary_key_is_error() { +async fn update_existing_primary_key_is_error() { let mut server = common::Server::with_uid("test"); - server.create_index(json!({ "uid": "test", "primaryKey": "key" })).await; + server + .create_index(json!({ "uid": "test", "primaryKey": "key" })) + .await; let (response, status) = server.update_index(json!({ "primaryKey": "test2" })).await; assert_eq!(status, StatusCode::BAD_REQUEST); - assert_eq!(response["errorCode"], "bad_request"); + assert_eq!(response["errorCode"], "primary_key_already_present"); assert_eq!(response["errorType"], "invalid_request_error"); }