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
[feature] #2053: Add tests to 'private_blockchain' #2332
[feature] #2053: Add tests to 'private_blockchain' #2332
Conversation
5cca2c0
to
8d8e9bc
Compare
Codecov Report
@@ Coverage Diff @@
## iroha2-dev #2332 +/- ##
==============================================
+ Coverage 65.50% 67.02% +1.52%
==============================================
Files 133 140 +7
Lines 24697 26282 +1585
==============================================
+ Hits 16177 17616 +1439
- Misses 8520 8666 +146
Continue to review full report at Codecov.
|
8d8e9bc
to
5d2d14a
Compare
|
||
use super::*; | ||
|
||
type TestEnv = ( |
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.
Doesn't matter here, but this should be a struct
. You can bind directly to names using Rust's pattern matching syntax.
f762fc5
to
636c667
Compare
|
||
{ | ||
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.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.
There should be a negative test-case (with is_ok()
).
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.
Can't allow to find any accounts with OnlyAccountsDomain
. That's why there are no is_ok()
cases here.
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 not to use another query to check is_ok()
variant?
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.
Oh I see, it tests queries, not validators
} | ||
|
||
impl TestEnv { | ||
fn new() -> Self { |
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.
With rust 1.62, we should be able to turn this into a const
function.
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'd remove the comments above blocks (but keep the grouping). Instead I'd add a doc-comment above the function, so people could hover and see both the assets, the domains and the relationships.
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.
With rust 1.62, we should be able to turn this into a
const
function.
It is not possible to turn fn new()
into a const fn
because the functions called inside fn new()
are not const
.
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.
Yes. const new for some types is going to be stabilised soon, so it should be possible to propagate those changes to here.
|
||
let wsv = WorldStateView::<World>::new(World::with( | ||
[first_domain.clone(), second_domain.clone()], | ||
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.
Style point: prefer Vec::new()
for empty immutable vectors. This conveys to the reader that what you made empty is meant to remain empty.
|
||
{ | ||
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.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.
This is an example of a good assertion block. You need to be able to demonstrate when it works and when it doesn't.
.. | ||
} = TestEnv::new(); | ||
|
||
let op1 = QueryBox::FindAssetById(FindAssetById::new(gold_asset_id)); |
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'd give these operations more descriprtive names. e.g. find_gold
, find_bronze
etc.
assert!(only_accounts_data.check(&alice_id, &op3, &wsv).is_err()); | ||
} | ||
} | ||
|
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.
Group the tests that check the queries into mod queries
. It should end about here.
Also add mod revoke_and_grant_tests
and include a few of the following tests.
} | ||
|
||
#[test] | ||
fn granted_allowed_register_domains_success() { |
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'd combine the test cases. I'd also give them better names: this one for example is closer to
add_register_domains_permission_allows_registering_domain
. You're documenting an invariant, in this case that it A) isn't possible to register the domain without this permission, B) that it becomes possible after adding the permission, C) that it becomes impossible again when you revoke it.
All three should be checked in a single test.
636c667
to
b63607f
Compare
} | ||
|
||
impl TestEnv { | ||
fn new() -> Self { |
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.
If you have time, please add doc comments, which explain who has which assets to this associated function, or to the struct (preferably both, preferably using #[doc(include...]
.
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 couldn't think of anything better than describing relationships in the comments to the fields of TestEnv
structure. Comments are prompted when you hover over the field. I wanted to use something like mermaid
but rustdoc
doesn't support it. Creating a separate markdown file is probably too much.
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.
Sounds good.
let register = if let Instruction::Register(register) = instruction { | ||
register | ||
} else { | ||
return Ok(()); | ||
}; | ||
Err("Domain registration is prohibited.".to_owned()) | ||
|
||
if let Ok(RegistrableBox::Domain(_)) = register.object.evaluate(wsv, &Context::new()) { | ||
Err("Domain registration is prohibited.".to_owned()) | ||
} else { | ||
Ok(()) | ||
} |
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'd rewrite something like this:
if let Instruction::Register(register) = instruction {
if let Ok(RegistrableBox::Domain(_)) = register.object.evaluate(wsv, &Context::new()) {
return Err("Domain registration is prohibited.".to_owned());
}
}
Ok(())
Easier to read for me
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.
Probably with the merge of #2284 you'll have to add into()
after to_owned()
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.
Updated
let TestEnv { | ||
alice_id, | ||
bob_id, | ||
carol_id, | ||
wsv, | ||
.. | ||
} = TestEnv::new(); |
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 idea
|
||
{ | ||
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.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.
Why not to use another query to check is_ok()
variant?
|
||
{ | ||
let only_accounts_domain: IsQueryAllowedBoxed<World> = query::OnlyAccountsDomain.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.
Oh I see, it tests queries, not validators
} | ||
} | ||
|
||
mod revoke_and_grant { |
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'd suggest to add more negative tests scenarios in this module tests.
I.e. add_register_domains_permission_allows_registering_domain_with_right_token()
checks that Alice can register new domainsm, but doesn't check if anyone without permission token can do it too.
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.
Added
8b7fd59
to
14fafcf
Compare
14fafcf
to
8a6f32d
Compare
1ab9bf8
to
57b6c0b
Compare
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.
Excellent!
} | ||
|
||
impl TestEnv { | ||
fn new() -> Self { |
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.
Sounds good.
57b6c0b
to
d9ef279
Compare
Signed-off-by: Vladimir Pesterev <pesterev@pm.me>
d9ef279
to
07e62bd
Compare
Signed-off-by: Vladimir Pesterev pesterev@pm.me
Description of the Change
I added tests for permissions related to account and some asset
queries
and also for permissions related toregister
.But the(will be in the next PRs) The tests are very different and it is very difficult to write a macro that would generate that tests.OnlyAccountsDomain
andOnlyAccountsData
permissions need to be tested on many other queries.IsAllowed
forProhibitRegisterDomains
Issue
Partially resolves #2053
Benefits
Improved coverage
Possible Drawbacks
None
Alternate Designs [optional]
Using macros would reduce the number of tests, but the tests are too different.
Also we can just write tests only for queries with non-trivial logic.