diff --git a/meilisearch/tests/auth/api_keys.rs b/meilisearch/tests/auth/api_keys.rs index 1cd02defc..253929428 100644 --- a/meilisearch/tests/auth/api_keys.rs +++ b/meilisearch/tests/auth/api_keys.rs @@ -1,8 +1,7 @@ use std::{thread, time}; -use serde_json::{json, Value}; - -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; #[actix_rt::test] async fn add_valid_api_key() { @@ -162,7 +161,7 @@ async fn add_valid_api_key_null_description() { server.use_api_key("MASTER_KEY"); let content = json!({ - "description": Value::Null, + "description": json!(null), "indexes": ["products"], "actions": ["documents.add"], "expiresAt": "2050-11-13T00:00:00" @@ -365,7 +364,7 @@ async fn error_add_api_key_invalid_index_uids() { server.use_api_key("MASTER_KEY"); let content = json!({ - "description": Value::Null, + "description": json!(null), "indexes": ["invalid index # / \\name with spaces"], "actions": [ "documents.add" @@ -507,7 +506,7 @@ async fn error_add_api_key_invalid_parameters_uid() { async fn error_add_api_key_parameters_uid_already_exist() { let mut server = Server::new_auth().await; server.use_api_key("MASTER_KEY"); - let content = json!({ + let content: Value = json!({ "uid": "4bc0887a-0e41-4f3b-935d-0c451dcee9c8", "indexes": ["products"], "actions": ["search"], @@ -1146,7 +1145,7 @@ async fn patch_api_key_description() { meili_snap::snapshot!(code, @"200 OK"); // Remove the description - let content = json!({ "description": serde_json::Value::Null }); + let content = json!({ "description": null }); let (response, code) = server.patch_api_key(&uid, content).await; meili_snap::snapshot!(meili_snap::json_string!(response, { ".createdAt" => "[ignored]", ".updatedAt" => "[ignored]", ".uid" => "[ignored]", ".key" => "[ignored]" }), @r###" diff --git a/meilisearch/tests/auth/authorization.rs b/meilisearch/tests/auth/authorization.rs index b122ec4a3..883c23267 100644 --- a/meilisearch/tests/auth/authorization.rs +++ b/meilisearch/tests/auth/authorization.rs @@ -3,10 +3,10 @@ use std::collections::{HashMap, HashSet}; use ::time::format_description::well_known::Rfc3339; use maplit::{hashmap, hashset}; use once_cell::sync::Lazy; -use serde_json::{json, Value}; use time::{Duration, OffsetDateTime}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; pub static AUTHORIZATIONS: Lazy>> = Lazy::new(|| { diff --git a/meilisearch/tests/auth/errors.rs b/meilisearch/tests/auth/errors.rs index fc5480011..581243a0a 100644 --- a/meilisearch/tests/auth/errors.rs +++ b/meilisearch/tests/auth/errors.rs @@ -1,8 +1,8 @@ use meili_snap::*; -use serde_json::json; use uuid::Uuid; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn create_api_key_bad_description() { diff --git a/meilisearch/tests/auth/mod.rs b/meilisearch/tests/auth/mod.rs index 2d88bf87e..781b2dcc0 100644 --- a/meilisearch/tests/auth/mod.rs +++ b/meilisearch/tests/auth/mod.rs @@ -7,9 +7,9 @@ mod tenant_token; mod tenant_token_multi_search; use actix_web::http::StatusCode; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; impl Server { pub fn use_api_key(&mut self, api_key: impl AsRef) { diff --git a/meilisearch/tests/auth/tenant_token.rs b/meilisearch/tests/auth/tenant_token.rs index 3992a9aed..ba3b0b234 100644 --- a/meilisearch/tests/auth/tenant_token.rs +++ b/meilisearch/tests/auth/tenant_token.rs @@ -3,11 +3,11 @@ use std::collections::HashMap; use ::time::format_description::well_known::Rfc3339; use maplit::hashmap; use once_cell::sync::Lazy; -use serde_json::{json, Value}; use time::{Duration, OffsetDateTime}; use super::authorization::{ALL_ACTIONS, AUTHORIZATIONS}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; fn generate_tenant_token( parent_uid: impl AsRef, @@ -233,31 +233,31 @@ async fn search_authorized_simple_token() { }, hashmap! { "searchRules" => json!({"*": {}}), - "exp" => Value::Null + "exp" => json!(null) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"*": null}), + "exp" => json!(null) }, hashmap! { "searchRules" => json!(["*"]), - "exp" => Value::Null + "exp" => json!(null) }, hashmap! { "searchRules" => json!({"sales": {}}), - "exp" => Value::Null + "exp" => json!(null) }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"sales": null}), + "exp" => json!(null) }, hashmap! { "searchRules" => json!(["sales"]), - "exp" => Value::Null + "exp" => json!(null) }, hashmap! { "searchRules" => json!(["sa*"]), - "exp" => Value::Null + "exp" => json!(null) }, ]; @@ -386,7 +386,7 @@ async fn error_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -398,7 +398,7 @@ async fn error_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), + "searchRules" => json!({"sales": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -428,15 +428,15 @@ async fn error_search_forbidden_token() { }, hashmap! { "searchRules" => json!({"products": {}}), - "exp" => Value::Null + "exp" => json!(null) }, hashmap! { - "searchRules" => json!({"products": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"products": null}), + "exp" => json!(null) }, hashmap! { "searchRules" => json!(["products"]), - "exp" => Value::Null + "exp" => json!(null) }, // expired token hashmap! { @@ -444,7 +444,7 @@ async fn error_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -456,7 +456,7 @@ async fn error_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), + "searchRules" => json!({"sales": null}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { diff --git a/meilisearch/tests/auth/tenant_token_multi_search.rs b/meilisearch/tests/auth/tenant_token_multi_search.rs index ef206ea3d..09b5dbbcc 100644 --- a/meilisearch/tests/auth/tenant_token_multi_search.rs +++ b/meilisearch/tests/auth/tenant_token_multi_search.rs @@ -3,11 +3,11 @@ use std::collections::HashMap; use ::time::format_description::well_known::Rfc3339; use maplit::hashmap; use once_cell::sync::Lazy; -use serde_json::{json, Value}; use time::{Duration, OffsetDateTime}; use super::authorization::ALL_ACTIONS; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; fn generate_tenant_token( parent_uid: impl AsRef, @@ -512,31 +512,31 @@ async fn single_search_authorized_simple_token() { }, hashmap! { "searchRules" => json!({"*": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"*": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"*": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["*"]), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { "searchRules" => json!({"sales": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"sales": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["sales"]), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["sa*"]), - "exp" => Value::Null + "exp" => json!(null), }, ]; @@ -564,31 +564,31 @@ async fn multi_search_authorized_simple_token() { }, hashmap! { "searchRules" => json!({"*": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"*": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"*": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["*"]), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { "searchRules" => json!({"sales": {}, "products": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"sales": Value::Null, "products": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"sales": null, "products": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["sales", "products"]), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["sa*", "pro*"]), - "exp" => Value::Null + "exp" => json!(null), }, ]; @@ -823,7 +823,7 @@ async fn error_single_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -835,7 +835,7 @@ async fn error_single_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), + "searchRules" => json!({"sales": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -864,7 +864,7 @@ async fn error_multi_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -876,7 +876,7 @@ async fn error_multi_search_token_forbidden_parent_key() { "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null, "products": Value::Null}), + "searchRules" => json!({"sales": null, "products": null}), "exp" => json!((OffsetDateTime::now_utc() + Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -919,15 +919,15 @@ async fn error_single_search_forbidden_token() { }, hashmap! { "searchRules" => json!({"products": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"products": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"products": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["products"]), - "exp" => Value::Null + "exp" => json!(null), }, // expired token hashmap! { @@ -935,7 +935,7 @@ async fn error_single_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -947,7 +947,7 @@ async fn error_single_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), + "searchRules" => json!({"sales": null}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -978,15 +978,15 @@ async fn error_multi_search_forbidden_token() { }, hashmap! { "searchRules" => json!({"products": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"products": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"products": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["products"]), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { "searchRules" => json!({"sales": {}}), @@ -998,15 +998,15 @@ async fn error_multi_search_forbidden_token() { }, hashmap! { "searchRules" => json!({"sales": {}}), - "exp" => Value::Null + "exp" => json!(null), }, hashmap! { - "searchRules" => json!({"sales": Value::Null}), - "exp" => Value::Null + "searchRules" => json!({"sales": null}), + "exp" => json!(null), }, hashmap! { "searchRules" => json!(["sales"]), - "exp" => Value::Null + "exp" => json!(null), }, // expired token hashmap! { @@ -1014,7 +1014,7 @@ async fn error_multi_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"*": Value::Null}), + "searchRules" => json!({"*": null}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { @@ -1026,7 +1026,7 @@ async fn error_multi_search_forbidden_token() { "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { - "searchRules" => json!({"sales": Value::Null, "products": {}}), + "searchRules" => json!({"sales": null, "products": {}}), "exp" => json!((OffsetDateTime::now_utc() - Duration::hours(1)).unix_timestamp()) }, hashmap! { diff --git a/meilisearch/tests/common/index.rs b/meilisearch/tests/common/index.rs index 1bb4345e8..4992eeb13 100644 --- a/meilisearch/tests/common/index.rs +++ b/meilisearch/tests/common/index.rs @@ -3,12 +3,13 @@ use std::panic::{catch_unwind, resume_unwind, UnwindSafe}; use std::time::Duration; use actix_web::http::StatusCode; -use serde_json::{json, Value}; use tokio::time::sleep; use urlencoding::encode as urlencode; use super::encoder::Encoder; use super::service::Service; +use super::Value; +use crate::json; pub struct Index<'a> { pub uid: String, @@ -242,7 +243,9 @@ impl Index<'_> { pub async fn delete_batch(&self, ids: Vec) -> (Value, StatusCode) { let url = format!("/indexes/{}/documents/delete-batch", urlencode(self.uid.as_ref())); - self.service.post_encoded(url, serde_json::to_value(&ids).unwrap(), self.encoder).await + self.service + .post_encoded(url, serde_json::to_value(&ids).unwrap().into(), self.encoder) + .await } pub async fn delete_batch_raw(&self, body: Value) -> (Value, StatusCode) { diff --git a/meilisearch/tests/common/mod.rs b/meilisearch/tests/common/mod.rs index cb8f576ce..20acb3e4e 100644 --- a/meilisearch/tests/common/mod.rs +++ b/meilisearch/tests/common/mod.rs @@ -3,9 +3,84 @@ pub mod index; pub mod server; pub mod service; +use std::fmt::{self, Display}; + pub use index::{GetAllDocumentsOptions, GetDocumentOptions}; +use meili_snap::json_string; +use serde::{Deserialize, Serialize}; pub use server::{default_settings, Server}; +#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)] +pub struct Value(pub serde_json::Value); + +impl Value { + pub fn uid(&self) -> u64 { + if let Some(uid) = self["uid"].as_u64() { + uid + } else if let Some(uid) = self["taskUid"].as_u64() { + uid + } else { + panic!("Didn't find any task id in: {self}"); + } + } +} + +impl Into for serde_json::Value { + fn into(self) -> Value { + serde_json::json!(null); + Value(self) + } +} + +impl std::ops::Deref for Value { + type Target = serde_json::Value; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl PartialEq for Value { + fn eq(&self, other: &serde_json::Value) -> bool { + &self.0 == other + } +} + +impl PartialEq for serde_json::Value { + fn eq(&self, other: &Value) -> bool { + self == &other.0 + } +} + +impl PartialEq<&str> for Value { + fn eq(&self, other: &&str) -> bool { + self.0.eq(other) + } +} + +impl Display for Value { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!( + f, + "{}", + json_string!(self, { ".enqueuedAt" => "[date]", ".processedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }) + ) + } +} + +impl From> for Value { + fn from(value: Vec) -> Self { + Self(value.into_iter().map(|value| value.0).collect::()) + } +} + +#[macro_export] +macro_rules! json { + ($($json:tt)+) => { + $crate::common::Value(serde_json::json!($($json)+)) + }; +} + /// Performs a search test on both post and get routes #[macro_export] macro_rules! test_post_get_search { diff --git a/meilisearch/tests/common/server.rs b/meilisearch/tests/common/server.rs index d3ec71a72..c5d2cd85c 100644 --- a/meilisearch/tests/common/server.rs +++ b/meilisearch/tests/common/server.rs @@ -11,13 +11,14 @@ use clap::Parser; use meilisearch::option::{IndexerOpts, MaxMemory, Opt}; use meilisearch::{analytics, create_app, setup_meilisearch}; use once_cell::sync::Lazy; -use serde_json::{json, Value}; use tempfile::TempDir; use tokio::time::sleep; use super::index::Index; use super::service::Service; use crate::common::encoder::Encoder; +use crate::common::Value; +use crate::json; pub struct Server { pub service: Service, diff --git a/meilisearch/tests/common/service.rs b/meilisearch/tests/common/service.rs index 2ff474157..078ddb6e5 100644 --- a/meilisearch/tests/common/service.rs +++ b/meilisearch/tests/common/service.rs @@ -7,9 +7,9 @@ use actix_web::test::TestRequest; use index_scheduler::IndexScheduler; use meilisearch::{analytics, create_app, Opt}; use meilisearch_auth::AuthController; -use serde_json::Value; use crate::common::encoder::Encoder; +use crate::common::Value; pub struct Service { pub index_scheduler: Arc, diff --git a/meilisearch/tests/content_type.rs b/meilisearch/tests/content_type.rs index e16a83c06..0fc5b26ac 100644 --- a/meilisearch/tests/content_type.rs +++ b/meilisearch/tests/content_type.rs @@ -3,9 +3,8 @@ mod common; use actix_web::test; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; enum HttpVerb { Put, diff --git a/meilisearch/tests/documents/add_documents.rs b/meilisearch/tests/documents/add_documents.rs index 11a5e0fc8..b2904691f 100644 --- a/meilisearch/tests/documents/add_documents.rs +++ b/meilisearch/tests/documents/add_documents.rs @@ -1,11 +1,11 @@ use actix_web::test; use meili_snap::{json_string, snapshot}; -use serde_json::{json, Value}; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; use crate::common::encoder::Encoder; -use crate::common::{GetAllDocumentsOptions, Server}; +use crate::common::{GetAllDocumentsOptions, Server, Value}; +use crate::json; /// This is the basic usage of our API and every other tests uses the content-type application/json #[actix_rt::test] diff --git a/meilisearch/tests/documents/delete_documents.rs b/meilisearch/tests/documents/delete_documents.rs index 8f6ae1985..760ebbaa8 100644 --- a/meilisearch/tests/documents/delete_documents.rs +++ b/meilisearch/tests/documents/delete_documents.rs @@ -1,7 +1,7 @@ use meili_snap::{json_string, snapshot}; -use serde_json::json; use crate::common::{GetAllDocumentsOptions, Server}; +use crate::json; #[actix_rt::test] async fn delete_one_document_unexisting_index() { diff --git a/meilisearch/tests/documents/errors.rs b/meilisearch/tests/documents/errors.rs index 7dab16a25..bd06aabce 100644 --- a/meilisearch/tests/documents/errors.rs +++ b/meilisearch/tests/documents/errors.rs @@ -1,8 +1,8 @@ use meili_snap::*; -use serde_json::json; use urlencoding::encode; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn get_all_documents_bad_offset() { diff --git a/meilisearch/tests/documents/get_documents.rs b/meilisearch/tests/documents/get_documents.rs index 7e6c5a2a9..3b0629fcb 100644 --- a/meilisearch/tests/documents/get_documents.rs +++ b/meilisearch/tests/documents/get_documents.rs @@ -1,11 +1,11 @@ use actix_web::test; use http::header::ACCEPT_ENCODING; use meili_snap::*; -use serde_json::{json, Value}; use urlencoding::encode as urlencode; use crate::common::encoder::Encoder; -use crate::common::{GetAllDocumentsOptions, GetDocumentOptions, Server}; +use crate::common::{GetAllDocumentsOptions, GetDocumentOptions, Server, Value}; +use crate::json; // TODO: partial test since we are testing error, amd error is not yet fully implemented in // transplant @@ -40,7 +40,7 @@ async fn get_document() { let server = Server::new().await; let index = server.index("test"); index.create(None).await; - let documents = serde_json::json!([ + let documents = json!([ { "id": 0, "nested": { "content": "foobar" }, @@ -53,7 +53,7 @@ async fn get_document() { assert_eq!(code, 200); assert_eq!( response, - serde_json::json!({ + json!({ "id": 0, "nested": { "content": "foobar" }, }) @@ -64,7 +64,7 @@ async fn get_document() { assert_eq!(code, 200); assert_eq!( response, - serde_json::json!({ + json!({ "id": 0, }) ); @@ -75,7 +75,7 @@ async fn get_document() { assert_eq!(code, 200); assert_eq!( response, - serde_json::json!({ + json!({ "nested": { "content": "foobar" }, }) ); @@ -122,7 +122,7 @@ async fn get_all_documents_no_options() { assert_eq!(code, 200); let arr = response["results"].as_array().unwrap(); assert_eq!(arr.len(), 20); - let first = serde_json::json!({ + let first = json!({ "id":0, "isActive":false, "balance":"$2,668.55", diff --git a/meilisearch/tests/documents/update_documents.rs b/meilisearch/tests/documents/update_documents.rs index 688605861..b4f61bf99 100644 --- a/meilisearch/tests/documents/update_documents.rs +++ b/meilisearch/tests/documents/update_documents.rs @@ -1,7 +1,8 @@ -use serde_json::json; +use meili_snap::snapshot; use crate::common::encoder::Encoder; use crate::common::{GetAllDocumentsOptions, Server}; +use crate::json; #[actix_rt::test] async fn error_document_update_create_index_bad_uid() { @@ -84,7 +85,13 @@ async fn update_document() { let (response, code) = index.get_document(1, None).await; assert_eq!(code, 200); - assert_eq!(response.to_string(), r##"{"doc_id":1,"content":"foo","other":"bar"}"##); + snapshot!(response, @r###" + { + "doc_id": 1, + "content": "foo", + "other": "bar" + } + "###); } #[actix_rt::test] @@ -122,7 +129,13 @@ async fn update_document_gzip_encoded() { let (response, code) = index.get_document(1, None).await; assert_eq!(code, 200); - assert_eq!(response.to_string(), r##"{"doc_id":1,"content":"foo","other":"bar"}"##); + snapshot!(response, @r###" + { + "doc_id": 1, + "content": "foo", + "other": "bar" + } + "###); } #[actix_rt::test] diff --git a/meilisearch/tests/dumps/mod.rs b/meilisearch/tests/dumps/mod.rs index 7e426cf54..e60893d4e 100644 --- a/meilisearch/tests/dumps/mod.rs +++ b/meilisearch/tests/dumps/mod.rs @@ -2,10 +2,10 @@ mod data; use meili_snap::{json_string, snapshot}; use meilisearch::Opt; -use serde_json::json; use self::data::GetDump; use crate::common::{default_settings, GetAllDocumentsOptions, Server}; +use crate::json; // all the following test are ignored on windows. See #2364 #[actix_rt::test] diff --git a/meilisearch/tests/features/mod.rs b/meilisearch/tests/features/mod.rs index 045440f49..348deb5e2 100644 --- a/meilisearch/tests/features/mod.rs +++ b/meilisearch/tests/features/mod.rs @@ -1,6 +1,5 @@ -use serde_json::json; - use crate::common::Server; +use crate::json; /// Feature name to test against. /// This will have to be changed by a different one when that feature is stabilized. diff --git a/meilisearch/tests/index/create_index.rs b/meilisearch/tests/index/create_index.rs index 48a07b67f..7ce56d440 100644 --- a/meilisearch/tests/index/create_index.rs +++ b/meilisearch/tests/index/create_index.rs @@ -2,10 +2,10 @@ use actix_web::http::header::ContentType; use actix_web::test; use http::header::ACCEPT_ENCODING; use meili_snap::{json_string, snapshot}; -use serde_json::{json, Value}; use crate::common::encoder::Encoder; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; #[actix_rt::test] async fn create_index_no_primary_key() { @@ -21,7 +21,7 @@ async fn create_index_no_primary_key() { assert_eq!(response["status"], "succeeded"); assert_eq!(response["type"], "indexCreation"); - assert_eq!(response["details"]["primaryKey"], Value::Null); + assert_eq!(response["details"]["primaryKey"], json!(null)); } #[actix_rt::test] @@ -38,7 +38,7 @@ async fn create_index_with_gzip_encoded_request() { assert_eq!(response["status"], "succeeded"); assert_eq!(response["type"], "indexCreation"); - assert_eq!(response["details"]["primaryKey"], Value::Null); + assert_eq!(response["details"]["primaryKey"], json!(null)); } #[actix_rt::test] @@ -86,7 +86,7 @@ async fn create_index_with_zlib_encoded_request() { assert_eq!(response["status"], "succeeded"); assert_eq!(response["type"], "indexCreation"); - assert_eq!(response["details"]["primaryKey"], Value::Null); + assert_eq!(response["details"]["primaryKey"], json!(null)); } #[actix_rt::test] @@ -103,7 +103,7 @@ async fn create_index_with_brotli_encoded_request() { assert_eq!(response["status"], "succeeded"); assert_eq!(response["type"], "indexCreation"); - assert_eq!(response["details"]["primaryKey"], Value::Null); + assert_eq!(response["details"]["primaryKey"], json!(null)); } #[actix_rt::test] @@ -136,7 +136,7 @@ async fn create_index_with_invalid_primary_key() { let (response, code) = index.get().await; assert_eq!(code, 200); - assert_eq!(response["primaryKey"], Value::Null); + assert_eq!(response["primaryKey"], json!(null)); } #[actix_rt::test] diff --git a/meilisearch/tests/index/delete_index.rs b/meilisearch/tests/index/delete_index.rs index b6efc7a68..e404a6003 100644 --- a/meilisearch/tests/index/delete_index.rs +++ b/meilisearch/tests/index/delete_index.rs @@ -1,6 +1,5 @@ -use serde_json::json; - use crate::common::Server; +use crate::json; #[actix_rt::test] async fn create_and_delete_index() { diff --git a/meilisearch/tests/index/errors.rs b/meilisearch/tests/index/errors.rs index ae17a68f1..01273fc88 100644 --- a/meilisearch/tests/index/errors.rs +++ b/meilisearch/tests/index/errors.rs @@ -1,7 +1,7 @@ use meili_snap::*; -use serde_json::json; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn get_indexes_bad_offset() { diff --git a/meilisearch/tests/index/stats.rs b/meilisearch/tests/index/stats.rs index 813f05b4a..d0b0a56b9 100644 --- a/meilisearch/tests/index/stats.rs +++ b/meilisearch/tests/index/stats.rs @@ -1,6 +1,5 @@ -use serde_json::json; - use crate::common::Server; +use crate::json; #[actix_rt::test] async fn stats() { diff --git a/meilisearch/tests/index/update_index.rs b/meilisearch/tests/index/update_index.rs index 3c283407c..36ec27306 100644 --- a/meilisearch/tests/index/update_index.rs +++ b/meilisearch/tests/index/update_index.rs @@ -1,9 +1,9 @@ -use serde_json::json; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; use crate::common::encoder::Encoder; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn update_primary_key() { diff --git a/meilisearch/tests/search/errors.rs b/meilisearch/tests/search/errors.rs index 0745aa7d6..8be70d162 100644 --- a/meilisearch/tests/search/errors.rs +++ b/meilisearch/tests/search/errors.rs @@ -1,8 +1,8 @@ use meili_snap::*; -use serde_json::json; use super::DOCUMENTS; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn search_unexisting_index() { diff --git a/meilisearch/tests/search/facet_search.rs b/meilisearch/tests/search/facet_search.rs index 446396856..1b06f1b98 100644 --- a/meilisearch/tests/search/facet_search.rs +++ b/meilisearch/tests/search/facet_search.rs @@ -1,8 +1,8 @@ use meili_snap::snapshot; use once_cell::sync::Lazy; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; pub(self) static DOCUMENTS: Lazy = Lazy::new(|| { json!([ diff --git a/meilisearch/tests/search/formatted.rs b/meilisearch/tests/search/formatted.rs index 6347a90ca..90c81f949 100644 --- a/meilisearch/tests/search/formatted.rs +++ b/meilisearch/tests/search/formatted.rs @@ -1,8 +1,8 @@ use insta::{allow_duplicates, assert_json_snapshot}; -use serde_json::json; use super::*; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn formatted_contain_wildcard() { diff --git a/meilisearch/tests/search/geo.rs b/meilisearch/tests/search/geo.rs index 5eec5857a..67a4ca7df 100644 --- a/meilisearch/tests/search/geo.rs +++ b/meilisearch/tests/search/geo.rs @@ -1,8 +1,8 @@ use meili_snap::{json_string, snapshot}; use once_cell::sync::Lazy; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; pub(self) static DOCUMENTS: Lazy = Lazy::new(|| { json!([ diff --git a/meilisearch/tests/search/mod.rs b/meilisearch/tests/search/mod.rs index 6fcc33309..43304b223 100644 --- a/meilisearch/tests/search/mod.rs +++ b/meilisearch/tests/search/mod.rs @@ -10,9 +10,9 @@ mod pagination; mod restrict_searchable; use once_cell::sync::Lazy; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; pub(self) static DOCUMENTS: Lazy = Lazy::new(|| { json!([ diff --git a/meilisearch/tests/search/multi.rs b/meilisearch/tests/search/multi.rs index 7cff42f3d..0e2e5158d 100644 --- a/meilisearch/tests/search/multi.rs +++ b/meilisearch/tests/search/multi.rs @@ -1,8 +1,8 @@ use meili_snap::{json_string, snapshot}; -use serde_json::json; use super::{DOCUMENTS, NESTED_DOCUMENTS}; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn search_empty_list() { diff --git a/meilisearch/tests/search/pagination.rs b/meilisearch/tests/search/pagination.rs index bc4a68f98..ff3b50d04 100644 --- a/meilisearch/tests/search/pagination.rs +++ b/meilisearch/tests/search/pagination.rs @@ -1,6 +1,5 @@ -use serde_json::json; - use crate::common::Server; +use crate::json; use crate::search::DOCUMENTS; #[actix_rt::test] diff --git a/meilisearch/tests/search/restrict_searchable.rs b/meilisearch/tests/search/restrict_searchable.rs index 219c747ed..cfdff95ee 100644 --- a/meilisearch/tests/search/restrict_searchable.rs +++ b/meilisearch/tests/search/restrict_searchable.rs @@ -1,9 +1,9 @@ use meili_snap::{json_string, snapshot}; use once_cell::sync::Lazy; -use serde_json::{json, Value}; use crate::common::index::Index; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; async fn index_with_documents<'a>(server: &'a Server, documents: &Value) -> Index<'a> { let index = server.index("test"); diff --git a/meilisearch/tests/settings/distinct.rs b/meilisearch/tests/settings/distinct.rs index a60792329..42c5d38bd 100644 --- a/meilisearch/tests/settings/distinct.rs +++ b/meilisearch/tests/settings/distinct.rs @@ -1,6 +1,5 @@ -use serde_json::json; - use crate::common::Server; +use crate::json; #[actix_rt::test] async fn set_and_reset_distinct_attribute() { diff --git a/meilisearch/tests/settings/errors.rs b/meilisearch/tests/settings/errors.rs index 52dadcf98..687cef1f8 100644 --- a/meilisearch/tests/settings/errors.rs +++ b/meilisearch/tests/settings/errors.rs @@ -1,7 +1,7 @@ use meili_snap::*; -use serde_json::json; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn settings_bad_displayed_attributes() { diff --git a/meilisearch/tests/settings/get_settings.rs b/meilisearch/tests/settings/get_settings.rs index f62957a32..b5c4644a5 100644 --- a/meilisearch/tests/settings/get_settings.rs +++ b/meilisearch/tests/settings/get_settings.rs @@ -1,16 +1,16 @@ use std::collections::HashMap; use once_cell::sync::Lazy; -use serde_json::{json, Value}; -use crate::common::Server; +use crate::common::{Server, Value}; +use crate::json; static DEFAULT_SETTINGS_VALUES: Lazy> = Lazy::new(|| { let mut map = HashMap::new(); map.insert("displayed_attributes", json!(["*"])); map.insert("searchable_attributes", json!(["*"])); map.insert("filterable_attributes", json!([])); - map.insert("distinct_attribute", json!(Value::Null)); + map.insert("distinct_attribute", json!(null)); map.insert( "ranking_rules", json!(["words", "typo", "proximity", "attribute", "sort", "exactness"]), @@ -229,7 +229,7 @@ macro_rules! test_setting_routes { .chars() .map(|c| if c == '_' { '-' } else { c }) .collect::()); - let (response, code) = server.service.$write_method(url, serde_json::Value::Null).await; + let (response, code) = server.service.$write_method(url, serde_json::Value::Null.into()).await; assert_eq!(code, 202, "{}", response); server.index("").wait_task(0).await; let (response, code) = server.index("test").get().await; diff --git a/meilisearch/tests/settings/tokenizer_customization.rs b/meilisearch/tests/settings/tokenizer_customization.rs index fc5d8a880..4602e31f7 100644 --- a/meilisearch/tests/settings/tokenizer_customization.rs +++ b/meilisearch/tests/settings/tokenizer_customization.rs @@ -1,7 +1,7 @@ use meili_snap::{json_string, snapshot}; -use serde_json::json; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn set_and_reset() { diff --git a/meilisearch/tests/snapshot/mod.rs b/meilisearch/tests/snapshot/mod.rs index e88f27b95..be84cc0f3 100644 --- a/meilisearch/tests/snapshot/mod.rs +++ b/meilisearch/tests/snapshot/mod.rs @@ -7,6 +7,7 @@ use meilisearch::Opt; use crate::common::server::default_settings; use crate::common::{GetAllDocumentsOptions, Server}; +use crate::json; macro_rules! verify_snapshot { ( @@ -45,7 +46,7 @@ async fn perform_snapshot() { let index = server.index("test"); index - .update_settings(serde_json::json! ({ + .update_settings(json! ({ "searchableAttributes": [], })) .await; @@ -104,7 +105,7 @@ async fn perform_on_demand_snapshot() { let index = server.index("catto"); index - .update_settings(serde_json::json! ({ + .update_settings(json! ({ "searchableAttributes": [], })) .await; @@ -128,23 +129,15 @@ async fn perform_on_demand_snapshot() { "enqueuedAt": "[date]" } "###); - let task = index.wait_task(3).await; + let task = index.wait_task(4).await; snapshot!(json_string!(task, { ".enqueuedAt" => "[date]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" }), @r###" { - "uid": 3, - "indexUid": "doggo", - "status": "failed", - "type": "indexCreation", + "uid": 4, + "indexUid": null, + "status": "succeeded", + "type": "snapshotCreation", "canceledBy": null, - "details": { - "primaryKey": "bone" - }, - "error": { - "message": "Index `doggo` already exists.", - "code": "index_already_exists", - "type": "invalid_request", - "link": "https://docs.meilisearch.com/errors#index_already_exists" - }, + "error": null, "duration": "[duration]", "enqueuedAt": "[date]", "startedAt": "[date]", diff --git a/meilisearch/tests/stats/mod.rs b/meilisearch/tests/stats/mod.rs index 152e4f625..a02a48a87 100644 --- a/meilisearch/tests/stats/mod.rs +++ b/meilisearch/tests/stats/mod.rs @@ -1,8 +1,8 @@ -use serde_json::json; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn get_settings_unexisting_index() { diff --git a/meilisearch/tests/swap_indexes/errors.rs b/meilisearch/tests/swap_indexes/errors.rs index 03625fd08..d8fa85b3a 100644 --- a/meilisearch/tests/swap_indexes/errors.rs +++ b/meilisearch/tests/swap_indexes/errors.rs @@ -1,7 +1,7 @@ use meili_snap::*; -use serde_json::json; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn swap_indexes_bad_format() { diff --git a/meilisearch/tests/swap_indexes/mod.rs b/meilisearch/tests/swap_indexes/mod.rs index 4d01995b5..7f2b1623c 100644 --- a/meilisearch/tests/swap_indexes/mod.rs +++ b/meilisearch/tests/swap_indexes/mod.rs @@ -1,9 +1,9 @@ mod errors; use meili_snap::{json_string, snapshot}; -use serde_json::json; use crate::common::{GetAllDocumentsOptions, Server}; +use crate::json; #[actix_rt::test] async fn swap_indexes() { diff --git a/meilisearch/tests/tasks/mod.rs b/meilisearch/tests/tasks/mod.rs index 4ac134871..7a5fa6388 100644 --- a/meilisearch/tests/tasks/mod.rs +++ b/meilisearch/tests/tasks/mod.rs @@ -1,11 +1,11 @@ mod errors; use meili_snap::insta::assert_json_snapshot; -use serde_json::json; use time::format_description::well_known::Rfc3339; use time::OffsetDateTime; use crate::common::Server; +use crate::json; #[actix_rt::test] async fn error_get_unexisting_task_status() { @@ -33,7 +33,7 @@ async fn get_task_status() { index.create(None).await; index .add_documents( - serde_json::json!([{ + json!([{ "id": 1, "content": "foobar", }]),