-
Notifications
You must be signed in to change notification settings - Fork 117
For #7, implement simple Data Object Type registry #13
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
Merged
Merged
Changes from 10 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
a4c152f
For #7, implment simple Data Object Type registry
8668f1b
Fix comment typo
8d66c12
These extrinsics should be public
ff33136
Be explicit about requiring root. IMHO this is better style.
ba3413d
Unit test setup for storage module
913538b
Get unit tests to run
6c14ac7
Make unit tests work, but still allow building wasm runtime.
c928b88
Silence warning
854378a
Cover extrinsics with unit tests
0286059
Improve naming
61dfc3c
Fixes #14
a695dd2
Respond to comment about consistent naming. I think the best compromise
701e2b0
More renaming (debated in rocket chat); works for WASM build, but not
32b659a
Adjust renaming to unit tests
cb73509
Simplify ensure_data_object_type()
f744032
Separate activate and deactivate functions for DO types
b3e5ff2
Add comment about future field in DO types
64b79d3
Merge branch 'development' into data-object-type-registry
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| #![cfg(test)] | ||
|
|
||
| use rstd::prelude::*; | ||
| pub use super::{types}; | ||
| pub use system; | ||
|
|
||
| pub use primitives::{H256, Blake2Hasher}; | ||
| pub use runtime_primitives::{ | ||
| BuildStorage, | ||
| traits::{BlakeTwo256, OnFinalise, IdentityLookup}, | ||
| testing::{Digest, DigestItem, Header, UintAuthorityId} | ||
| }; | ||
|
|
||
| use srml_support::{impl_outer_origin, impl_outer_event}; | ||
|
|
||
| impl_outer_origin! { | ||
| pub enum Origin for Test {} | ||
| } | ||
|
|
||
| impl_outer_event! { | ||
| pub enum MetaEvent for Test | ||
| { | ||
| types<T>, | ||
| } | ||
| } | ||
|
|
||
| // For testing the module, we construct most of a mock runtime. This means | ||
| // first constructing a configuration type (`Test`) which `impl`s each of the | ||
| // configuration traits of modules we want to use. | ||
| #[derive(Clone, Eq, PartialEq, Debug)] | ||
| pub struct Test; | ||
| impl system::Trait for Test | ||
| { | ||
| type Origin = Origin; | ||
| type Index = u64; | ||
| type BlockNumber = u64; | ||
| type Hash = H256; | ||
| type Hashing = BlakeTwo256; | ||
| type Digest = Digest; | ||
| type AccountId = u64; | ||
| type Header = Header; | ||
| type Event = MetaEvent; | ||
| type Log = DigestItem; | ||
| type Lookup = IdentityLookup<u64>; | ||
| } | ||
| impl types::Trait for Test | ||
| { | ||
| type Event = MetaEvent; | ||
| type DataObjectTypeID = u64; | ||
| } | ||
|
|
||
| pub struct ExtBuilder | ||
| { | ||
| first_data_object_type_id: u64, | ||
| } | ||
|
|
||
| impl Default for ExtBuilder | ||
| { | ||
| fn default() -> Self | ||
| { | ||
| Self { | ||
| first_data_object_type_id: 1, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl ExtBuilder | ||
| { | ||
| pub fn first_data_object_type_id(mut self, first_data_object_type_id: u64) -> Self | ||
| { | ||
| self.first_data_object_type_id = first_data_object_type_id; | ||
| self | ||
| } | ||
| pub fn build(self) -> runtime_io::TestExternalities<Blake2Hasher> | ||
| { | ||
| let mut t = system::GenesisConfig::<Test>::default().build_storage().unwrap().0; | ||
|
|
||
| t.extend(types::GenesisConfig::<Test>{ | ||
| first_data_object_type_id: self.first_data_object_type_id, | ||
| }.build_storage().unwrap().0); | ||
|
|
||
| t.into() | ||
| } | ||
| } | ||
|
|
||
|
|
||
| pub type System = system::Module<Test>; | ||
| pub type Types = types::Module<Test>; | ||
| pub type TestDataObjectType = types::DataObjectType<Test>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| pub mod types; | ||
|
|
||
| mod mock; | ||
| mod tests; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,162 @@ | ||
| #![cfg(test)] | ||
|
|
||
| use super::*; | ||
| use super::mock::*; | ||
|
|
||
| use runtime_io::with_externalities; | ||
| use srml_support::*; | ||
| use system::{self, Phase, EventRecord}; | ||
|
|
||
| #[test] | ||
| fn initial_state() | ||
| { | ||
| const DEFAULT_FIRST_ID: u64 = 1000; | ||
|
|
||
| with_externalities(&mut ExtBuilder::default() | ||
| .first_data_object_type_id(DEFAULT_FIRST_ID).build(), || | ||
| { | ||
| assert_eq!(Types::first_data_object_type_id(), DEFAULT_FIRST_ID); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn fail_register_without_root() | ||
| { | ||
| const DEFAULT_FIRST_ID: u64 = 1000; | ||
|
|
||
| with_externalities(&mut ExtBuilder::default() | ||
| .first_data_object_type_id(DEFAULT_FIRST_ID).build(), || | ||
| { | ||
| let data: TestDataObjectType = TestDataObjectType { | ||
| id: None, | ||
| description: "foo".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let res = Types::register_data_object_type(Origin::signed(1), data); | ||
| assert!(res.is_err()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn succeed_register_as_root() | ||
| { | ||
| const DEFAULT_FIRST_ID: u64 = 1000; | ||
|
|
||
| with_externalities(&mut ExtBuilder::default() | ||
| .first_data_object_type_id(DEFAULT_FIRST_ID).build(), || | ||
| { | ||
| let data: TestDataObjectType = TestDataObjectType { | ||
| id: None, | ||
| description: "foo".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let res = Types::register_data_object_type(Origin::ROOT, data); | ||
| assert!(res.is_ok()); | ||
| }); | ||
| } | ||
|
|
||
| #[test] | ||
| fn update_existing() | ||
| { | ||
| const DEFAULT_FIRST_ID: u64 = 1000; | ||
|
|
||
| with_externalities(&mut ExtBuilder::default() | ||
| .first_data_object_type_id(DEFAULT_FIRST_ID).build(), || | ||
| { | ||
| // First register a type | ||
| let data: TestDataObjectType = TestDataObjectType { | ||
| id: None, | ||
| description: "foo".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let id_res = Types::register_data_object_type(Origin::ROOT, data); | ||
| assert!(id_res.is_ok()); | ||
| assert_eq!(*System::events().last().unwrap(), | ||
| EventRecord { | ||
| phase: Phase::ApplyExtrinsic(0), | ||
| event: MetaEvent::types(types::RawEvent::DataObjectTypeAdded(DEFAULT_FIRST_ID)), | ||
| } | ||
| ); | ||
|
|
||
|
|
||
| // Now update it with new data - we need the ID to be the same as in | ||
| // returned by the previous call. First, though, try and fail without | ||
| let updated1: TestDataObjectType = TestDataObjectType { | ||
| id: None, | ||
| description: "bar".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let res = Types::update_data_object_type(Origin::ROOT, updated1); | ||
| assert!(res.is_err()); | ||
|
|
||
| // Now try with a bad ID | ||
| let updated2: TestDataObjectType = TestDataObjectType { | ||
| id: Some(DEFAULT_FIRST_ID + 1), | ||
| description: "bar".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let res = Types::update_data_object_type(Origin::ROOT, updated2); | ||
| assert!(res.is_err()); | ||
|
|
||
| // Finally with an existing ID, it should work. | ||
| let updated3: TestDataObjectType = TestDataObjectType { | ||
| id: Some(DEFAULT_FIRST_ID), | ||
| description: "bar".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let res = Types::update_data_object_type(Origin::ROOT, updated3); | ||
| assert!(res.is_ok()); | ||
| assert_eq!(*System::events().last().unwrap(), | ||
| EventRecord { | ||
| phase: Phase::ApplyExtrinsic(0), | ||
| event: MetaEvent::types(types::RawEvent::DataObjectTypeUpdated(DEFAULT_FIRST_ID)), | ||
| } | ||
| ); | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| #[test] | ||
| fn activate_existing() | ||
| { | ||
| const DEFAULT_FIRST_ID: u64 = 1000; | ||
|
|
||
| with_externalities(&mut ExtBuilder::default() | ||
| .first_data_object_type_id(DEFAULT_FIRST_ID).build(), || | ||
| { | ||
| // First register a type | ||
| let data: TestDataObjectType = TestDataObjectType { | ||
| id: None, | ||
| description: "foo".as_bytes().to_vec(), | ||
| active: false, | ||
| }; | ||
| let id_res = Types::register_data_object_type(Origin::ROOT, data); | ||
| assert!(id_res.is_ok()); | ||
| assert_eq!(*System::events().last().unwrap(), | ||
| EventRecord { | ||
| phase: Phase::ApplyExtrinsic(0), | ||
| event: MetaEvent::types(types::RawEvent::DataObjectTypeAdded(DEFAULT_FIRST_ID)), | ||
| } | ||
| ); | ||
|
|
||
| // Retrieve, and ensure it's not active. | ||
| let data = Types::data_object_type(DEFAULT_FIRST_ID); | ||
| assert!(data.is_some()); | ||
| assert!(!data.unwrap().active); | ||
|
|
||
| // Now activate the data object type | ||
| let res = Types::activate_data_object_type(Origin::ROOT, DEFAULT_FIRST_ID, true); | ||
| assert!(res.is_ok()); | ||
| assert_eq!(*System::events().last().unwrap(), | ||
| EventRecord { | ||
| phase: Phase::ApplyExtrinsic(0), | ||
| event: MetaEvent::types(types::RawEvent::DataObjectTypeUpdated(DEFAULT_FIRST_ID)), | ||
| } | ||
| ); | ||
|
|
||
| // Ensure that the item is actually activated. | ||
| let data = Types::data_object_type(DEFAULT_FIRST_ID); | ||
| assert!(data.is_some()); | ||
| assert!(data.unwrap().active); | ||
| }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.