Skip to content

Commit

Permalink
Filter tests (estk#357)
Browse files Browse the repository at this point in the history
* chore: add filter tests
  • Loading branch information
bconn98 authored Mar 3, 2024
1 parent bcd7fb0 commit 8ab1b34
Show file tree
Hide file tree
Showing 3 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ streaming-stats = "0.2.3"
humantime = "2.1"
tempfile = "3.8"
mock_instant = "0.3"
serde_test = "1.0.176"

[[example]]
name = "json_logger"
Expand Down
42 changes: 42 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ impl Deserializable for dyn Filter {
}
}

#[derive(PartialEq, Debug)]
/// The response returned by a filter.
pub enum Response {
/// Accept the log event.
Expand Down Expand Up @@ -78,3 +79,44 @@ impl<'de> de::Deserialize<'de> for FilterConfig {
})
}
}

#[cfg(test)]
mod test {
#[cfg(feature = "config_parsing")]
use super::*;
#[cfg(feature = "config_parsing")]
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};

#[test]
#[cfg(feature = "config_parsing")]
fn test_cfg_deserializer() {
let filter = FilterConfig {
kind: "threshold".to_owned(),
config: Value::Map({
let mut map = BTreeMap::new();
map.insert(
Value::String("level".to_owned()),
Value::String("error".to_owned()),
);
map
}),
};

let mut cfg = vec![
Token::Struct {
name: "FilterConfig",
len: 2,
},
Token::Str("kind"),
Token::Str("threshold"),
Token::Str("level"),
Token::Str("error"),
Token::StructEnd,
];

assert_de_tokens(&filter, &cfg);

cfg[1] = Token::Str("knd");
assert_de_tokens_error::<FilterConfig>(&cfg, "missing field `kind`");
}
}
95 changes: 95 additions & 0 deletions src/filter/threshold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,98 @@ impl Deserialize for ThresholdFilterDeserializer {
Ok(Box::new(ThresholdFilter::new(config.level)))
}
}

#[cfg(test)]
mod test {
use log::{Level, LevelFilter, Record};

use super::*;

#[cfg(feature = "config_parsing")]
use crate::config::Deserializers;

#[cfg(feature = "config_parsing")]
use serde_test::{assert_de_tokens, assert_de_tokens_error, Token};

#[test]
#[cfg(feature = "config_parsing")]
fn test_cfg_deserialize() {
let filter_cfg = ThresholdFilterConfig {
level: LevelFilter::Off,
};

let mut cfg = vec![
Token::Struct {
name: "ThresholdFilterConfig",
len: 1,
},
Token::Str("level"),
Token::Enum {
name: "LevelFilter",
},
Token::Str("Off"),
Token::Unit,
Token::StructEnd,
];

assert_de_tokens(&filter_cfg, &cfg);

cfg[1] = Token::Str("leel");
assert_de_tokens_error::<ThresholdFilterConfig>(&cfg, "missing field `level`");

cfg[1] = Token::Str("level");
cfg[3] = Token::Str("On");
cfg.remove(4); // No Unit on this one as the Option is invalid
assert_de_tokens_error::<ThresholdFilterConfig>(
&cfg,
"unknown variant `On`, expected one of `OFF`, `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`",
);
}

#[test]
fn test_filter_new_vs_struct() {
assert_eq!(
ThresholdFilter::new(LevelFilter::Info),
ThresholdFilter {
level: LevelFilter::Info
}
);
}

#[test]
fn test_threshold_filter() {
let thres = ThresholdFilter::new(LevelFilter::Info);
let debug_record = Record::builder()
.level(Level::Debug)
.args(format_args!("the message"))
.module_path(Some("path"))
.file(Some("file"))
.line(Some(132))
.build();

assert_eq!(thres.filter(&debug_record), Response::Reject);

let error_record = Record::builder()
.level(Level::Error)
.args(format_args!("the message"))
.module_path(Some("path"))
.file(Some("file"))
.line(Some(132))
.build();

assert_eq!(thres.filter(&error_record), Response::Neutral);
}

#[test]
#[cfg(feature = "config_parsing")]
fn test_cfg_deserializer() {
let filter_cfg = ThresholdFilterConfig {
level: LevelFilter::Off,
};

let deserializer = ThresholdFilterDeserializer;

let res = deserializer.deserialize(filter_cfg, &Deserializers::default());
assert!(res.is_ok());
}
}

0 comments on commit 8ab1b34

Please sign in to comment.