Skip to content

Commit

Permalink
mdBook generated from gitorial
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Sep 14, 2024
1 parent 6a27202 commit e4ed263
Show file tree
Hide file tree
Showing 81 changed files with 1,707 additions and 695 deletions.
28 changes: 23 additions & 5 deletions src/1/source/changes.diff
Original file line number Diff line number Diff line change
Expand Up @@ -3838,10 +3838,10 @@ index 0000000..ae8a09b
+}
diff --git a/src/tests.rs b/src/tests.rs
new file mode 100644
index 0000000..266ea5e
index 0000000..375cd3f
--- /dev/null
+++ b/src/tests.rs
@@ -0,0 +1,79 @@
@@ -0,0 +1,97 @@
+// Tests for the Kitties Pallet.
+//
+// Normally this file would be split into two parts: `mock.rs` and `tests.rs`.
Expand All @@ -3865,6 +3865,17 @@ index 0000000..266ea5e
+type Balance = u64;
+type Block = frame_system::mocking::MockBlock<TestRuntime>;
+
+// In our "test runtime", we represent a user `AccountId` with a `u64`.
+// This is just a simplification so that we don't need to generate a bunch of proper cryptographic
+// public keys when writing tests. It is just easier to say "user 1 transfers to user 2".
+// We create the constants `ALICE` and `BOB` to make it clear when we are representing users below.
+const ALICE: u64 = 1;
+const BOB: u64 = 2;
+
+// Our blockchain tests only need 3 Pallets:
+// 1. System: Which is included with every FRAME runtime.
+// 2. Balances: Which is manages your blockchain's native currency. (i.e. DOT on Polkadot)
+// 3. PalletKitties: The pallet you are building in this tutorial!
+construct_runtime! {
+ pub struct TestRuntime {
+ System: frame_system,
Expand All @@ -3873,25 +3884,32 @@ index 0000000..266ea5e
+ }
+}
+
+const ALICE: u64 = 1;
+const BOB: u64 = 2;
+
+// Normally `System` would have many more configurations, but you can see that we use some macro
+// magic to automatically configure most of the pallet for a "default test configuration".
+#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
+impl frame_system::Config for TestRuntime {
+ type Block = Block;
+ type AccountData = pallet_balances::AccountData<Balance>;
+}
+
+// Normally `Balances` would have many more configurations, but you can see that we use some macro
+// magic to automatically configure most of the pallet for a "default test configuration".
+#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
+impl pallet_balances::Config for TestRuntime {
+ type AccountStore = System;
+ type Balance = Balance;
+}
+
+// This is the configuration of our Pallet! If you make changes to the pallet's `trait Config`, you
+// will also need to update this configuration to represent that.
+impl pallet_kitties::Config for TestRuntime {
+ type RuntimeEvent = RuntimeEvent;
+}
+
+// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
+// It simulates the blockchain database backend for our tests.
+// If you forget to include this and try to access your Pallet storage, you will get an error like:
+// "`get_version_1` called outside of an Externalities-provided environment."
+pub fn new_test_ext() -> sp_io::TestExternalities {
+ frame_system::GenesisConfig::<TestRuntime>::default()
+ .build_storage()
Expand Down
24 changes: 21 additions & 3 deletions src/1/source/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ use frame::traits::fungible::*;
type Balance = u64;
type Block = frame_system::mocking::MockBlock<TestRuntime>;

// In our "test runtime", we represent a user `AccountId` with a `u64`.
// This is just a simplification so that we don't need to generate a bunch of proper cryptographic
// public keys when writing tests. It is just easier to say "user 1 transfers to user 2".
// We create the constants `ALICE` and `BOB` to make it clear when we are representing users below.
const ALICE: u64 = 1;
const BOB: u64 = 2;

// Our blockchain tests only need 3 Pallets:
// 1. System: Which is included with every FRAME runtime.
// 2. Balances: Which is manages your blockchain's native currency. (i.e. DOT on Polkadot)
// 3. PalletKitties: The pallet you are building in this tutorial!
construct_runtime! {
pub struct TestRuntime {
System: frame_system,
Expand All @@ -29,25 +40,32 @@ construct_runtime! {
}
}

const ALICE: u64 = 1;
const BOB: u64 = 2;

// Normally `System` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for TestRuntime {
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
}

// Normally `Balances` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for TestRuntime {
type AccountStore = System;
type Balance = Balance;
}

// This is the configuration of our Pallet! If you make changes to the pallet's `trait Config`, you
// will also need to update this configuration to represent that.
impl pallet_kitties::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
}

// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
// It simulates the blockchain database backend for our tests.
// If you forget to include this and try to access your Pallet storage, you will get an error like:
// "`get_version_1` called outside of an Externalities-provided environment."
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<TestRuntime>::default()
.build_storage()
Expand Down
28 changes: 23 additions & 5 deletions src/10/source/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ use frame::traits::fungible::*;
type Balance = u64;
type Block = frame_system::mocking::MockBlock<TestRuntime>;

// In our "test runtime", we represent a user `AccountId` with a `u64`.
// This is just a simplification so that we don't need to generate a bunch of proper cryptographic
// public keys when writing tests. It is just easier to say "user 1 transfers to user 2".
// We create the constants `ALICE` and `BOB` to make it clear when we are representing users below.
const ALICE: u64 = 1;
const BOB: u64 = 2;

// Our blockchain tests only need 3 Pallets:
// 1. System: Which is included with every FRAME runtime.
// 2. Balances: Which is manages your blockchain's native currency. (i.e. DOT on Polkadot)
// 3. PalletKitties: The pallet you are building in this tutorial!
construct_runtime! {
pub struct TestRuntime {
System: frame_system,
Expand All @@ -29,25 +40,32 @@ construct_runtime! {
}
}

const ALICE: u64 = 1;
const BOB: u64 = 2;

// Normally `System` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for TestRuntime {
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
}

// Normally `Balances` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for TestRuntime {
type AccountStore = System;
type Balance = Balance;
}

// This is the configuration of our Pallet! If you make changes to the pallet's `trait Config`, you
// will also need to update this configuration to represent that.
impl pallet_kitties::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
}

// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
// It simulates the blockchain database backend for our tests.
// If you forget to include this and try to access your Pallet storage, you will get an error like:
// "`get_version_1` called outside of an Externalities-provided environment."
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<TestRuntime>::default()
.build_storage()
Expand Down Expand Up @@ -82,7 +100,7 @@ fn system_and_balances_work() {
fn create_kitty_checks_signed() {
new_test_ext().execute_with(|| {
// The `create_kitty` extrinsic should work when being called by a user.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// The `create_kitty` extrinsic should fail when being called by an unsigned message.
assert_noop!(PalletKitties::create_kitty(RuntimeOrigin::none()), DispatchError::BadOrigin);
})
Expand All @@ -94,7 +112,7 @@ fn create_kitty_emits_event() {
// We need to set block number to 1 to view events.
System::set_block_number(1);
// Execute our call, and ensure it is successful.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// Assert the last event by our blockchain is the `Created` event with the correct owner.
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
Expand Down
4 changes: 2 additions & 2 deletions src/11/solution/solution.diff
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ index 7d88fff..90242f6 100644
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
diff --git a/src/tests.rs b/src/tests.rs
index a33f4de..0c1aca2 100644
index 8fa3dc6..d3e7ab7 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -99,3 +99,15 @@ fn create_kitty_emits_event() {
@@ -117,3 +117,15 @@ fn create_kitty_emits_event() {
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
}
Expand Down
28 changes: 23 additions & 5 deletions src/11/solution/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ use frame::traits::fungible::*;
type Balance = u64;
type Block = frame_system::mocking::MockBlock<TestRuntime>;

// In our "test runtime", we represent a user `AccountId` with a `u64`.
// This is just a simplification so that we don't need to generate a bunch of proper cryptographic
// public keys when writing tests. It is just easier to say "user 1 transfers to user 2".
// We create the constants `ALICE` and `BOB` to make it clear when we are representing users below.
const ALICE: u64 = 1;
const BOB: u64 = 2;

// Our blockchain tests only need 3 Pallets:
// 1. System: Which is included with every FRAME runtime.
// 2. Balances: Which is manages your blockchain's native currency. (i.e. DOT on Polkadot)
// 3. PalletKitties: The pallet you are building in this tutorial!
construct_runtime! {
pub struct TestRuntime {
System: frame_system,
Expand All @@ -29,25 +40,32 @@ construct_runtime! {
}
}

const ALICE: u64 = 1;
const BOB: u64 = 2;

// Normally `System` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for TestRuntime {
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
}

// Normally `Balances` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for TestRuntime {
type AccountStore = System;
type Balance = Balance;
}

// This is the configuration of our Pallet! If you make changes to the pallet's `trait Config`, you
// will also need to update this configuration to represent that.
impl pallet_kitties::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
}

// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
// It simulates the blockchain database backend for our tests.
// If you forget to include this and try to access your Pallet storage, you will get an error like:
// "`get_version_1` called outside of an Externalities-provided environment."
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<TestRuntime>::default()
.build_storage()
Expand Down Expand Up @@ -82,7 +100,7 @@ fn system_and_balances_work() {
fn create_kitty_checks_signed() {
new_test_ext().execute_with(|| {
// The `create_kitty` extrinsic should work when being called by a user.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// The `create_kitty` extrinsic should fail when being called by an unsigned message.
assert_noop!(PalletKitties::create_kitty(RuntimeOrigin::none()), DispatchError::BadOrigin);
})
Expand All @@ -94,7 +112,7 @@ fn create_kitty_emits_event() {
// We need to set block number to 1 to view events.
System::set_block_number(1);
// Execute our call, and ensure it is successful.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// Assert the last event by our blockchain is the `Created` event with the correct owner.
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
Expand Down
28 changes: 23 additions & 5 deletions src/11/template/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@ use frame::traits::fungible::*;
type Balance = u64;
type Block = frame_system::mocking::MockBlock<TestRuntime>;

// In our "test runtime", we represent a user `AccountId` with a `u64`.
// This is just a simplification so that we don't need to generate a bunch of proper cryptographic
// public keys when writing tests. It is just easier to say "user 1 transfers to user 2".
// We create the constants `ALICE` and `BOB` to make it clear when we are representing users below.
const ALICE: u64 = 1;
const BOB: u64 = 2;

// Our blockchain tests only need 3 Pallets:
// 1. System: Which is included with every FRAME runtime.
// 2. Balances: Which is manages your blockchain's native currency. (i.e. DOT on Polkadot)
// 3. PalletKitties: The pallet you are building in this tutorial!
construct_runtime! {
pub struct TestRuntime {
System: frame_system,
Expand All @@ -29,25 +40,32 @@ construct_runtime! {
}
}

const ALICE: u64 = 1;
const BOB: u64 = 2;

// Normally `System` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(frame_system::config_preludes::TestDefaultConfig)]
impl frame_system::Config for TestRuntime {
type Block = Block;
type AccountData = pallet_balances::AccountData<Balance>;
}

// Normally `Balances` would have many more configurations, but you can see that we use some macro
// magic to automatically configure most of the pallet for a "default test configuration".
#[derive_impl(pallet_balances::config_preludes::TestDefaultConfig)]
impl pallet_balances::Config for TestRuntime {
type AccountStore = System;
type Balance = Balance;
}

// This is the configuration of our Pallet! If you make changes to the pallet's `trait Config`, you
// will also need to update this configuration to represent that.
impl pallet_kitties::Config for TestRuntime {
type RuntimeEvent = RuntimeEvent;
}

// We need to run most of our tests using this function: `new_test_ext().execute_with(|| { ... });`
// It simulates the blockchain database backend for our tests.
// If you forget to include this and try to access your Pallet storage, you will get an error like:
// "`get_version_1` called outside of an Externalities-provided environment."
pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::<TestRuntime>::default()
.build_storage()
Expand Down Expand Up @@ -82,7 +100,7 @@ fn system_and_balances_work() {
fn create_kitty_checks_signed() {
new_test_ext().execute_with(|| {
// The `create_kitty` extrinsic should work when being called by a user.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// The `create_kitty` extrinsic should fail when being called by an unsigned message.
assert_noop!(PalletKitties::create_kitty(RuntimeOrigin::none()), DispatchError::BadOrigin);
})
Expand All @@ -94,7 +112,7 @@ fn create_kitty_emits_event() {
// We need to set block number to 1 to view events.
System::set_block_number(1);
// Execute our call, and ensure it is successful.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
// Assert the last event by our blockchain is the `Created` event with the correct owner.
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
Expand Down
6 changes: 3 additions & 3 deletions src/12/solution/solution.diff
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ index b396f98..9739330 100644
Ok(())
}
diff --git a/src/tests.rs b/src/tests.rs
index 0c1aca2..6c8a90c 100644
index d3e7ab7..6318ca0 100644
--- a/src/tests.rs
+++ b/src/tests.rs
@@ -111,3 +111,15 @@ fn count_for_kitties_created_correctly() {
@@ -129,3 +129,15 @@ fn count_for_kitties_created_correctly() {
CountForKitties::<TestRuntime>::put(1337u32);
})
}
Expand All @@ -33,7 +33,7 @@ index 0c1aca2..6c8a90c 100644
+ // Querying storage before anything is set will return `None`.
+ assert_eq!(CountForKitties::<TestRuntime>::get(), None);
+ // Call `create_kitty` which will call `mint`.
+ assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
+ assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(ALICE)));
+ // Now the storage should be `Some(1)`
+ assert_eq!(CountForKitties::<TestRuntime>::get(), Some(1));
+ })
Expand Down
Loading

0 comments on commit e4ed263

Please sign in to comment.