From d3758b6f76d16dd8d0a506edf8a5a6a4a0949bc6 Mon Sep 17 00:00:00 2001 From: mpostma Date: Mon, 22 Feb 2021 16:03:17 +0100 Subject: [PATCH] test delete documents --- tests/common/index.rs | 14 +++++- tests/documents/delete_documents.rs | 72 +++++++++++++++++++++++++++++ tests/documents/mod.rs | 1 + 3 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 tests/documents/delete_documents.rs diff --git a/tests/common/index.rs b/tests/common/index.rs index ed5e533a5..8afe78a35 100644 --- a/tests/common/index.rs +++ b/tests/common/index.rs @@ -68,7 +68,7 @@ impl Index<'_> { self.service.put(url, documents).await } - pub async fn wait_update_id(&self, update_id: u64) { + pub async fn wait_update_id(&self, update_id: u64) -> Value { // try 10 times to get status, or panic to not wait forever let url = format!("/indexes/{}/updates/{}", self.uid, update_id); for _ in 0..10 { @@ -76,7 +76,7 @@ impl Index<'_> { assert_eq!(status_code, 200); if response["status"] == "processed" || response["status"] == "failed" { - return; + return response; } delay_for(Duration::from_secs(1)).await; @@ -116,6 +116,16 @@ impl Index<'_> { self.service.get(url).await } + + pub async fn delete_document(&self, id: u64) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents/{}", self.uid, id); + self.service.delete(url).await + } + + pub async fn clear_all_documents(&self) -> (Value, StatusCode) { + let url = format!("/indexes/{}/documents", self.uid); + self.service.delete(url).await + } } pub struct GetDocumentOptions; diff --git a/tests/documents/delete_documents.rs b/tests/documents/delete_documents.rs new file mode 100644 index 000000000..149319308 --- /dev/null +++ b/tests/documents/delete_documents.rs @@ -0,0 +1,72 @@ +use serde_json::json; + +use crate::common::{Server, GetAllDocumentsOptions}; + +#[actix_rt::test] +async fn delete_one_document_unexisting_index() { + let server = Server::new().await; + let (_response, code) = server.index("test").delete_document(0).await; + assert_eq!(code, 400); +} + +#[actix_rt::test] +async fn delete_one_unexisting_document() { + let server = Server::new().await; + let index = server.index("test"); + index.create(None).await; + let (_response, code) = index.delete_document(0).await; + assert_eq!(code, 200); + let update = index.wait_update_id(0).await; + assert_eq!(update["status"], "processed"); +} + +#[actix_rt::test] +async fn delete_one_document() { + let server = Server::new().await; + let index = server.index("test"); + index.add_documents(json!([{ "id": 0, "content": "foobar" }]), None).await; + index.wait_update_id(0).await; + let (_response, code) = server.index("test").delete_document(0).await; + assert_eq!(code, 200); + + let (_response, code) = index.get_document(0, None).await; + assert_eq!(code, 400); +} + +#[actix_rt::test] +async fn clear_all_documents_unexisting_index() { + let server = Server::new().await; + let (_response, code) = server.index("test").clear_all_documents().await; + assert_eq!(code, 400); + +} + +#[actix_rt::test] +async fn clear_all_documents() { + let server = Server::new().await; + let index = server.index("test"); + index.add_documents(json!([{ "id": 1, "content": "foobar" }, { "id": 0, "content": "foobar" }]), None).await; + index.wait_update_id(0).await; + let (_response, code) = index.clear_all_documents().await; + assert_eq!(code, 200); + + let _update = index.wait_update_id(0).await; + let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; + assert_eq!(code, 200); + assert!(response.as_array().unwrap().is_empty()); +} + +#[actix_rt::test] +async fn clear_all_documents_empty_index() { + let server = Server::new().await; + let index = server.index("test"); + index.create(None).await; + + let (_response, code) = index.clear_all_documents().await; + assert_eq!(code, 200); + + let _update = index.wait_update_id(0).await; + let (response, code) = index.get_all_documents(GetAllDocumentsOptions::default()).await; + assert_eq!(code, 200); + assert!(response.as_array().unwrap().is_empty()); +} diff --git a/tests/documents/mod.rs b/tests/documents/mod.rs index 69c73e37a..ea0e39c69 100644 --- a/tests/documents/mod.rs +++ b/tests/documents/mod.rs @@ -1,2 +1,3 @@ mod add_documents; mod get_documents; +mod delete_documents;