diff --git a/steps/26/src/tests.rs b/steps/26/src/tests.rs index 401092ae..01b246d1 100644 --- a/steps/26/src/tests.rs +++ b/steps/26/src/tests.rs @@ -84,3 +84,71 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, ()); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} diff --git a/steps/27/src/tests.rs b/steps/27/src/tests.rs index 401092ae..01b246d1 100644 --- a/steps/27/src/tests.rs +++ b/steps/27/src/tests.rs @@ -84,3 +84,71 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, ()); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} diff --git a/steps/28/src/tests.rs b/steps/28/src/tests.rs index 401092ae..93d49214 100644 --- a/steps/28/src/tests.rs +++ b/steps/28/src/tests.rs @@ -84,3 +84,79 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, ()); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { + dna: [0u8; 32], + owner: 1, + }; +} diff --git a/steps/29/src/tests.rs b/steps/29/src/tests.rs index 401092ae..93d49214 100644 --- a/steps/29/src/tests.rs +++ b/steps/29/src/tests.rs @@ -84,3 +84,79 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, ()); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { + dna: [0u8; 32], + owner: 1, + }; +} diff --git a/steps/30/src/tests.rs b/steps/30/src/tests.rs index 401092ae..b8635457 100644 --- a/steps/30/src/tests.rs +++ b/steps/30/src/tests.rs @@ -29,6 +29,8 @@ construct_runtime! { } } +const DEFAULT_KITTY: Kitty = Kitty { dna: [0u8; 32], owner: 1 }; + #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for TestRuntime { type Block = Block; @@ -84,3 +86,97 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, DEFAULT_KITTY); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; +} + +#[test] +fn kitty_struct_has_expected_traits() { + new_test_ext().execute_with(|| { + let kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; + let bytes = kitty.encode(); + let _new_kitty = Kitty::::decode(&mut &bytes[..]).unwrap(); + assert_eq!(Kitty::::max_encoded_len(), 40); + let _info = Kitty::::type_info(); + }) +} + +#[test] +fn mint_stores_owner_in_kitty() { + new_test_ext().execute_with(|| { + assert_ok!(PalletKitties::mint(1337, [42u8; 32])); + let kitty = Kitties::::get([42u8; 32]).unwrap(); + assert_eq!(kitty.owner, 1337); + assert_eq!(kitty.dna, [42u8; 32]); + }) +} diff --git a/steps/31/src/tests.rs b/steps/31/src/tests.rs index 401092ae..b8635457 100644 --- a/steps/31/src/tests.rs +++ b/steps/31/src/tests.rs @@ -29,6 +29,8 @@ construct_runtime! { } } +const DEFAULT_KITTY: Kitty = Kitty { dna: [0u8; 32], owner: 1 }; + #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for TestRuntime { type Block = Block; @@ -84,3 +86,97 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, DEFAULT_KITTY); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; +} + +#[test] +fn kitty_struct_has_expected_traits() { + new_test_ext().execute_with(|| { + let kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; + let bytes = kitty.encode(); + let _new_kitty = Kitty::::decode(&mut &bytes[..]).unwrap(); + assert_eq!(Kitty::::max_encoded_len(), 40); + let _info = Kitty::::type_info(); + }) +} + +#[test] +fn mint_stores_owner_in_kitty() { + new_test_ext().execute_with(|| { + assert_ok!(PalletKitties::mint(1337, [42u8; 32])); + let kitty = Kitties::::get([42u8; 32]).unwrap(); + assert_eq!(kitty.owner, 1337); + assert_eq!(kitty.dna, [42u8; 32]); + }) +} diff --git a/steps/32/src/tests.rs b/steps/32/src/tests.rs index 401092ae..042bfbda 100644 --- a/steps/32/src/tests.rs +++ b/steps/32/src/tests.rs @@ -29,6 +29,8 @@ construct_runtime! { } } +const DEFAULT_KITTY: Kitty = Kitty { dna: [0u8; 32], owner: 1 }; + #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for TestRuntime { type Block = Block; @@ -84,3 +86,110 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, DEFAULT_KITTY); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; +} + +#[test] +fn kitty_struct_has_expected_traits() { + new_test_ext().execute_with(|| { + let kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; + let bytes = kitty.encode(); + let _new_kitty = Kitty::::decode(&mut &bytes[..]).unwrap(); + assert_eq!(Kitty::::max_encoded_len(), 40); + let _info = Kitty::::type_info(); + }) +} + +#[test] +fn mint_stores_owner_in_kitty() { + new_test_ext().execute_with(|| { + assert_ok!(PalletKitties::mint(1337, [42u8; 32])); + let kitty = Kitties::::get([42u8; 32]).unwrap(); + assert_eq!(kitty.owner, 1337); + assert_eq!(kitty.dna, [42u8; 32]); + }) +} + +#[test] +fn create_kitty_makes_unique_kitties() { + new_test_ext().execute_with(|| { + // Two calls to `create_kitty` should work. + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1))); + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2))); + // And should result in two kitties in our system. + assert_eq!(CountForKitties::::get(), 2); + assert_eq!(Kitties::::iter().count(), 2); + + }) +} diff --git a/steps/33/src/tests.rs b/steps/33/src/tests.rs index 401092ae..042bfbda 100644 --- a/steps/33/src/tests.rs +++ b/steps/33/src/tests.rs @@ -29,6 +29,8 @@ construct_runtime! { } } +const DEFAULT_KITTY: Kitty = Kitty { dna: [0u8; 32], owner: 1 }; + #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for TestRuntime { type Block = Block; @@ -84,3 +86,110 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, DEFAULT_KITTY); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; +} + +#[test] +fn kitty_struct_has_expected_traits() { + new_test_ext().execute_with(|| { + let kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; + let bytes = kitty.encode(); + let _new_kitty = Kitty::::decode(&mut &bytes[..]).unwrap(); + assert_eq!(Kitty::::max_encoded_len(), 40); + let _info = Kitty::::type_info(); + }) +} + +#[test] +fn mint_stores_owner_in_kitty() { + new_test_ext().execute_with(|| { + assert_ok!(PalletKitties::mint(1337, [42u8; 32])); + let kitty = Kitties::::get([42u8; 32]).unwrap(); + assert_eq!(kitty.owner, 1337); + assert_eq!(kitty.dna, [42u8; 32]); + }) +} + +#[test] +fn create_kitty_makes_unique_kitties() { + new_test_ext().execute_with(|| { + // Two calls to `create_kitty` should work. + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1))); + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2))); + // And should result in two kitties in our system. + assert_eq!(CountForKitties::::get(), 2); + assert_eq!(Kitties::::iter().count(), 2); + + }) +} diff --git a/steps/34/src/tests.rs b/steps/34/src/tests.rs index 401092ae..6fa89b88 100644 --- a/steps/34/src/tests.rs +++ b/steps/34/src/tests.rs @@ -29,6 +29,8 @@ construct_runtime! { } } +const DEFAULT_KITTY: Kitty = Kitty { dna: [0u8; 32], owner: 1 }; + #[derive_impl(frame_system::config_preludes::TestDefaultConfig)] impl frame_system::Config for TestRuntime { type Block = Block; @@ -84,3 +86,122 @@ fn create_kitty_emits_event() { System::assert_last_event(Event::::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::::get(), 0); + // You can `set` the value using an `u32`. + CountForKitties::::set(1337u32); + // You can `put` the value directly with a `u32`. + CountForKitties::::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::::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::::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::::get(), 3); + }) +} + +#[test] +fn mint_errors_when_overflow() { + new_test_ext().execute_with(|| { + // Set the count to the largest value possible. + CountForKitties::::set(u32::MAX); + // `create_kitty` should not succeed because of safe math. + assert_noop!( + PalletKitties::create_kitty(RuntimeOrigin::signed(1)), + Error::::TooManyKitties + ); + }) +} + +#[test] +fn kitties_map_created_correctly() { + new_test_ext().execute_with(|| { + let zero_key = [0u8; 32]; + assert_eq!(Kitties::::contains_key(zero_key), false); + Kitties::::insert(zero_key, DEFAULT_KITTY); + assert_eq!(Kitties::::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::::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::::DuplicateKitty); + }) +} + +#[test] +fn kitty_struct_created_correctly() { + let _kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; +} + +#[test] +fn kitty_struct_has_expected_traits() { + new_test_ext().execute_with(|| { + let kitty = Kitty:: { dna: [0u8; 32], owner: 1 }; + let bytes = kitty.encode(); + let _new_kitty = Kitty::::decode(&mut &bytes[..]).unwrap(); + assert_eq!(Kitty::::max_encoded_len(), 40); + let _info = Kitty::::type_info(); + }) +} + +#[test] +fn mint_stores_owner_in_kitty() { + new_test_ext().execute_with(|| { + assert_ok!(PalletKitties::mint(1337, [42u8; 32])); + let kitty = Kitties::::get([42u8; 32]).unwrap(); + assert_eq!(kitty.owner, 1337); + assert_eq!(kitty.dna, [42u8; 32]); + }) +} + +#[test] +fn create_kitty_makes_unique_kitties() { + new_test_ext().execute_with(|| { + // Two calls to `create_kitty` should work. + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1))); + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(2))); + // And should result in two kitties in our system. + assert_eq!(CountForKitties::::get(), 2); + assert_eq!(Kitties::::iter().count(), 2); + }) +} + +#[test] +fn kitties_owned_created_correctly() { + new_test_ext().execute_with(|| { + // Initially users have no kitties owned. + assert_eq!(KittiesOwned::::get(1).len(), 0); + // Let's create two kitties. + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1))); + assert_ok!(PalletKitties::create_kitty(RuntimeOrigin::signed(1))); + // Now they should have two kitties owned. + assert_eq!(KittiesOwned::::get(1).len(), 2); + }); +}