From a934b0ac6ac8f201b1f7db5e510d1015e5515993 Mon Sep 17 00:00:00 2001 From: Pedro Turik Firmino Date: Tue, 29 Oct 2024 18:49:06 -0300 Subject: [PATCH 1/5] Applies optimizations to some integration tests --- .../tests/documents/get_documents.rs | 91 ++++++++++--------- 1 file changed, 47 insertions(+), 44 deletions(-) diff --git a/crates/meilisearch/tests/documents/get_documents.rs b/crates/meilisearch/tests/documents/get_documents.rs index e77165f8b..88251aad9 100644 --- a/crates/meilisearch/tests/documents/get_documents.rs +++ b/crates/meilisearch/tests/documents/get_documents.rs @@ -11,17 +11,18 @@ use crate::json; // transplant #[actix_rt::test] async fn get_unexisting_index_single_document() { - let server = Server::new().await; - let (_response, code) = server.index("test").get_document(1, None).await; + let server = Server::new_shared(); + let (_response, code) = server.unique_index().get_document(1, None).await; assert_eq!(code, 404); } #[actix_rt::test] async fn error_get_unexisting_document() { - let server = Server::new().await; - let index = server.index("test"); - index.create(None).await; - index.wait_task(0).await; + let server = Server::new_shared(); + let index = server.unique_index(); + let (task, _code) = index.create(None).await; + index.wait_task(task.uid()).await.succeeded(); + let (response, code) = index.get_document(1, None).await; let expected_response = json!({ @@ -37,18 +38,19 @@ async fn error_get_unexisting_document() { #[actix_rt::test] async fn get_document() { - let server = Server::new().await; - let index = server.index("test"); - index.create(None).await; + let server = Server::new_shared(); + let index = server.unique_index(); + let (task, _code) = index.create(None).await; + index.wait_task(task.uid()).await.succeeded(); let documents = json!([ { "id": 0, "nested": { "content": "foobar" }, } ]); - let (_, code) = index.add_documents(documents, None).await; + let (task, code) = index.add_documents(documents, None).await; assert_eq!(code, 202); - index.wait_task(1).await; + index.wait_task(task.uid()).await.succeeded(); let (response, code) = index.get_document(0, None).await; assert_eq!(code, 200); assert_eq!( @@ -81,12 +83,12 @@ async fn get_document() { #[actix_rt::test] async fn error_get_unexisting_index_all_documents() { - let server = Server::new().await; - let (response, code) = - server.index("test").get_all_documents(GetAllDocumentsOptions::default()).await; + let server = Server::new_shared(); + let index = server.unique_index(); + let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; let expected_response = json!({ - "message": "Index `test` not found.", + "message": format!("Index `{}` not found.", index.uid), "code": "index_not_found", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#index_not_found" @@ -98,12 +100,12 @@ async fn error_get_unexisting_index_all_documents() { #[actix_rt::test] async fn get_no_document() { - let server = Server::new().await; - let index = server.index("test"); - let (_, code) = index.create(None).await; + let server = Server::new_shared(); + let index = server.unique_index(); + let (task, code) = index.create(None).await; assert_eq!(code, 202); - index.wait_task(0).await; + index.wait_task(task.uid()).await.succeeded(); let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; assert_eq!(code, 200); @@ -112,8 +114,8 @@ async fn get_no_document() { #[actix_rt::test] async fn get_all_documents_no_options() { - let server = Server::new().await; - let index = server.index("test"); + let server = Server::new_shared(); + let index = server.unique_index(); index.load_test_set().await; let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; @@ -143,14 +145,13 @@ async fn get_all_documents_no_options() { #[actix_rt::test] async fn get_all_documents_no_options_with_response_compression() { - let server = Server::new().await; - let index_uid = "test"; - let index = server.index(index_uid); + let server = Server::new_shared(); + let index = server.unique_index(); index.load_test_set().await; let app = server.init_web_app().await; let req = test::TestRequest::get() - .uri(&format!("/indexes/{}/documents?", urlencode(index_uid))) + .uri(&format!("/indexes/{}/documents?", urlencode(&index.uid))) .insert_header((ACCEPT_ENCODING, "gzip")) .to_request(); @@ -169,8 +170,8 @@ async fn get_all_documents_no_options_with_response_compression() { #[actix_rt::test] async fn test_get_all_documents_limit() { - let server = Server::new().await; - let index = server.index("test"); + let server = Server::new_shared(); + let index = server.unique_index(); index.load_test_set().await; let (response, code) = index @@ -186,8 +187,8 @@ async fn test_get_all_documents_limit() { #[actix_rt::test] async fn test_get_all_documents_offset() { - let server = Server::new().await; - let index = server.index("test"); + let server = Server::new_shared(); + let index = server.unique_index(); index.load_test_set().await; let (response, code) = index @@ -203,8 +204,8 @@ async fn test_get_all_documents_offset() { #[actix_rt::test] async fn test_get_all_documents_attributes_to_retrieve() { - let server = Server::new().await; - let index = server.index("test"); + let server = Server::new_shared(); + let index = server.unique_index(); index.load_test_set().await; let (response, code) = index @@ -286,9 +287,11 @@ async fn test_get_all_documents_attributes_to_retrieve() { #[actix_rt::test] async fn get_document_s_nested_attributes_to_retrieve() { - let server = Server::new().await; - let index = server.index("test"); - index.create(None).await; + let server = Server::new_shared(); + let index = server.unique_index(); + let (task, _code) = index.create(None).await; + index.wait_task(task.uid()).await.succeeded(); + let documents = json!([ { "id": 0, @@ -302,9 +305,9 @@ async fn get_document_s_nested_attributes_to_retrieve() { }, }, ]); - let (_, code) = index.add_documents(documents, None).await; + let (task, code) = index.add_documents(documents, None).await; assert_eq!(code, 202); - index.wait_task(1).await; + index.wait_task(task.uid()).await.succeeded(); let (response, code) = index.get_document(0, Some(json!({ "fields": ["content"] }))).await; assert_eq!(code, 200); @@ -343,8 +346,8 @@ async fn get_document_s_nested_attributes_to_retrieve() { #[actix_rt::test] async fn get_documents_displayed_attributes_is_ignored() { - let server = Server::new().await; - let index = server.index("test"); + let server = Server::new_shared(); + let index = server.unique_index(); index.update_settings(json!({"displayedAttributes": ["gender"]})).await; index.load_test_set().await; @@ -366,10 +369,10 @@ async fn get_documents_displayed_attributes_is_ignored() { #[actix_rt::test] async fn get_document_by_filter() { - let server = Server::new().await; - let index = server.index("doggo"); + let server = Server::new_shared(); + let index = server.unique_index(); index.update_settings_filterable_attributes(json!(["color"])).await; - index + let (task, _code) = index .add_documents( json!([ { "id": 0, "color": "red" }, @@ -380,7 +383,7 @@ async fn get_document_by_filter() { Some("id"), ) .await; - index.wait_task(1).await; + index.wait_task(task.uid()).await.succeeded(); let (response, code) = index.get_document_by_filter(json!({})).await; let (response2, code2) = index.get_all_documents_raw("").await; @@ -552,7 +555,7 @@ async fn get_document_with_vectors() { })) .await; snapshot!(code, @"202 Accepted"); - server.wait_task(response.uid()).await; + server.wait_task(response.uid()).await.succeeded(); let documents = json!([ {"id": 0, "name": "kefir", "_vectors": { "manual": [0, 0, 0] }}, @@ -560,7 +563,7 @@ async fn get_document_with_vectors() { ]); let (value, code) = index.add_documents(documents, None).await; snapshot!(code, @"202 Accepted"); - index.wait_task(value.uid()).await; + index.wait_task(value.uid()).await.succeeded(); // by default you shouldn't see the `_vectors` object let (documents, _code) = index.get_all_documents(Default::default()).await; From c79ca9679b99f8655ab6017995f7f3d52ea7deb0 Mon Sep 17 00:00:00 2001 From: Pedro Turik Firmino Date: Sat, 2 Nov 2024 18:25:33 -0300 Subject: [PATCH 2/5] Changes variable name to re-run CI --- crates/meilisearch/tests/documents/get_documents.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/crates/meilisearch/tests/documents/get_documents.rs b/crates/meilisearch/tests/documents/get_documents.rs index 88251aad9..02b99b016 100644 --- a/crates/meilisearch/tests/documents/get_documents.rs +++ b/crates/meilisearch/tests/documents/get_documents.rs @@ -120,8 +120,8 @@ async fn get_all_documents_no_options() { let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; assert_eq!(code, 200); - let arr = response["results"].as_array().unwrap(); - assert_eq!(arr.len(), 20); + let results = response["results"].as_array().unwrap(); + assert_eq!(results.len(), 20); let first = json!({ "id":0, "isActive":false, @@ -140,7 +140,7 @@ async fn get_all_documents_no_options() { "longitude":-145.725388, "tags":["bug" ,"bug"]}); - assert_eq!(first, arr[0]); + assert_eq!(first, results[0]); } #[actix_rt::test] From d0b1ba20cb4a7d6a2b279f198255edfccba49af7 Mon Sep 17 00:00:00 2001 From: Pedro Turik Firmino Date: Mon, 4 Nov 2024 17:26:50 -0300 Subject: [PATCH 3/5] Improves usage of shared indexes --- crates/meilisearch/tests/common/mod.rs | 20 +++++++++ .../tests/documents/get_documents.rs | 42 ++++++------------- 2 files changed, 33 insertions(+), 29 deletions(-) diff --git a/crates/meilisearch/tests/common/mod.rs b/crates/meilisearch/tests/common/mod.rs index afbd5a2f5..9727d45bd 100644 --- a/crates/meilisearch/tests/common/mod.rs +++ b/crates/meilisearch/tests/common/mod.rs @@ -389,3 +389,23 @@ pub static VECTOR_DOCUMENTS: Lazy = Lazy::new(|| { }, ]) }); + +pub async fn shared_index_with_test_set() -> &'static Index<'static, Shared> { + static INDEX: OnceCell> = OnceCell::const_new(); + INDEX.get_or_init(|| async { + let server = Server::new_shared(); + let index = server._index("SHARED_TEST_SET").to_shared(); + let url = format!("/indexes/{}/documents", urlencoding::encode(index.uid.as_ref())); + let (response, code) = index + .service + .post_str( + url, + include_str!("../assets/test_set.json"), + vec![("content-type", "application/json")], + ) + .await; + assert_eq!(code, 202); + index.wait_task(response.uid()).await; + index + }).await +} \ No newline at end of file diff --git a/crates/meilisearch/tests/documents/get_documents.rs b/crates/meilisearch/tests/documents/get_documents.rs index 02b99b016..8aaeb10be 100644 --- a/crates/meilisearch/tests/documents/get_documents.rs +++ b/crates/meilisearch/tests/documents/get_documents.rs @@ -4,15 +4,14 @@ use meili_snap::*; use urlencoding::encode as urlencode; use crate::common::encoder::Encoder; -use crate::common::{GetAllDocumentsOptions, Server, Value}; +use crate::common::{shared_does_not_exists_index, shared_empty_index, shared_index_with_test_set, GetAllDocumentsOptions, Server, Value}; use crate::json; // TODO: partial test since we are testing error, amd error is not yet fully implemented in // transplant #[actix_rt::test] async fn get_unexisting_index_single_document() { - let server = Server::new_shared(); - let (_response, code) = server.unique_index().get_document(1, None).await; + let (_response, code) = shared_does_not_exists_index().await.get_document(1, None).await; assert_eq!(code, 404); } @@ -83,12 +82,11 @@ async fn get_document() { #[actix_rt::test] async fn error_get_unexisting_index_all_documents() { - let server = Server::new_shared(); - let index = server.unique_index(); + let index = shared_does_not_exists_index().await; let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; let expected_response = json!({ - "message": format!("Index `{}` not found.", index.uid), + "message": "Index `DOES_NOT_EXISTS` not found.", "code": "index_not_found", "type": "invalid_request", "link": "https://docs.meilisearch.com/errors#index_not_found" @@ -100,12 +98,7 @@ async fn error_get_unexisting_index_all_documents() { #[actix_rt::test] async fn get_no_document() { - let server = Server::new_shared(); - let index = server.unique_index(); - let (task, code) = index.create(None).await; - assert_eq!(code, 202); - - index.wait_task(task.uid()).await.succeeded(); + let index = shared_empty_index().await; let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; assert_eq!(code, 200); @@ -114,9 +107,7 @@ async fn get_no_document() { #[actix_rt::test] async fn get_all_documents_no_options() { - let server = Server::new_shared(); - let index = server.unique_index(); - index.load_test_set().await; + let index = shared_index_with_test_set().await; let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; assert_eq!(code, 200); @@ -145,11 +136,9 @@ async fn get_all_documents_no_options() { #[actix_rt::test] async fn get_all_documents_no_options_with_response_compression() { - let server = Server::new_shared(); - let index = server.unique_index(); - index.load_test_set().await; + let index = shared_index_with_test_set().await; - let app = server.init_web_app().await; + let app = Server::new_shared().init_web_app().await; let req = test::TestRequest::get() .uri(&format!("/indexes/{}/documents?", urlencode(&index.uid))) .insert_header((ACCEPT_ENCODING, "gzip")) @@ -170,9 +159,7 @@ async fn get_all_documents_no_options_with_response_compression() { #[actix_rt::test] async fn test_get_all_documents_limit() { - let server = Server::new_shared(); - let index = server.unique_index(); - index.load_test_set().await; + let index = shared_index_with_test_set().await; let (response, code) = index .get_all_documents(GetAllDocumentsOptions { limit: Some(5), ..Default::default() }) @@ -187,9 +174,7 @@ async fn test_get_all_documents_limit() { #[actix_rt::test] async fn test_get_all_documents_offset() { - let server = Server::new_shared(); - let index = server.unique_index(); - index.load_test_set().await; + let index = shared_index_with_test_set().await; let (response, code) = index .get_all_documents(GetAllDocumentsOptions { offset: Some(5), ..Default::default() }) @@ -204,9 +189,8 @@ async fn test_get_all_documents_offset() { #[actix_rt::test] async fn test_get_all_documents_attributes_to_retrieve() { - let server = Server::new_shared(); - let index = server.unique_index(); - index.load_test_set().await; + let index = shared_index_with_test_set().await; + let (response, code) = index .get_all_documents(GetAllDocumentsOptions { @@ -348,8 +332,8 @@ async fn get_document_s_nested_attributes_to_retrieve() { async fn get_documents_displayed_attributes_is_ignored() { let server = Server::new_shared(); let index = server.unique_index(); - index.update_settings(json!({"displayedAttributes": ["gender"]})).await; index.load_test_set().await; + index.update_settings(json!({"displayedAttributes": ["gender"]})).await; let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; assert_eq!(code, 200); From da4d47b5d083f4c215b366baaec96653d1a65973 Mon Sep 17 00:00:00 2001 From: Pedro Turik Firmino Date: Wed, 6 Nov 2024 09:54:20 -0300 Subject: [PATCH 4/5] Fixes formatting issues --- crates/meilisearch/tests/common/mod.rs | 36 ++++++++++--------- .../tests/documents/get_documents.rs | 8 +++-- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/crates/meilisearch/tests/common/mod.rs b/crates/meilisearch/tests/common/mod.rs index 9727d45bd..a0137931d 100644 --- a/crates/meilisearch/tests/common/mod.rs +++ b/crates/meilisearch/tests/common/mod.rs @@ -392,20 +392,22 @@ pub static VECTOR_DOCUMENTS: Lazy = Lazy::new(|| { pub async fn shared_index_with_test_set() -> &'static Index<'static, Shared> { static INDEX: OnceCell> = OnceCell::const_new(); - INDEX.get_or_init(|| async { - let server = Server::new_shared(); - let index = server._index("SHARED_TEST_SET").to_shared(); - let url = format!("/indexes/{}/documents", urlencoding::encode(index.uid.as_ref())); - let (response, code) = index - .service - .post_str( - url, - include_str!("../assets/test_set.json"), - vec![("content-type", "application/json")], - ) - .await; - assert_eq!(code, 202); - index.wait_task(response.uid()).await; - index - }).await -} \ No newline at end of file + INDEX + .get_or_init(|| async { + let server = Server::new_shared(); + let index = server._index("SHARED_TEST_SET").to_shared(); + let url = format!("/indexes/{}/documents", urlencoding::encode(index.uid.as_ref())); + let (response, code) = index + .service + .post_str( + url, + include_str!("../assets/test_set.json"), + vec![("content-type", "application/json")], + ) + .await; + assert_eq!(code, 202); + index.wait_task(response.uid()).await; + index + }) + .await +} diff --git a/crates/meilisearch/tests/documents/get_documents.rs b/crates/meilisearch/tests/documents/get_documents.rs index 8aaeb10be..d66d36c56 100644 --- a/crates/meilisearch/tests/documents/get_documents.rs +++ b/crates/meilisearch/tests/documents/get_documents.rs @@ -4,7 +4,10 @@ use meili_snap::*; use urlencoding::encode as urlencode; use crate::common::encoder::Encoder; -use crate::common::{shared_does_not_exists_index, shared_empty_index, shared_index_with_test_set, GetAllDocumentsOptions, Server, Value}; +use crate::common::{ + shared_does_not_exists_index, shared_empty_index, shared_index_with_test_set, + GetAllDocumentsOptions, Server, Value, +}; use crate::json; // TODO: partial test since we are testing error, amd error is not yet fully implemented in @@ -159,7 +162,7 @@ async fn get_all_documents_no_options_with_response_compression() { #[actix_rt::test] async fn test_get_all_documents_limit() { - let index = shared_index_with_test_set().await; + let index = shared_index_with_test_set().await; let (response, code) = index .get_all_documents(GetAllDocumentsOptions { limit: Some(5), ..Default::default() }) @@ -191,7 +194,6 @@ async fn test_get_all_documents_offset() { async fn test_get_all_documents_attributes_to_retrieve() { let index = shared_index_with_test_set().await; - let (response, code) = index .get_all_documents(GetAllDocumentsOptions { fields: Some(vec!["name"]), From da59a043ba74e46c274e70b5218b1c375cc12e10 Mon Sep 17 00:00:00 2001 From: Pedro Turik Firmino Date: Wed, 6 Nov 2024 09:55:48 -0300 Subject: [PATCH 5/5] Fixes formatting issues --- crates/meilisearch/tests/common/index.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/meilisearch/tests/common/index.rs b/crates/meilisearch/tests/common/index.rs index 784067c2d..221333fd7 100644 --- a/crates/meilisearch/tests/common/index.rs +++ b/crates/meilisearch/tests/common/index.rs @@ -9,8 +9,7 @@ use urlencoding::encode as urlencode; use super::encoder::Encoder; use super::service::Service; -use super::Value; -use super::{Owned, Shared}; +use super::{Owned, Shared, Value}; use crate::json; pub struct Index<'a, State = Owned> {