From acd5cc7f4235852f81a3d58a8e3cf6e146cc081f Mon Sep 17 00:00:00 2001 From: David Estes Date: Wed, 26 Jun 2024 17:13:37 -0600 Subject: [PATCH] refactor: extract some test code into functions for clarity --- service/src/event/order_events.rs | 83 ++++++++++++++++--------------- 1 file changed, 43 insertions(+), 40 deletions(-) diff --git a/service/src/event/order_events.rs b/service/src/event/order_events.rs index f2102d0c6..c28c4f2f7 100644 --- a/service/src/event/order_events.rs +++ b/service/src/event/order_events.rs @@ -163,6 +163,43 @@ mod test { (stream_1, stream_2, to_insert) } + /// Asserts the events are deliverable and returns IDs for events in stream_1 as the first value and things in stream_2 as the second + fn split_deliverable_order_by_stream( + stream_1: &[(EventId, Vec)], + stream_2: &[(EventId, Vec)], + events: &[(EventInsertable, EventMetadata)], + ) -> (Vec, Vec) { + let mut after_1 = Vec::with_capacity(stream_1.len()); + let mut after_2 = Vec::with_capacity(stream_2.len()); + for (event, _) in events { + assert!(event.deliverable()); + if stream_1.iter().any(|e| e.0 == event.order_key) { + after_1.push(event.order_key.clone()); + } else { + after_2.push(event.order_key.clone()); + } + } + + (after_1, after_2) + } + + async fn get_insertable_events( + events: &[(EventId, Vec)], + ) -> Vec<(EventInsertable, EventMetadata)> { + let mut insertable = Vec::with_capacity(events.len()); + for event in events { + let new = CeramicEventService::validate_discovered_event( + event.0.to_owned(), + event.1.as_slice(), + ) + .await + .unwrap(); + insertable.push(new); + } + + insertable + } + #[test(tokio::test)] async fn out_of_order_streams_valid() { let pool = SqlitePool::connect_in_memory().await.unwrap(); @@ -177,16 +214,8 @@ mod test { ordered.missing_history.len(), ordered.missing_history ); - let mut after_1 = Vec::with_capacity(10); - let mut after_2 = Vec::with_capacity(10); - for (event, _) in ordered.deliverable { - assert!(event.deliverable()); - if stream_1.iter().any(|e| e.0 == event.order_key) { - after_1.push(event.order_key.clone()); - } else { - after_2.push(event.order_key.clone()); - } - } + let (after_1, after_2) = + split_deliverable_order_by_stream(&stream_1, &stream_2, ordered.deliverable()); assert_eq!( stream_1.into_iter().map(|e| e.0).collect::>(), @@ -214,16 +243,8 @@ mod test { "Missing history: {:?}", ordered.missing_history ); - let mut after_1 = Vec::with_capacity(10); - let mut after_2 = Vec::with_capacity(10); - for (event, _) in ordered.deliverable { - assert!(event.deliverable()); - if stream_1.iter().any(|e| e.0 == event.order_key) { - after_1.push(event.order_key.clone()); - } else { - after_2.push(event.order_key.clone()); - } - } + let (after_1, after_2) = + split_deliverable_order_by_stream(&stream_1, &stream_2, ordered.deliverable()); assert_eq!(vec![stream_1[0].0.clone()], after_1); assert_eq!( @@ -240,16 +261,7 @@ mod test { let pool = SqlitePool::connect_in_memory().await.unwrap(); let stream_1 = get_n_events(10).await; - let mut insertable = Vec::with_capacity(10); - for event in stream_1.iter() { - let new = CeramicEventService::validate_discovered_event( - event.0.to_owned(), - event.1.as_slice(), - ) - .await - .unwrap(); - insertable.push(new); - } + let insertable = get_insertable_events(&stream_1).await; let to_insert = insertable .iter() .take(3) @@ -278,16 +290,7 @@ mod test { let pool = SqlitePool::connect_in_memory().await.unwrap(); let stream_1 = get_n_events(10).await; - let mut insertable = Vec::with_capacity(10); - for event in stream_1.iter() { - let new = CeramicEventService::validate_discovered_event( - event.0.to_owned(), - event.1.as_slice(), - ) - .await - .unwrap(); - insertable.push(new); - } + let mut insertable = get_insertable_events(&stream_1).await; let to_insert = insertable .iter_mut() .take(3)