Use insta snapshot

This commit is contained in:
ManyTheFish 2023-06-22 14:42:52 +02:00
parent 63ca25290b
commit 9d2a12821d
3 changed files with 95 additions and 60 deletions

View File

@ -346,19 +346,26 @@ impl Index<'_> {
query: Value, query: Value,
test: impl Fn(Value, StatusCode) + UnwindSafe + Clone, test: impl Fn(Value, StatusCode) + UnwindSafe + Clone,
) { ) {
let (response, code) = self.search_post(query.clone()).await; let post = self.search_post(query.clone()).await;
let query = yaup::to_string(&query).unwrap();
let get = self.search_get(&query).await;
insta::allow_duplicates! {
let (response, code) = post;
let t = test.clone(); let t = test.clone();
if let Err(e) = catch_unwind(move || t(response, code)) { if let Err(e) = catch_unwind(move || t(response, code)) {
eprintln!("Error with post search"); eprintln!("Error with post search");
resume_unwind(e); resume_unwind(e);
} }
let query = yaup::to_string(&query).unwrap();
let (response, code) = self.search_get(&query).await; let (response, code) = get;
if let Err(e) = catch_unwind(move || test(response, code)) { if let Err(e) = catch_unwind(move || test(response, code)) {
eprintln!("Error with get search"); eprintln!("Error with get search");
resume_unwind(e); resume_unwind(e);
} }
} }
}
pub async fn search_post(&self, query: Value) -> (Value, StatusCode) { pub async fn search_post(&self, query: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/search", urlencode(self.uid.as_ref())); let url = format!("/indexes/{}/search", urlencode(self.uid.as_ref()));

View File

@ -976,13 +976,15 @@ async fn search_on_unknown_field() {
.search( .search(
json!({"q": "Captain Marvel", "attributesToSearchOn": ["unknown"]}), json!({"q": "Captain Marvel", "attributesToSearchOn": ["unknown"]}),
|response, code| { |response, code| {
assert_eq!(400, code, "{}", response); snapshot!(code, @"400 Bad Request");
assert_eq!(response, json!({ snapshot!(json_string!(response), @r###"
{
"message": "Attribute `unknown` is not searchable. Available searchable attributes are: `id, title`.", "message": "Attribute `unknown` is not searchable. Available searchable attributes are: `id, title`.",
"code": "invalid_attributes_to_search_on", "code": "invalid_attributes_to_search_on",
"type": "invalid_request", "type": "invalid_request",
"link": "https://docs.meilisearch.com/errors#invalid_attributes_to_search_on" "link": "https://docs.meilisearch.com/errors#invalid_attributes_to_search_on"
})); }
"###);
}, },
) )
.await; .await;

View File

@ -1,3 +1,4 @@
use meili_snap::{json_string, snapshot};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use serde_json::{json, Value}; use serde_json::{json, Value};
@ -41,8 +42,8 @@ async fn simple_search_on_title() {
.search( .search(
json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"]}), json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"]}),
|response, code| { |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!(response["hits"].as_array().unwrap().len(), 2); snapshot!(response["hits"].as_array().unwrap().len(), @"2");
}, },
) )
.await; .await;
@ -56,8 +57,8 @@ async fn simple_prefix_search_on_title() {
// simple search should return 2 documents (ids: 2 and 3). // simple search should return 2 documents (ids: 2 and 3).
index index
.search(json!({"q": "Captain Mar", "attributesToSearchOn": ["title"]}), |response, code| { .search(json!({"q": "Captain Mar", "attributesToSearchOn": ["title"]}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!(response["hits"].as_array().unwrap().len(), 2); snapshot!(response["hits"].as_array().unwrap().len(), @"2");
}) })
.await; .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). // simple search matching strategy all should only return 1 document (ids: 2).
index index
.search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "matchingStrategy": "all"}), |response, code| { .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "matchingStrategy": "all"}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!(response["hits"].as_array().unwrap().len(), 1); snapshot!(response["hits"].as_array().unwrap().len(), @"1");
}) })
.await; .await;
} }
@ -82,8 +83,8 @@ async fn simple_search_on_no_field() {
// simple search on no field shouldn't return any document. // simple search on no field shouldn't return any document.
index index
.search(json!({"q": "Captain Marvel", "attributesToSearchOn": []}), |response, code| { .search(json!({"q": "Captain Marvel", "attributesToSearchOn": []}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!(response["hits"].as_array().unwrap().len(), 0); snapshot!(response["hits"].as_array().unwrap().len(), @"0");
}) })
.await; .await;
} }
@ -98,13 +99,18 @@ async fn word_ranking_rule_order() {
.search( .search(
json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}),
|response, code| { |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!( snapshot!(json_string!(response["hits"]),
response["hits"], @r###"
json!([ [
{"id": "3"}, {
{"id": "2"}, "id": "3"
]) },
{
"id": "2"
}
]
"###
); );
}, },
) )
@ -123,13 +129,18 @@ async fn word_ranking_rule_order_exact_words() {
.search( .search(
json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}),
|response, code| { |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!( snapshot!(json_string!(response["hits"]),
response["hits"], @r###"
json!([ [
{"id": "3"}, {
{"id": "2"}, "id": "3"
]) },
{
"id": "2"
}
]
"###
); );
}, },
) )
@ -158,13 +169,18 @@ async fn typo_ranking_rule_order() {
// Document 2 should appear before document 1. // Document 2 should appear before document 1.
index index
.search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| { .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["title"], "attributesToRetrieve": ["id"]}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!( snapshot!(json_string!(response["hits"]),
response["hits"], @r###"
json!([ [
{"id": "2"}, {
{"id": "1"}, "id": "2"
]) },
{
"id": "1"
}
]
"###
); );
}) })
.await; .await;
@ -194,13 +210,18 @@ async fn attributes_ranking_rule_order() {
// Document 2 should appear before document 1. // Document 2 should appear before document 1.
index index
.search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["desc", "footer"], "attributesToRetrieve": ["id"]}), |response, code| { .search(json!({"q": "Captain Marvel", "attributesToSearchOn": ["desc", "footer"], "attributesToRetrieve": ["id"]}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!( snapshot!(json_string!(response["hits"]),
response["hits"], @r###"
json!([ [
{"id": "2"}, {
{"id": "1"}, "id": "2"
]) },
{
"id": "1"
}
]
"###
); );
}) })
.await; .await;
@ -228,13 +249,18 @@ async fn exactness_ranking_rule_order() {
// Document 2 should appear before document 1. // Document 2 should appear before document 1.
index index
.search(json!({"q": "Captain Marvel", "attributesToRetrieve": ["id"], "attributesToSearchOn": ["desc"]}), |response, code| { .search(json!({"q": "Captain Marvel", "attributesToRetrieve": ["id"], "attributesToSearchOn": ["desc"]}), |response, code| {
assert_eq!(200, code, "{}", response); snapshot!(code, @"200 OK");
assert_eq!( snapshot!(json_string!(response["hits"]),
response["hits"], @r###"
json!([ [
{"id": "2"}, {
{"id": "1"}, "id": "2"
]) },
{
"id": "1"
}
]
"###
); );
}) })
.await; .await;