From c8e428a6b43573190187d3a0263dcfe5a8a3e2c1 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 25 Aug 2021 20:22:08 +0200 Subject: [PATCH 1/4] Add helpers to check events --- packages/multi-test/src/executor.rs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/packages/multi-test/src/executor.rs b/packages/multi-test/src/executor.rs index 5bc56cecb..365b65f48 100644 --- a/packages/multi-test/src/executor.rs +++ b/packages/multi-test/src/executor.rs @@ -21,6 +21,26 @@ impl AppResponse { assert_eq!(self.events[idx].ty.as_str(), "wasm"); &self.events[idx].attributes[1..] } + + /// Check if there is an Event that is a super-set of this. + /// It has the same type, and all compare.attributes are included in it as well. + /// You don't need to specify them all. + #[track_caller] + pub fn has_event(&self, compare: Event) -> bool { + self.events.iter().any(|ev| { + compare.ty == ev.ty + && compare + .attributes + .iter() + .all(|cat| ev.attributes.iter().any(|evat| cat == evat)) + }) + } + + /// Like has_event but panics if no match + #[track_caller] + pub fn assert_event(&self, compare: Event) { + assert!(self.has_event(compare)); + } } pub trait Executor From 0d8f8e038a3dc757db53dcc18756373dbf07ea36 Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Wed, 25 Aug 2021 22:28:51 +0200 Subject: [PATCH 2/4] Update with PR comments --- packages/multi-test/src/executor.rs | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/packages/multi-test/src/executor.rs b/packages/multi-test/src/executor.rs index 365b65f48..45d8fee2a 100644 --- a/packages/multi-test/src/executor.rs +++ b/packages/multi-test/src/executor.rs @@ -25,21 +25,25 @@ impl AppResponse { /// Check if there is an Event that is a super-set of this. /// It has the same type, and all compare.attributes are included in it as well. /// You don't need to specify them all. - #[track_caller] - pub fn has_event(&self, compare: Event) -> bool { + pub fn has_event(&self, expected: &Event) -> bool { self.events.iter().any(|ev| { - compare.ty == ev.ty - && compare + expected.ty == ev.ty + && expected .attributes .iter() - .all(|cat| ev.attributes.iter().any(|evat| cat == evat)) + .all(|at| ev.attributes.contains(at)) }) } /// Like has_event but panics if no match #[track_caller] - pub fn assert_event(&self, compare: Event) { - assert!(self.has_event(compare)); + pub fn assert_event(&self, expected: &Event) { + assert!( + self.has_event(expected), + "Expected to find an event {:?}, but received: {:?}", + expected, + self.events + ); } } From 9c4c5d294dc46d312e916f29028eda3300e4ee2e Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 26 Aug 2021 10:12:58 +0200 Subject: [PATCH 3/4] Use assert_event in some tests --- packages/multi-test/src/app.rs | 54 +++++++++++----------------------- 1 file changed, 17 insertions(+), 37 deletions(-) diff --git a/packages/multi-test/src/app.rs b/packages/multi-test/src/app.rs index f81e91b45..64a237c1f 100644 --- a/packages/multi-test/src/app.rs +++ b/packages/multi-test/src/app.rs @@ -717,45 +717,25 @@ mod test { // expected events: execute, transfer, reply, custom wasm (set in reply) assert_eq!(4, res.events.len(), "{:?}", res.events); - let first = &res.events[0]; - assert_eq!(first.ty.as_str(), "execute"); - assert_eq!(first.attributes, [("_contract_addr", &reflect_addr)]); - - // next event is the transfer from bank - let transfer = &res.events[1]; - assert_eq!(transfer.ty.as_str(), "transfer"); - - // then we get notification reply was called - let reply = &res.events[2]; - assert_eq!(reply.ty.as_str(), "reply"); - assert_eq!( - reply.attributes, - [ - ("_contract_addr", reflect_addr.as_str()), - ("mode", "handle_success") - ] - ); - - // the last one is a custom event (from reply) - let custom = &res.events[3]; - assert_eq!("wasm-custom", custom.ty.as_str()); - assert_eq!( - custom.attributes, - [ - // TODO - ("_contract_addr", reflect_addr.as_str()), - ("from", "reply"), - ("to", "test") - ] + res.assert_event(&Event::new("execute").add_attribute("_contract_addr", &reflect_addr)); + res.assert_event(&Event::new("transfer").add_attribute("amount", "7eth")); + res.assert_event( + &Event::new("reply") + .add_attribute("_contract_addr", reflect_addr.as_str()) + .add_attribute("mode", "handle_success"), ); + res.assert_event(&Event::new("wasm-custom").add_attribute("from", "reply")); // ensure success was written let res: Reply = app.wrap().query_wasm_smart(&reflect_addr, &query).unwrap(); assert_eq!(res.id, 123); // validate the events written in the reply blob...should just be bank transfer - let reply_events = res.result.unwrap().events; - assert_eq!(1, reply_events.len()); - assert_eq!("transfer", &reply_events[0].ty); + let reply = res.result.unwrap(); + assert_eq!(1, reply.events.len()); + // Note: this shows we must make the helpers more generic + let app_res = AppResponse{ events: reply.events, data: reply.data}; + app_res + .assert_event(&Event::new("transfer").add_attribute("amount", "7eth")); // reflect sends 300 btc, failure, but error caught by submessage (so shows success) let msg = SubMsg::reply_always( @@ -1192,16 +1172,16 @@ mod test { }; let res = app - .execute_contract(owner, reflect_addr, &reflect_msg, &[]) + .execute_contract(owner, reflect_addr.clone(), &reflect_msg, &[]) .unwrap(); // ensure data is empty assert_eq!(res.data, None); // ensure expected events assert_eq!(res.events.len(), 3, "{:?}", res.events); - assert_eq!("execute", &res.events[0].ty); - assert_eq!("execute", &res.events[1].ty); - assert_eq!("wasm-echo", &res.events[2].ty); + res.assert_event(&Event::new("execute").add_attribute("_contract_addr", &reflect_addr)); + res.assert_event(&Event::new("execute").add_attribute("_contract_addr", &echo_addr)); + res.assert_event(&Event::new("wasm-echo")); } #[test] From 929a43a62bc1f9b77fd1781153293642d8ea609a Mon Sep 17 00:00:00 2001 From: Ethan Frey Date: Thu, 26 Aug 2021 11:04:30 +0200 Subject: [PATCH 4/4] cargo fmt --- packages/multi-test/src/app.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/multi-test/src/app.rs b/packages/multi-test/src/app.rs index 64a237c1f..0f5f1a242 100644 --- a/packages/multi-test/src/app.rs +++ b/packages/multi-test/src/app.rs @@ -733,9 +733,11 @@ mod test { let reply = res.result.unwrap(); assert_eq!(1, reply.events.len()); // Note: this shows we must make the helpers more generic - let app_res = AppResponse{ events: reply.events, data: reply.data}; - app_res - .assert_event(&Event::new("transfer").add_attribute("amount", "7eth")); + let app_res = AppResponse { + events: reply.events, + data: reply.data, + }; + app_res.assert_event(&Event::new("transfer").add_attribute("amount", "7eth")); // reflect sends 300 btc, failure, but error caught by submessage (so shows success) let msg = SubMsg::reply_always(