MeiliSearch/meilisearch/tests/common/mod.rs

108 lines
2.9 KiB
Rust
Raw Normal View History

pub mod encoder;
2021-03-24 11:03:01 +01:00
pub mod index;
pub mod server;
pub mod service;
2021-02-18 19:50:52 +01:00
2023-09-11 16:50:53 +02:00
use std::fmt::{self, Display};
2023-10-30 11:00:46 +01:00
#[allow(unused)]
2021-03-15 18:11:10 +01:00
pub use index::{GetAllDocumentsOptions, GetDocumentOptions};
2023-09-11 16:50:53 +02:00
use meili_snap::json_string;
use serde::{Deserialize, Serialize};
2023-10-30 11:00:46 +01:00
#[allow(unused)]
2022-04-28 10:48:57 +02:00
pub use server::{default_settings, Server};
2021-02-18 19:50:52 +01:00
2023-09-11 16:50:53 +02:00
#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct Value(pub serde_json::Value);
impl Value {
#[track_caller]
2023-09-11 16:50:53 +02:00
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}");
}
}
}
2023-09-11 17:02:01 +02:00
impl From<serde_json::Value> for Value {
fn from(value: serde_json::Value) -> Self {
Value(value)
2023-09-11 16:50:53 +02:00
}
}
impl std::ops::Deref for Value {
type Target = serde_json::Value;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl std::ops::DerefMut for Value {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
2023-09-11 16:50:53 +02:00
impl PartialEq<serde_json::Value> for Value {
fn eq(&self, other: &serde_json::Value) -> bool {
&self.0 == other
}
}
impl PartialEq<Value> 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]", ".startedAt" => "[date]", ".finishedAt" => "[date]", ".duration" => "[duration]" })
2023-09-11 16:50:53 +02:00
)
}
}
impl From<Vec<Value>> for Value {
fn from(value: Vec<Value>) -> Self {
Self(value.into_iter().map(|value| value.0).collect::<serde_json::Value>())
}
}
#[macro_export]
macro_rules! json {
($($json:tt)+) => {
$crate::common::Value(serde_json::json!($($json)+))
};
}
2021-02-18 19:50:52 +01:00
/// Performs a search test on both post and get routes
#[macro_export]
macro_rules! test_post_get_search {
($server:expr, $query:expr, |$response:ident, $status_code:ident | $block:expr) => {
let post_query: meilisearch::routes::search::SearchQueryPost =
2021-02-18 19:50:52 +01:00
serde_json::from_str(&$query.clone().to_string()).unwrap();
let get_query: meilisearch::routes::search::SearchQuery = post_query.into();
2021-02-18 19:50:52 +01:00
let get_query = ::serde_url_params::to_string(&get_query).unwrap();
let ($response, $status_code) = $server.search_get(&get_query).await;
2022-10-20 18:00:07 +02:00
let _ = ::std::panic::catch_unwind(|| $block)
.map_err(|e| panic!("panic in get route: {:?}", e.downcast_ref::<&str>().unwrap()));
2021-02-18 19:50:52 +01:00
let ($response, $status_code) = $server.search_post($query).await;
2022-10-20 18:00:07 +02:00
let _ = ::std::panic::catch_unwind(|| $block)
.map_err(|e| panic!("panic in post route: {:?}", e.downcast_ref::<&str>().unwrap()));
2021-02-18 19:50:52 +01:00
};
}