Skip to content

Commit

Permalink
Merge pull request #392 from CosmWasm/event-compare-helper
Browse files Browse the repository at this point in the history
Add helpers to check events
  • Loading branch information
ethanfrey authored Aug 26, 2021
2 parents 6084e55 + 929a43a commit 3539081
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 37 deletions.
56 changes: 19 additions & 37 deletions packages/multi-test/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -717,45 +717,27 @@ 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(
Expand Down Expand Up @@ -1192,16 +1174,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]
Expand Down
24 changes: 24 additions & 0 deletions packages/multi-test/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,30 @@ 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.
pub fn has_event(&self, expected: &Event) -> bool {
self.events.iter().any(|ev| {
expected.ty == ev.ty
&& expected
.attributes
.iter()
.all(|at| ev.attributes.contains(at))
})
}

/// Like has_event but panics if no match
#[track_caller]
pub fn assert_event(&self, expected: &Event) {
assert!(
self.has_event(expected),
"Expected to find an event {:?}, but received: {:?}",
expected,
self.events
);
}
}

pub trait Executor<C>
Expand Down

0 comments on commit 3539081

Please sign in to comment.