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 136369146..29fbed070 100644 --- a/meilisearch-http/tests/index.rs +++ b/meilisearch-http/tests/index.rs @@ -1,5 +1,6 @@ +use actix_web::http::StatusCode; use assert_json_diff::assert_json_eq; -use serde_json::json; +use serde_json::{json, Value}; mod common; @@ -71,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 @@ -662,3 +666,114 @@ 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/test_set.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 update_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"], "primary_key_already_present"); + assert_eq!(response["errorType"], "invalid_request_error"); +}