-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Properly handling data in submessages in multi-test #369
Conversation
a289ed3
to
2eedb20
Compare
Events get appended (they should already!). Data may get overwritten if set to Some(_). Only the Response of I would merge this (once linting fixed), and I'd be happy to see your echo contract used to test all events (+attributed) being properly propagated up. If they don't, then yes, please fix that as well. (That can be another PR - test and possibly fix events) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great stuff.
I'd just ask you to remove the unneeded Binary
in Response<Binary>
, SubMsg<Binary>
for echo. We may need better docs to explain when you need that T.
packages/multi-test/src/app.rs
Outdated
contract: Addr, | ||
data: impl Into<Option<&'static str>>, | ||
sub_msg: Vec<SubMsg<Binary>>, | ||
) -> SubMsg<Binary> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need SubMsg<Binary>
?
They type T
here just controls the type of CosmosMsg::Custom variant, so we can compile with code that uses it. We return an error if anyone actually returns those variants in multi-test (this is a future TODO to support it)
_env: Env, | ||
_info: MessageInfo, | ||
_msg: EmptyMsg, | ||
) -> Result<Response<Binary>, StdError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Commented above, but I am quite sure there is no need for Response<Binary>
unless you do something like:
let msg = CosmosMsg::Custom(Binary::from(b"foo"));
res.add_message(msg);
The type here is unused unless you use some blockchain-specific custom return types. Which is only the case for tgrade-specific contracts (PoE), terra-specific contracts, etc. Not for any tests. Multi-test does not support those messages when returned
msg: Message, | ||
) -> Result<Response<Binary>, StdError> { | ||
let mut resp = Response::new(); | ||
if let Some(data) = msg.data { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nice logic
to_binary(&msg) | ||
} | ||
|
||
fn reply(_deps: DepsMut, _env: Env, msg: Reply) -> Result<Response<Binary>, StdError> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And this will re-play whatever reply data came from the submsg. Nice for tests
let data = response | ||
.messages | ||
.into_iter() | ||
.try_fold(response.data, |data, resend| { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting rework of loop to functional, but looks good
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I could do that or introduce mutable variable which is updated. I find folding way more readable that mutating, as if you understand folding, nothing surprises you. Analyze mutation always requires you to carefully read the loop. Otherwise I won't bother changing it.
packages/multi-test/src/app.rs
Outdated
) | ||
.unwrap(); | ||
|
||
assert_eq!(response.data, Some("Data".to_owned().into_bytes().into())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is some chain... Doesn't it work without the to_owned
?
Or "Data".to_bytes().into()
?
packages/multi-test/src/app.rs
Outdated
) | ||
.unwrap(); | ||
|
||
assert_eq!(response.data, Some("Second".to_owned().into_bytes().into())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice test case. Very correct
packages/multi-test/src/app.rs
Outdated
) | ||
.unwrap(); | ||
|
||
assert_eq!(response.data, Some("First".to_owned().into_bytes().into())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No overwrite on None. 👍
&echo::Message { | ||
data: Some("Orig".to_owned()), | ||
sub_msg: vec![ | ||
make_echo_submsg(contract.clone(), None, vec![]), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very cool test case for the fold
contract.clone(), | ||
&echo::Message { | ||
data: Some("Orig".to_owned()), | ||
sub_msg: vec![make_echo_submsg( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another great test case.
I had to read it twice to ensure the final result made sense, but yes, very correct
2eedb20
to
6b52132
Compare
Nice updates |
Closes #352
I am not entirely sure - the issue mentions only data, but I am pretty sure that events from sub messages should also be merged (however I can misunderstand something) - as this would be similar and simple change, I think I can add it to the same PR.