refactor: remove code duplication for simple_event

This commit is contained in:
benthecarman 2022-12-21 01:59:04 -06:00 committed by Greg Heartsfield
parent ae0f7171ed
commit e08647867c
2 changed files with 24 additions and 34 deletions

View File

@ -332,19 +332,6 @@ mod tests {
assert_eq!(parsed, cq); assert_eq!(parsed, cq);
Ok(()) Ok(())
} }
fn simple_event() -> Event {
Event {
id: "0".to_owned(),
pubkey: "0".to_owned(),
delegated_by: None,
created_at: 0,
kind: 0,
tags: vec![],
content: "".to_owned(),
sig: "0".to_owned(),
tagidx: None,
}
}
// Check for condition logic on event w/ empty values // Check for condition logic on event w/ empty values
#[test] #[test]
fn condition_with_empty_values() { fn condition_with_empty_values() {
@ -353,7 +340,7 @@ mod tests {
operator: Operator::GreaterThan, operator: Operator::GreaterThan,
values: vec![], values: vec![],
}; };
let e = simple_event(); let e = Event::simple_event();
assert!(!c.allows_event(&e)); assert!(!c.allows_event(&e));
c.operator = Operator::LessThan; c.operator = Operator::LessThan;
assert!(!c.allows_event(&e)); assert!(!c.allows_event(&e));
@ -373,7 +360,7 @@ mod tests {
operator: Operator::GreaterThan, operator: Operator::GreaterThan,
values: vec![10], values: vec![10],
}; };
let mut e = simple_event(); let mut e = Event::simple_event();
// kind is not greater than 10, not allowed // kind is not greater than 10, not allowed
e.kind = 1; e.kind = 1;
assert!(!c.allows_event(&e)); assert!(!c.allows_event(&e));
@ -392,7 +379,7 @@ mod tests {
operator: Operator::Equals, operator: Operator::Equals,
values: vec![0, 10, 20], values: vec![0, 10, 20],
}; };
let mut e = simple_event(); let mut e = Event::simple_event();
// Allow if event kind is in list for Equals // Allow if event kind is in list for Equals
e.kind = 10; e.kind = 10;
assert!(c.allows_event(&e)); assert!(c.allows_event(&e));

View File

@ -101,6 +101,22 @@ impl From<EventCmd> for Result<Event> {
} }
impl Event { impl Event {
#[cfg(test)]
pub fn simple_event() -> Event {
Event {
id: "0".to_owned(),
pubkey: "0".to_owned(),
delegated_by: None,
created_at: 0,
kind: 0,
tags: vec![],
content: "".to_owned(),
sig: "0".to_owned(),
tagidx: None,
}
}
pub fn is_kind_metadata(&self) -> bool { pub fn is_kind_metadata(&self) -> bool {
self.kind == 0 self.kind == 0
} }
@ -319,31 +335,18 @@ impl Event {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
fn simple_event() -> Event {
Event {
id: "0".to_owned(),
pubkey: "0".to_owned(),
delegated_by: None,
created_at: 0,
kind: 0,
tags: vec![],
content: "".to_owned(),
sig: "0".to_owned(),
tagidx: None,
}
}
#[test] #[test]
fn event_creation() { fn event_creation() {
// create an event // create an event
let event = simple_event(); let event = Event::simple_event();
assert_eq!(event.id, "0"); assert_eq!(event.id, "0");
} }
#[test] #[test]
fn event_serialize() -> Result<()> { fn event_serialize() -> Result<()> {
// serialize an event to JSON string // serialize an event to JSON string
let event = simple_event(); let event = Event::simple_event();
let j = serde_json::to_string(&event)?; let j = serde_json::to_string(&event)?;
assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}"); assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}");
Ok(()) Ok(())
@ -351,14 +354,14 @@ mod tests {
#[test] #[test]
fn empty_event_tag_match() { fn empty_event_tag_match() {
let event = simple_event(); let event = Event::simple_event();
assert!(!event assert!(!event
.generic_tag_val_intersect('e', &HashSet::from(["foo".to_owned(), "bar".to_owned()]))); .generic_tag_val_intersect('e', &HashSet::from(["foo".to_owned(), "bar".to_owned()])));
} }
#[test] #[test]
fn single_event_tag_match() { fn single_event_tag_match() {
let mut event = simple_event(); let mut event = Event::simple_event();
event.tags = vec![vec!["e".to_owned(), "foo".to_owned()]]; event.tags = vec![vec!["e".to_owned(), "foo".to_owned()]];
event.build_index(); event.build_index();
assert_eq!( assert_eq!(
@ -373,7 +376,7 @@ mod tests {
#[test] #[test]
fn event_tags_serialize() -> Result<()> { fn event_tags_serialize() -> Result<()> {
// serialize an event with tags to JSON string // serialize an event with tags to JSON string
let mut event = simple_event(); let mut event = Event::simple_event();
event.tags = vec![ event.tags = vec![
vec![ vec![
"e".to_owned(), "e".to_owned(),