From e08647867c06a9e79b4d363c4fcab1f87e7181fe Mon Sep 17 00:00:00 2001 From: benthecarman Date: Wed, 21 Dec 2022 01:59:04 -0600 Subject: [PATCH] refactor: remove code duplication for simple_event --- src/delegation.rs | 19 +++---------------- src/event.rs | 39 +++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 34 deletions(-) diff --git a/src/delegation.rs b/src/delegation.rs index 2b056be..fe2f805 100644 --- a/src/delegation.rs +++ b/src/delegation.rs @@ -332,19 +332,6 @@ mod tests { assert_eq!(parsed, cq); 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 #[test] fn condition_with_empty_values() { @@ -353,7 +340,7 @@ mod tests { operator: Operator::GreaterThan, values: vec![], }; - let e = simple_event(); + let e = Event::simple_event(); assert!(!c.allows_event(&e)); c.operator = Operator::LessThan; assert!(!c.allows_event(&e)); @@ -373,7 +360,7 @@ mod tests { operator: Operator::GreaterThan, values: vec![10], }; - let mut e = simple_event(); + let mut e = Event::simple_event(); // kind is not greater than 10, not allowed e.kind = 1; assert!(!c.allows_event(&e)); @@ -392,7 +379,7 @@ mod tests { operator: Operator::Equals, 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 e.kind = 10; assert!(c.allows_event(&e)); diff --git a/src/event.rs b/src/event.rs index 11e219b..84229bd 100644 --- a/src/event.rs +++ b/src/event.rs @@ -101,6 +101,22 @@ impl From for Result { } 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 { self.kind == 0 } @@ -319,31 +335,18 @@ impl Event { #[cfg(test)] mod tests { 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] fn event_creation() { // create an event - let event = simple_event(); + let event = Event::simple_event(); assert_eq!(event.id, "0"); } #[test] fn event_serialize() -> Result<()> { // serialize an event to JSON string - let event = simple_event(); + let event = Event::simple_event(); let j = serde_json::to_string(&event)?; assert_eq!(j, "{\"id\":\"0\",\"pubkey\":\"0\",\"created_at\":0,\"kind\":0,\"tags\":[],\"content\":\"\",\"sig\":\"0\"}"); Ok(()) @@ -351,14 +354,14 @@ mod tests { #[test] fn empty_event_tag_match() { - let event = simple_event(); + let event = Event::simple_event(); assert!(!event .generic_tag_val_intersect('e', &HashSet::from(["foo".to_owned(), "bar".to_owned()]))); } #[test] 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.build_index(); assert_eq!( @@ -373,7 +376,7 @@ mod tests { #[test] fn event_tags_serialize() -> Result<()> { // serialize an event with tags to JSON string - let mut event = simple_event(); + let mut event = Event::simple_event(); event.tags = vec![ vec![ "e".to_owned(),