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,
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();
if let Err(e) = catch_unwind(move || t(response, code)) {
eprintln!("Error with post search");
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)) {
eprintln!("Error with get search");
resume_unwind(e);
}
}
}
pub async fn search_post(&self, query: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/search", urlencode(self.uid.as_ref()));

View File

@ -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!({
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;

View File

@ -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;