improve the test of the get document by filter

This commit is contained in:
Tamo 2023-05-03 19:43:23 +02:00
parent b92da5d15a
commit ce6507d20c
No known key found for this signature in database
GPG Key ID: 20CD8020AFA88D69
2 changed files with 65 additions and 6 deletions

View File

@ -140,11 +140,6 @@ impl Index<'_> {
} }
} }
pub async fn get_document_by_filter(&self, payload: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/fetch", urlencode(self.uid.as_ref()));
self.service.post(url, payload).await
}
pub async fn wait_task(&self, update_id: u64) -> Value { pub async fn wait_task(&self, update_id: u64) -> Value {
// try several times to get status, or panic to not wait forever // try several times to get status, or panic to not wait forever
let url = format!("/tasks/{}", update_id); let url = format!("/tasks/{}", update_id);
@ -203,6 +198,11 @@ impl Index<'_> {
self.service.get(url).await self.service.get(url).await
} }
pub async fn get_document_by_filter(&self, payload: Value) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents/fetch", urlencode(self.uid.as_ref()));
self.service.post(url, payload).await
}
pub async fn get_all_documents_raw(&self, options: &str) -> (Value, StatusCode) { pub async fn get_all_documents_raw(&self, options: &str) -> (Value, StatusCode) {
let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), options); let url = format!("/indexes/{}/documents{}", urlencode(self.uid.as_ref()), options);
self.service.get(url).await self.service.get(url).await

View File

@ -381,7 +381,7 @@ async fn get_documents_displayed_attributes_is_ignored() {
} }
#[actix_rt::test] #[actix_rt::test]
async fn fetch_document_by_filter() { async fn get_document_by_filter() {
let server = Server::new().await; let server = Server::new().await;
let index = server.index("doggo"); let index = server.index("doggo");
index.update_settings_filterable_attributes(json!(["color"])).await; index.update_settings_filterable_attributes(json!(["color"])).await;
@ -399,6 +399,7 @@ async fn fetch_document_by_filter() {
index.wait_task(1).await; index.wait_task(1).await;
let (response, code) = index.get_document_by_filter(json!({})).await; let (response, code) = index.get_document_by_filter(json!({})).await;
let (response2, code2) = index.get_all_documents_raw("").await;
snapshot!(code, @"200 OK"); snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###" snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{ {
@ -424,8 +425,11 @@ async fn fetch_document_by_filter() {
"total": 4 "total": 4
} }
"###); "###);
assert_eq!(code, code2);
assert_eq!(response, response2);
let (response, code) = index.get_document_by_filter(json!({ "filter": "color = blue" })).await; let (response, code) = index.get_document_by_filter(json!({ "filter": "color = blue" })).await;
let (response2, code2) = index.get_all_documents_raw("?filter=color=blue").await;
snapshot!(code, @"200 OK"); snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###" snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{ {
@ -444,10 +448,14 @@ async fn fetch_document_by_filter() {
"total": 2 "total": 2
} }
"###); "###);
assert_eq!(code, code2);
assert_eq!(response, response2);
let (response, code) = index let (response, code) = index
.get_document_by_filter(json!({ "offset": 1, "limit": 1, "filter": "color != blue" })) .get_document_by_filter(json!({ "offset": 1, "limit": 1, "filter": "color != blue" }))
.await; .await;
let (response2, code2) =
index.get_all_documents_raw("?filter=color!=blue&offset=1&limit=1").await;
snapshot!(code, @"200 OK"); snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###" snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{ {
@ -461,10 +469,14 @@ async fn fetch_document_by_filter() {
"total": 2 "total": 2
} }
"###); "###);
assert_eq!(code, code2);
assert_eq!(response, response2);
let (response, code) = index let (response, code) = index
.get_document_by_filter(json!({ "limit": 1, "filter": "color != blue", "fields": "color" })) .get_document_by_filter(json!({ "limit": 1, "filter": "color != blue", "fields": "color" }))
.await; .await;
let (response2, code2) =
index.get_all_documents_raw("?limit=1&filter=color!=blue&fields=color").await;
snapshot!(code, @"200 OK"); snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###" snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{ {
@ -478,4 +490,51 @@ async fn fetch_document_by_filter() {
"total": 2 "total": 2
} }
"###); "###);
assert_eq!(code, code2);
assert_eq!(response, response2);
// Now testing more complex filter that the get route can't represent
let (response, code) =
index.get_document_by_filter(json!({ "filter": [["color = blue", "color = red"]] })).await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
"results": [
{
"id": 0,
"color": "red"
},
{
"id": 1,
"color": "blue"
},
{
"id": 2,
"color": "blue"
}
],
"offset": 0,
"limit": 20,
"total": 3
}
"###);
let (response, code) = index
.get_document_by_filter(json!({ "filter": [["color != blue"], "color EXISTS"] }))
.await;
snapshot!(code, @"200 OK");
snapshot!(json_string!(response, { ".enqueuedAt" => "[date]" }), @r###"
{
"results": [
{
"id": 0,
"color": "red"
}
],
"offset": 0,
"limit": 20,
"total": 1
}
"###);
} }