Browse Source

fixed tests

ignazio 3 years ago
parent
commit
40c4dd1ec3

+ 1 - 1
runtime-modules/content/src/lib.rs

@@ -798,7 +798,7 @@ decl_module! {
             let sender = ensure_signed(origin)?;
 
             // check that channel exists
-            let channel = Self::ensure_channel_validity(&channel_id)?;
+            let mut channel = Self::ensure_channel_validity(&channel_id)?;
 
             ensure_actor_authorized_to_update_channel_assets::<T>(
                 &sender,

+ 28 - 15
runtime-modules/content/src/tests/fixtures.rs

@@ -4,14 +4,6 @@ use crate::*;
 use frame_support::assert_ok;
 use frame_support::traits::Currency;
 
-// constants used
-pub const VOUCHER_OBJECTS_NUMBER_LIMIT: u64 = 40;
-pub const VOUCHER_OBJECTS_SIZE_LIMIT: u64 = 400;
-pub const STORAGE_BUCKET_OBJECTS_NUMBER_LIMIT: u64 = 10;
-pub const STORAGE_BUCKET_OBJECTS_SIZE_LIMIT: u64 = 100;
-pub const STORAGE_BUCKET_ACCEPTING_BAGS: bool = true;
-pub const DATA_OBJECTS_NUMBER: u8 = 2;
-
 // type aliases
 type AccountId = <Test as frame_system::Trait>::AccountId;
 type VideoId = <Test as Trait>::VideoId;
@@ -91,7 +83,7 @@ impl CreateChannelFixture {
 
         if actual_result.is_ok() {
             // ensure channel is on chain
-            assert_ok!(Content::ensure_channel_exists(&channel_id));
+            assert!(ChannelById::<Test>::contains_key(&channel_id));
 
             // channel counter increased
             assert_eq!(
@@ -214,7 +206,7 @@ impl CreateVideoFixture {
         assert_eq!(actual_result, expected_result);
 
         if actual_result.is_ok() {
-            assert_ok!(Content::ensure_video_exists(&video_id));
+            assert!(VideoById::<Test>::contains_key(&video_id));
 
             assert_eq!(
                 Content::next_video_id(),
@@ -794,15 +786,15 @@ pub fn slash_account_balance_helper(account_id: u64) {
 
 pub fn create_data_object_candidates_helper(
     starting_ipfs_index: u8,
-    number: u8,
+    number: u64,
 ) -> Vec<DataObjectCreationParameters> {
-    let range = starting_ipfs_index..(starting_ipfs_index + number);
+    let range = (starting_ipfs_index as u64)..((starting_ipfs_index as u64) + number);
 
     range
         .into_iter()
-        .map(|idx| DataObjectCreationParameters {
-            size: 10 * idx as u64,
-            ipfs_content_id: vec![idx],
+        .map(|_| DataObjectCreationParameters {
+            size: DEFAULT_OBJECT_SIZE,
+            ipfs_content_id: vec![1u8],
         })
         .collect()
 }
@@ -889,3 +881,24 @@ pub fn create_default_curator_owned_channel_with_video() {
         .with_channel_id(NextChannelId::<Test>::get() - 1)
         .call_and_assert(Ok(()));
 }
+
+pub fn create_default_member_owned_channels_with_videos(n: u64) -> (u64, u64) {
+    for _ in 0..n {
+        create_default_member_owned_channel_with_video();
+    }
+
+    // assert that the specified channels have been created
+    assert_eq!(VideoById::<Test>::iter().count() as u64, n);
+    assert_eq!(ChannelById::<Test>::iter().count() as u64, n);
+
+    let channels_migrations_per_block = <Test as Trait>::ChannelsMigrationsEachBlock::get();
+    let videos_migrations_per_block = <Test as Trait>::VideosMigrationsEachBlock::get();
+
+    // return the number of blocks required for migration
+    let divide_with_ceiling =
+        |x: u64, y: u64| (x / y) + ((x.checked_rem(y).unwrap_or_default() > 0u64) as u64);
+    (
+        divide_with_ceiling(n, channels_migrations_per_block),
+        divide_with_ceiling(n, videos_migrations_per_block),
+    )
+}

+ 23 - 72
runtime-modules/content/src/tests/migration.rs

@@ -1,9 +1,7 @@
 #![cfg(test)]
-
+use super::fixtures::*;
 use super::mock::*;
-use crate::sp_api_hidden_includes_decl_storage::hidden_include::traits::Currency;
 use crate::*;
-use std::ops::Rem;
 
 fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
     let params = VideoCreationParametersRecord {
@@ -27,12 +25,13 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
 
     assert_eq!(
         Content::create_channel(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             ChannelCreationParametersRecord {
                 assets: None,
                 meta: Some(vec![]),
                 reward_account: None,
+                collaborators: BTreeSet::new(),
             },
         ),
         result
@@ -40,8 +39,8 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
 
     assert_eq!(
         Content::create_video(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             channel_id.clone(),
             params.clone()
         ),
@@ -49,22 +48,23 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
     );
     assert_eq!(
         Content::update_channel(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             channel_id.clone(),
             ChannelUpdateParametersRecord {
                 assets_to_upload: None,
                 new_meta: Some(vec![]),
                 reward_account: None,
                 assets_to_remove: BTreeSet::new(),
+                collaborators: None,
             },
         ),
         result
     );
     assert_eq!(
         Content::update_video(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             video_id.clone(),
             VideoUpdateParametersRecord {
                 assets_to_upload: None,
@@ -77,8 +77,8 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
 
     assert_eq!(
         Content::update_channel_censorship_status(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             channel_id.clone(),
             false,
             b"test".to_vec()
@@ -88,8 +88,8 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
 
     assert_eq!(
         Content::update_video_censorship_status(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             video_id.clone(),
             false,
             b"test".to_vec()
@@ -99,8 +99,8 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
 
     assert_eq!(
         Content::delete_video(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             video_id.clone(),
             BTreeSet::new(),
         ),
@@ -108,8 +108,8 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
     );
     assert_eq!(
         Content::delete_channel(
-            Origin::signed(FIRST_MEMBER_ORIGIN),
-            ContentActor::Member(FIRST_MEMBER_ID),
+            Origin::signed(DEFAULT_MEMBER_ACCOUNT_ID),
+            ContentActor::Member(DEFAULT_MEMBER_ID),
             channel_id.clone(),
             0u64,
         ),
@@ -117,58 +117,6 @@ fn assert_video_and_channel_existrinsics_with(result: DispatchResult) {
     );
 }
 
-fn setup_scenario_with(n_videos: u64, n_channels: u64) -> (u64, u64) {
-    let _ = balances::Module::<Test>::deposit_creating(
-        &FIRST_MEMBER_ORIGIN,
-        <Test as balances::Trait>::Balance::from(10_000u32),
-    );
-
-    // create n_channels channels
-    for _ in 0..n_channels {
-        create_channel_mock(
-            FIRST_MEMBER_ORIGIN,
-            ContentActor::Member(FIRST_MEMBER_ID),
-            ChannelCreationParametersRecord {
-                assets: None,
-                meta: Some(vec![]),
-                reward_account: None,
-            },
-            Ok(()),
-        );
-    }
-
-    let params = VideoCreationParametersRecord {
-        assets: None,
-        meta: None,
-    };
-
-    // create n_videos videos
-    for i in 0..n_videos {
-        create_video_mock(
-            FIRST_MEMBER_ORIGIN,
-            ContentActor::Member(FIRST_MEMBER_ID),
-            i.rem(n_channels) + 1,
-            params.clone(),
-            Ok(()),
-        );
-    }
-
-    // assert that the specified channels have been created
-    assert_eq!(VideoById::<Test>::iter().count() as u64, n_videos);
-    assert_eq!(ChannelById::<Test>::iter().count() as u64, n_channels);
-
-    let channels_migrations_per_block = <Test as Trait>::ChannelsMigrationsEachBlock::get();
-    let videos_migrations_per_block = <Test as Trait>::VideosMigrationsEachBlock::get();
-
-    // return the number of blocks required for migration
-    let divide_with_ceiling =
-        |x: u64, y: u64| (x / y) + ((x.checked_rem(y).unwrap_or_default() > 0u64) as u64);
-    (
-        divide_with_ceiling(n_channels, channels_migrations_per_block),
-        divide_with_ceiling(n_videos, videos_migrations_per_block),
-    )
-}
-
 #[test]
 fn migration_test() {
     with_default_mock_builder(|| {
@@ -176,7 +124,10 @@ fn migration_test() {
         run_to_block(START_MIGRATION_AT_BLOCK);
 
         // setup scenario
-        let (blocks_channels, blocks_videos) = setup_scenario_with(100u64, 100u64);
+        increase_account_balance_helper(DEFAULT_MEMBER_ACCOUNT_ID, INITIAL_BALANCE);
+        create_initial_storage_buckets_helper();
+        let (blocks_channels, blocks_videos) =
+            create_default_member_owned_channels_with_videos(NUMBER_OF_CHANNELS_VIDEOS);
 
         // block at which all migrations should be completed
         let last_migration_block = std::cmp::max(blocks_channels, blocks_videos);

+ 10 - 1
runtime-modules/content/src/tests/mock.rs

@@ -41,8 +41,17 @@ pub const UNAUTHORIZED_MEMBER_ID: u64 = 205;
 pub const UNAUTHORIZED_CURATOR_ID: u64 = 206;
 pub const UNAUTHORIZED_COLLABORATOR_MEMBER_ID: u64 = 207;
 
-// initial balancer for an account
 pub const INITIAL_BALANCE: u64 = 1000;
+pub const DATA_OBJECTS_NUMBER: u64 = 10;
+pub const DEFAULT_OBJECT_SIZE: u64 = 5;
+pub const STORAGE_BUCKET_OBJECTS_NUMBER_LIMIT: u64 = 4 * DATA_OBJECTS_NUMBER;
+pub const STORAGE_BUCKET_OBJECTS_SIZE_LIMIT: u64 =
+    STORAGE_BUCKET_OBJECTS_NUMBER_LIMIT * DEFAULT_OBJECT_SIZE;
+pub const VOUCHER_OBJECTS_NUMBER_LIMIT: u64 = 2 * STORAGE_BUCKET_OBJECTS_NUMBER_LIMIT;
+pub const VOUCHER_OBJECTS_SIZE_LIMIT: u64 = 2 * STORAGE_BUCKET_OBJECTS_SIZE_LIMIT;
+pub const STORAGE_BUCKET_ACCEPTING_BAGS: bool = true;
+pub const NUMBER_OF_CHANNELS_VIDEOS: u64 =
+    STORAGE_BUCKET_OBJECTS_SIZE_LIMIT / (2 * DEFAULT_OBJECT_SIZE) - 1;
 
 impl_outer_origin! {
     pub enum Origin for Test {}

+ 5 - 5
runtime-modules/content/src/tests/videos.rs

@@ -885,7 +885,7 @@ fn unsuccessful_video_update_with_max_object_size_limits_exceeded() {
             .with_assets_to_upload(StorageAssets::<Test> {
                 expected_data_size_fee: Storage::<Test>::data_object_per_mega_byte_fee(),
                 object_creation_list: vec![DataObjectCreationParameters {
-                    size: VOUCHER_OBJECTS_SIZE_LIMIT + 1,
+                    size: <Test as storage::Trait>::MaxDataObjectSize::get() + 1,
                     ipfs_content_id: vec![1u8],
                 }],
             })
@@ -901,8 +901,8 @@ fn unsuccessful_video_update_with_invalid_object_ids() {
         create_initial_storage_buckets_helper();
         increase_account_balance_helper(DEFAULT_MEMBER_ACCOUNT_ID, INITIAL_BALANCE);
         create_default_member_owned_channel_with_video();
-        let invalid_objects_ids = ((2 * DATA_OBJECTS_NUMBER as u64)
-            ..(3 * DATA_OBJECTS_NUMBER as u64 - 1))
+        let invalid_objects_ids = (1..DATA_OBJECTS_NUMBER)
+            .map(|i| Storage::<Test>::next_data_object_id() + i)
             .collect::<BTreeSet<_>>();
 
         UpdateVideoFixture::default()
@@ -1129,8 +1129,8 @@ fn unsuccessful_video_deletion_with_invalid_object_ids() {
         create_initial_storage_buckets_helper();
         increase_account_balance_helper(DEFAULT_MEMBER_ACCOUNT_ID, INITIAL_BALANCE);
         create_default_member_owned_channel_with_video();
-        let invalid_objects_ids = ((2 * DATA_OBJECTS_NUMBER as u64)
-            ..(3 * DATA_OBJECTS_NUMBER as u64 - 1))
+        let invalid_objects_ids = (1..DATA_OBJECTS_NUMBER)
+            .map(|i| Storage::<Test>::next_data_object_id() + i)
             .collect::<BTreeSet<_>>();
 
         DeleteVideoFixture::default()