Skip to content

Commit

Permalink
more tests and fix up
Browse files Browse the repository at this point in the history
  • Loading branch information
shawntabrizi committed Sep 13, 2024
1 parent 894e0e4 commit 82bf395
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 12 deletions.
8 changes: 4 additions & 4 deletions steps/19/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `create_kitty` which will call `mint`.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(3)));
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
Expand Down
8 changes: 4 additions & 4 deletions steps/20/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `create_kitty` which will call `mint`.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(3)));
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
Expand Down
8 changes: 4 additions & 4 deletions steps/21/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,13 @@ fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `create_kitty` which will call `mint`.
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2)));
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(3)));
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
Expand Down
52 changes: 52 additions & 0 deletions steps/22/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,55 @@ fn create_kitty_emits_event() {
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
}

#[test]
fn count_for_kitties_created_correctly() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// You can `set` the value using an `u32`.
CountForKitties::<TestRuntime>::set(1337u32);
// You can `put` the value directly with a `u32`.
CountForKitties::<TestRuntime>::put(1337u32);
})
}

#[test]
fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
}

#[test]
fn mint_errors_when_overflow() {
new_test_ext().execute_with(|| {
// Set the count to the largest value possible.
CountForKitties::<TestRuntime>::set(u32::MAX);
// `create_kitty` should not succeed because of safe math.
assert_noop!(
PalletKitties::create_kitty(RuntimeOrigin::signed(1)),
Error::<TestRuntime>::TooManyKitties
);
})
}

#[test]
fn kitties_map_created_correctly() {
new_test_ext().execute_with(|| {
let zero_key = [0u8; 32];
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), false);
Kitties::<TestRuntime>::insert(zero_key, ());
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), true);
})
}
60 changes: 60 additions & 0 deletions steps/23/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,63 @@ fn create_kitty_emits_event() {
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
}

#[test]
fn count_for_kitties_created_correctly() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// You can `set` the value using an `u32`.
CountForKitties::<TestRuntime>::set(1337u32);
// You can `put` the value directly with a `u32`.
CountForKitties::<TestRuntime>::put(1337u32);
})
}

#[test]
fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
}

#[test]
fn mint_errors_when_overflow() {
new_test_ext().execute_with(|| {
// Set the count to the largest value possible.
CountForKitties::<TestRuntime>::set(u32::MAX);
// `create_kitty` should not succeed because of safe math.
assert_noop!(
PalletKitties::create_kitty(RuntimeOrigin::signed(1)),
Error::<TestRuntime>::TooManyKitties
);
})
}

#[test]
fn kitties_map_created_correctly() {
new_test_ext().execute_with(|| {
let zero_key = [0u8; 32];
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), false);
Kitties::<TestRuntime>::insert(zero_key, ());
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), true);
})
}

#[test]
fn create_kitty_adds_to_map() {
new_test_ext().execute_with(|| {
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_eq!(Kitties::<TestRuntime>::iter().count(), 1);
})
}
60 changes: 60 additions & 0 deletions steps/24/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,63 @@ fn create_kitty_emits_event() {
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
}

#[test]
fn count_for_kitties_created_correctly() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// You can `set` the value using an `u32`.
CountForKitties::<TestRuntime>::set(1337u32);
// You can `put` the value directly with a `u32`.
CountForKitties::<TestRuntime>::put(1337u32);
})
}

#[test]
fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
}

#[test]
fn mint_errors_when_overflow() {
new_test_ext().execute_with(|| {
// Set the count to the largest value possible.
CountForKitties::<TestRuntime>::set(u32::MAX);
// `create_kitty` should not succeed because of safe math.
assert_noop!(
PalletKitties::create_kitty(RuntimeOrigin::signed(1)),
Error::<TestRuntime>::TooManyKitties
);
})
}

#[test]
fn kitties_map_created_correctly() {
new_test_ext().execute_with(|| {
let zero_key = [0u8; 32];
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), false);
Kitties::<TestRuntime>::insert(zero_key, ());
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), true);
})
}

#[test]
fn create_kitty_adds_to_map() {
new_test_ext().execute_with(|| {
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_eq!(Kitties::<TestRuntime>::iter().count(), 1);
})
}
68 changes: 68 additions & 0 deletions steps/25/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,71 @@ fn create_kitty_emits_event() {
System::assert_last_event(Event::<TestRuntime>::Created { owner: 1 }.into());
})
}

#[test]
fn count_for_kitties_created_correctly() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// You can `set` the value using an `u32`.
CountForKitties::<TestRuntime>::set(1337u32);
// You can `put` the value directly with a `u32`.
CountForKitties::<TestRuntime>::put(1337u32);
})
}

#[test]
fn mint_increments_count_for_kitty() {
new_test_ext().execute_with(|| {
// Querying storage before anything is set will return `0`.
assert_eq!(CountForKitties::<TestRuntime>::get(), 0);
// Call `mint` to create a new kitty.
assert_ok!(PalletKitties::mint(1, [1u8; 32]));
// Now the storage should be `1`
assert_eq!(CountForKitties::<TestRuntime>::get(), 1);
// Let's call it two more times...
assert_ok!(PalletKitties::mint(2, [2u8; 32]));
assert_ok!(PalletKitties::mint(3, [3u8; 32]));
// Now the storage should be `3`
assert_eq!(CountForKitties::<TestRuntime>::get(), 3);
})
}

#[test]
fn mint_errors_when_overflow() {
new_test_ext().execute_with(|| {
// Set the count to the largest value possible.
CountForKitties::<TestRuntime>::set(u32::MAX);
// `create_kitty` should not succeed because of safe math.
assert_noop!(
PalletKitties::create_kitty(RuntimeOrigin::signed(1)),
Error::<TestRuntime>::TooManyKitties
);
})
}

#[test]
fn kitties_map_created_correctly() {
new_test_ext().execute_with(|| {
let zero_key = [0u8; 32];
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), false);
Kitties::<TestRuntime>::insert(zero_key, ());
assert_eq!(Kitties::<TestRuntime>::contains_key(zero_key), true);
})
}

#[test]
fn create_kitty_adds_to_map() {
new_test_ext().execute_with(|| {
assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1)));
assert_eq!(Kitties::<TestRuntime>::iter().count(), 1);
})
}

#[test]
fn cannot_mint_duplicate_kitty() {
new_test_ext().execute_with(|| {
assert_ok!(PalletKitties::mint(1, [0u8; 32]));
assert_noop!(PalletKitties::mint(2, [0u8; 32]), Error::<TestRuntime>::DuplicateKitty);
})
}

0 comments on commit 82bf395

Please sign in to comment.