Skip to content
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

Init Testing Chapter #87

Merged
merged 23 commits into from
Dec 1, 2019
Merged
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
b6908b0
init own testing section
4meta5 Nov 19, 2019
0e15d00
update SUMMARY.md according to proposed reorg
4meta5 Nov 19, 2019
af5b2a0
delete treasury and token sections, start changing smpl-treasury to b…
4meta5 Nov 20, 2019
05856d5
init externalities section
4meta5 Nov 20, 2019
3dbeb80
stash when debugging (considering adding an informal changelog for be…
4meta5 Nov 20, 2019
60c7f4b
update smpl-treasury with more logic
4meta5 Nov 21, 2019
000a72f
add genesis config and test for it
4meta5 Nov 24, 2019
1bea238
set intermediate objectives in the CHANGELOG.md, add more tests to sm…
4meta5 Nov 26, 2019
15904e6
update wording for the changelog
4meta5 Nov 26, 2019
7cd6841
rename schedule_on_finalize to execution_schedule with intention of a…
4meta5 Nov 27, 2019
4cd9c38
trouble compiling execution-schedule
4meta5 Nov 28, 2019
94ec415
fixed up struct-storage, adding tests next
4meta5 Nov 28, 2019
3c2397d
add struct-storage tests and cargo fmt
4meta5 Nov 28, 2019
f7d2393
rewrite code for execution-schedule, next is tests
4meta5 Nov 29, 2019
58fcc74
execution-schedule test scaffolding and one test
4meta5 Nov 29, 2019
404ef47
better test coverage for execution-schedule
4meta5 Nov 29, 2019
eefd2db
delete social-network, add 2/3 of writing for new test chapter
4meta5 Nov 29, 2019
a0bfaeb
fix all rust code block annotations to not allow user to compile and …
4meta5 Nov 30, 2019
f3e9f79
change rust annotations
4meta5 Nov 30, 2019
8aa5da3
master.into()
4meta5 Nov 30, 2019
0c28b3a
attempt to fix build, then ran cargo fmt
4meta5 Nov 30, 2019
3668f74
fix code blocks in instantiable-modules
4meta5 Dec 1, 2019
fb15d2b
fix event initialization instructions in instantiable modules recipe
4meta5 Dec 1, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add struct-storage tests and cargo fmt
  • Loading branch information
4meta5 committed Nov 28, 2019
commit 3c2397d61ad130a0474e0c47ede5d35ca47b0c0c
128 changes: 107 additions & 21 deletions kitchen/modules/struct-storage/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
#![cfg_attr(not(feature = "std"), no_std)]
use balances;
/// Nested Structs
use runtime_primitives::RuntimeDebug;
use runtime_primitives::{traits::SimpleArithmetic, RuntimeDebug};
use support::{
decl_module, decl_event, decl_storage, dispatch::Result, StorageMap, StorageValue, codec::{Decode, Encode},
codec::{Decode, Encode},
decl_event, decl_module, decl_storage,
dispatch::Result,
StorageMap, StorageValue,
};
use system::{self, ensure_signed};
use balances;

pub trait Trait: balances::Trait + system::Trait {
type Event: From<Event<Self>> + Into<<Self as system::Trait>::Event>;
Expand All @@ -18,8 +21,6 @@ pub struct InnerThing<Hash, Balance> {
balance: Balance,
}

// must implement Eq in order to properly test this

#[derive(Encode, Decode, Default, RuntimeDebug)]
pub struct SuperThing<Hash, Balance> {
super_number: u32,
Expand Down Expand Up @@ -85,6 +86,7 @@ decl_module! {
hash,
balance,
};
// overwrites any existing `InnerThing` with `number: inner_number` by default
<InnerThingsByNumbers<T>>::insert(inner_number, inner_thing.clone());
Self::deposit_event(RawEvent::NewInnerThing(inner_number, hash, balance));
// now construct and insert `super_thing`
Expand Down Expand Up @@ -112,6 +114,21 @@ mod tests {
use support::{assert_err, impl_outer_event, impl_outer_origin, parameter_types, traits::Get};
use system::{ensure_signed, EventRecord, Phase};

// hacky Eq implementation for testing InnerThing
impl<Hash: Clone, Balance: Copy + SimpleArithmetic> PartialEq for InnerThing<Hash, Balance> {
fn eq(&self, other: &Self) -> bool {
self.number == other.number
}
}
impl<Hash: Clone, Balance: Copy + SimpleArithmetic> Eq for InnerThing<Hash, Balance> {}
// "" for SuperThing
impl<Hash: Clone, Balance: Copy + SimpleArithmetic> PartialEq for SuperThing<Hash, Balance> {
fn eq(&self, other: &Self) -> bool {
self.super_number == other.super_number
}
}
impl<Hash: Clone, Balance: Copy + SimpleArithmetic> Eq for SuperThing<Hash, Balance> {}

impl_outer_origin! {
pub enum Origin for TestRuntime {}
}
Expand Down Expand Up @@ -141,9 +158,9 @@ mod tests {
type MaximumBlockLength = MaximumBlockLength;
type AvailableBlockRatio = AvailableBlockRatio;
type Version = ();
}
}
// note: very unrealistic for most test envs
parameter_types!{
parameter_types! {
pub const ExistentialDeposit: u64 = 0;
pub const TransferFee: u64 = 0;
pub const CreationFee: u64 = 0;
Expand Down Expand Up @@ -201,33 +218,102 @@ mod tests {
// prepare hash
let data = H256::from_low_u64_be(16);
// insert inner thing
StructStorage::insert_inner_thing(Origin::signed(1), 3u32, data, 5u64.into());
StructStorage::insert_inner_thing(Origin::signed(1), 3u32, data, 7u64.into());

// check storage matches expectations
let expected_storage_item = InnerThing {
number: 3u32,
hash: data,
balance: 5u64,
balance: 7u64,
};
assert_eq!(
StructStorage::inner_things_by_numbers(3u32),
expected_storage_item
);

// check events emitted match expectations
let expected_event = TestEvent::struct_storage(RawEvent::NewInnerThing(3u32, data, 5u64));
let expected_event =
TestEvent::struct_storage(RawEvent::NewInnerThing(3u32, data, 7u64));
assert!(System::events().iter().any(|a| a.event == expected_event));
})
}
}

// TODO LIST BEFORE NEXT PUSH
// - tests for struct-storage
// - debug `execution-schedule`
// PUSH
// - add tests for `execution-schedule`
// - finish testing section
// PUSH
// - finish testing doc for devhub
// - link from the recipe's testing chapter
// - could use just a last page in the testing chapter comparing the first two side by side
#[test]
fn insert_super_thing_with_existing_works() {
ExtBuilder::build().execute_with(|| {
// prepare hash
let data = H256::from_low_u64_be(16);
// insert inner first (tested in direct test above)
StructStorage::insert_inner_thing(Origin::signed(1), 3u32, data, 7u64.into());
// insert super with existing inner
StructStorage::insert_super_thing_with_existing_inner(Origin::signed(1), 3u32, 5u32);

// check storage matches expectations
let expected_inner = InnerThing {
number: 3u32,
hash: data,
balance: 7u64,
};
assert_eq!(StructStorage::inner_things_by_numbers(3u32), expected_inner);
let expected_outer = SuperThing {
super_number: 5u32,
inner_thing: expected_inner.clone(),
};
assert_eq!(
StructStorage::super_things_by_super_numbers(5u32),
expected_outer
);

let expected_event = TestEvent::struct_storage(RawEvent::NewSuperThingByExistingInner(
5u32,
3u32,
data,
7u64.into(),
));
assert!(System::events().iter().any(|a| a.event == expected_event));
})
}

#[test]
fn insert_super_with_new_inner_works() {
ExtBuilder::build().execute_with(|| {
// prepare hash
let data = H256::from_low_u64_be(16);
// insert super with new inner
StructStorage::insert_super_thing_with_new_inner(
Origin::signed(1),
3u32,
data,
7u64.into(),
5u32,
);

// check storage matches expectations
let expected_inner = InnerThing {
number: 3u32,
hash: data,
balance: 7u64,
};
assert_eq!(StructStorage::inner_things_by_numbers(3u32), expected_inner);
let expected_outer = SuperThing {
super_number: 5u32,
inner_thing: expected_inner.clone(),
};
assert_eq!(
StructStorage::super_things_by_super_numbers(5u32),
expected_outer
);

let expected_event =
TestEvent::struct_storage(RawEvent::NewInnerThing(3u32, data, 7u64));
assert!(System::events().iter().any(|a| a.event == expected_event));
let expected_event2 = TestEvent::struct_storage(RawEvent::NewSuperThingByNewInner(
5u32,
3u32,
data,
7u64.into(),
));
assert!(System::events().iter().any(|a| a.event == expected_event2));
})
}
}