From 9d2a12821d098accf04c82ca943d43f3710943fe Mon Sep 17 00:00:00 2001 From: ManyTheFish Date: Thu, 22 Jun 2023 14:42:52 +0200 Subject: [PATCH] Use insta snapshot --- meilisearch/tests/common/index.rs | 27 +++-- meilisearch/tests/search/errors.rs | 16 +-- .../tests/search/restrict_searchable.rs | 112 +++++++++++------- 3 files changed, 95 insertions(+), 60 deletions(-) diff --git a/meilisearch/tests/common/index.rs b/meilisearch/tests/common/index.rs index beab9970b..517024c74 100644 --- a/meilisearch/tests/common/index.rs +++ b/meilisearch/tests/common/index.rs @@ -346,17 +346,24 @@ impl Index<'_> { query: Value, test: impl Fn(Value, StatusCode) + UnwindSafe + Clone, ) { - let (response, code) = self.search_post(query.clone()).await; - let t = test.clone(); - if let Err(e) = catch_unwind(move || t(response, code)) { - eprintln!("Error with post search"); - resume_unwind(e); - } + let post = self.search_post(query.clone()).await; + let query = yaup::to_string(&query).unwrap(); - let (response, code) = self.search_get(&query).await; - if let Err(e) = catch_unwind(move || test(response, code)) { - eprintln!("Error with get search"); - resume_unwind(e); + let get = self.search_get(&query).await; + + insta::allow_duplicates! { + let (response, code) = post; + let t = test.clone(); + if let Err(e) = catch_unwind(move || t(response, code)) { + eprintln!("Error with post search"); + resume_unwind(e); + } + + let (response, code) = get; + if let Err(e) = catch_unwind(move || test(response, code)) { + eprintln!("Error with get search"); + resume_unwind(e); + } } } diff --git a/meilisearch/tests/search/errors.rs b/meilisearch/tests/search/errors.rs index 72c2c5b90..273472d46 100644 --- a/meilisearch/tests/search/errors.rs +++ b/meilisearch/tests/search/errors.rs @@ -976,13 +976,15 @@ async fn search_on_unknown_field() { .search( json!({"q": "Captain Marvel", "attributesToSearchOn": ["unknown"]}), |response, code| { - assert_eq!(400, code, "{}", response); - assert_eq!(response, json!({ - "message": "Attribute `unknown` is not searchable. Available searchable attributes are: `id, title`.", - "code": "invalid_attributes_to_search_on", - "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#invalid_attributes_to_search_on" - })); + snapshot!(code, @"400 Bad Request"); + snapshot!(json_string!(response), @r###" + { + "message": "Attribute `unknown` is not searchable. Available searchable attributes are: `id, title`.", + "code": "invalid_attributes_to_search_on", + "type": "invalid_request", + "link": "https://docs.meilisearch.com/errors#invalid_attributes_to_search_on" + } + "###); }, ) .await; diff --git a/meilisearch/tests/search/restrict_searchable.rs b/meilisearch/tests/search/restrict_searchable.rs index 9379b6558..f119acea5 100644 --- a/meilisearch/tests/search/restrict_searchable.rs +++ b/meilisearch/tests/search/restrict_searchable.rs @@ -1,3 +1,4 @@ +use meili_snap::{json_string, snapshot}; use once_cell::sync::Lazy; use serde_json::{json, Value}; @@ -41,8 +42,8 @@ async fn simple_search_on_title() { .search( json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!(response["hits"].as_array().unwrap().len(), 2); + snapshot!(code, @"200 OK"); + snapshot!(response["hits"].as_array().unwrap().len(), @"2"); }, ) .await; @@ -56,8 +57,8 @@ async fn simple_prefix_search_on_title() { // simple search should return 2 documents (ids: 2 and 3). index .search(json!({"q": "Captain Mar", "attributesToSearchOn": ["title"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!(response["hits"].as_array().unwrap().len(), 2); + snapshot!(code, @"200 OK"); + snapshot!(response["hits"].as_array().unwrap().len(), @"2"); }) .await; } @@ -69,8 +70,8 @@ async fn simple_search_on_title_matching_strategy_all() { // simple search matching strategy all should only return 1 document (ids: 2). index .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "matchingStrategy": "all"}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!(response["hits"].as_array().unwrap().len(), 1); + snapshot!(code, @"200 OK"); + snapshot!(response["hits"].as_array().unwrap().len(), @"1"); }) .await; } @@ -82,8 +83,8 @@ async fn simple_search_on_no_field() { // simple search on no field shouldn't return any document. index .search(json!({"q": "Captain Marvel", "attributesToSearchOn": []}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!(response["hits"].as_array().unwrap().len(), 0); + snapshot!(code, @"200 OK"); + snapshot!(response["hits"].as_array().unwrap().len(), @"0"); }) .await; } @@ -98,13 +99,18 @@ async fn word_ranking_rule_order() { .search( json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!( - response["hits"], - json!([ - {"id": "3"}, - {"id": "2"}, - ]) + snapshot!(code, @"200 OK"); + snapshot!(json_string!(response["hits"]), + @r###" + [ + { + "id": "3" + }, + { + "id": "2" + } + ] + "### ); }, ) @@ -123,13 +129,18 @@ async fn word_ranking_rule_order_exact_words() { .search( json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!( - response["hits"], - json!([ - {"id": "3"}, - {"id": "2"}, - ]) + snapshot!(code, @"200 OK"); + snapshot!(json_string!(response["hits"]), + @r###" + [ + { + "id": "3" + }, + { + "id": "2" + } + ] + "### ); }, ) @@ -158,13 +169,18 @@ async fn typo_ranking_rule_order() { // Document 2 should appear before document 1. index .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!( - response["hits"], - json!([ - {"id": "2"}, - {"id": "1"}, - ]) + snapshot!(code, @"200 OK"); + snapshot!(json_string!(response["hits"]), + @r###" + [ + { + "id": "2" + }, + { + "id": "1" + } + ] + "### ); }) .await; @@ -194,13 +210,18 @@ async fn attributes_ranking_rule_order() { // Document 2 should appear before document 1. index .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["desc", "footer"], "attributesToRetrieve": ["id"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!( - response["hits"], - json!([ - {"id": "2"}, - {"id": "1"}, - ]) + snapshot!(code, @"200 OK"); + snapshot!(json_string!(response["hits"]), + @r###" + [ + { + "id": "2" + }, + { + "id": "1" + } + ] + "### ); }) .await; @@ -228,13 +249,18 @@ async fn exactness_ranking_rule_order() { // Document 2 should appear before document 1. index .search(json!({"q": "Captain Marvel", "attributesToRetrieve": ["id"], "attributesToSearchOn": ["desc"]}), |response, code| { - assert_eq!(200, code, "{}", response); - assert_eq!( - response["hits"], - json!([ - {"id": "2"}, - {"id": "1"}, - ]) + snapshot!(code, @"200 OK"); + snapshot!(json_string!(response["hits"]), + @r###" + [ + { + "id": "2" + }, + { + "id": "1" + } + ] + "### ); }) .await;