From b840a8e6e210309a4fcd70ca5bc1cce10c98b0cf Mon Sep 17 00:00:00 2001 From: Bruce Guenter Date: Mon, 26 Aug 2024 08:53:01 -0600 Subject: [PATCH] Add simple test to ensure the serde bits handle strings --- src/datadog/search/node.rs | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/src/datadog/search/node.rs b/src/datadog/search/node.rs index f2783971f..0d2f9048d 100644 --- a/src/datadog/search/node.rs +++ b/src/datadog/search/node.rs @@ -5,7 +5,7 @@ use serde::{Deserialize, Deserializer, Serialize, Serializer}; use super::grammar::{unescape, DEFAULT_FIELD}; /// This enum represents value comparisons that Queries might perform -#[derive(Debug, Copy, Clone)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Comparison { /// Greater than. Gt, @@ -32,7 +32,7 @@ impl Comparison { /// This enum represents the values we might be using in a comparison, whether /// they are Strings, Numbers (currently only floating point numbers) or an /// Unbounded comparison with no terminating value. -#[derive(Debug, Clone)] +#[derive(Clone, Debug, PartialEq)] pub enum ComparisonValue { Unbounded, String(String), @@ -81,14 +81,14 @@ impl> From for ComparisonValue { /// This enum represents the tokens in a range, including "greater than (or equal to)" /// for the left bracket, "less than (or equal to) in the right bracket, and range values. -#[derive(Debug, Clone)] +#[derive(Clone, Debug, PartialEq)] pub enum Range { Comparison(Comparison), Value(ComparisonValue), } /// This enum represents the AND or OR Boolean operations we might perform on QueryNodes. -#[derive(Debug, Copy, Clone)] +#[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum BooleanType { And, Or, @@ -134,7 +134,7 @@ impl BooleanBuilder { } /// QueryNodes represent specific search criteria to be enforced. -#[derive(Debug, Clone)] +#[derive(Clone, Debug, PartialEq)] pub enum QueryNode { /// Match all documents. MatchAllDocs, @@ -388,3 +388,29 @@ static ESCAPE_RE: Lazy = Lazy::new(|| Regex::new("^\"(.+)\"$").unwrap()); fn escape_quotes>(value: T) -> String { ESCAPE_RE.replace_all(value.as_ref(), "$1").to_string() } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn query_node_serializes_to_string() { + assert_eq!( + serde_json::to_string(&QueryNode::AttributeExists { + attr: "something".into() + }) + .unwrap(), + r#""_exists_:something""# + ); + } + + #[test] + fn query_node_deserializes_from_string() { + assert_eq!( + serde_json::from_str::(r#""_missing_:something_else""#).unwrap(), + QueryNode::AttributeMissing { + attr: "something_else".into() + } + ); + } +}