Skip to content

Commit

Permalink
Add simple test to ensure the serde bits handle strings
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceg committed Aug 26, 2024
1 parent c7d7729 commit b840a8e
Showing 1 changed file with 31 additions and 5 deletions.
36 changes: 31 additions & 5 deletions src/datadog/search/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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),
Expand Down Expand Up @@ -81,14 +81,14 @@ impl<T: AsRef<str>> From<T> 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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -388,3 +388,29 @@ static ESCAPE_RE: Lazy<Regex> = Lazy::new(|| Regex::new("^\"(.+)\"$").unwrap());
fn escape_quotes<T: AsRef<str>>(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::<QueryNode>(r#""_missing_:something_else""#).unwrap(),
QueryNode::AttributeMissing {
attr: "something_else".into()
}
);
}
}

0 comments on commit b840a8e

Please sign in to comment.