123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904 |
- use super::curators;
- use super::mock::*;
- use crate::*;
- use frame_support::assert_ok;
- use frame_support::traits::Currency;
- // type aliases
- type AccountId = <Test as frame_system::Trait>::AccountId;
- type VideoId = <Test as Trait>::VideoId;
- // fixtures
- pub struct CreateChannelFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- params: ChannelCreationParameters<Test>,
- }
- impl CreateChannelFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ID),
- params: ChannelCreationParameters::<Test> {
- assets: None,
- meta: None,
- reward_account: None,
- collaborators: BTreeSet::new(),
- },
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_assets(self, assets: StorageAssets<Test>) -> Self {
- Self {
- params: ChannelCreationParameters::<Test> {
- assets: Some(assets),
- ..self.params
- },
- ..self
- }
- }
- pub fn with_collaborators(self, collaborators: BTreeSet<MemberId>) -> Self {
- Self {
- params: ChannelCreationParameters::<Test> {
- collaborators: collaborators,
- ..self.params
- },
- ..self
- }
- }
- pub fn with_reward_account(self, reward_account: AccountId) -> Self {
- Self {
- params: ChannelCreationParameters::<Test> {
- reward_account: Some(reward_account),
- ..self.params
- },
- ..self
- }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let channel_id = Content::next_channel_id();
- let channel_bag_id = Content::bag_id_for_channel(&channel_id);
- let beg_obj_id = storage::NextDataObjectId::<Test>::get();
- let actual_result =
- Content::create_channel(origin, self.actor.clone(), self.params.clone());
- let end_obj_id = storage::NextDataObjectId::<Test>::get();
- assert_eq!(actual_result, expected_result);
- let balance_post = Balances::usable_balance(self.sender);
- if actual_result.is_ok() {
- // ensure channel is on chain
- assert!(ChannelById::<Test>::contains_key(&channel_id));
- // channel counter increased
- assert_eq!(
- Content::next_channel_id(),
- channel_id.saturating_add(One::one())
- );
- // dynamic bag for channel is created
- assert_ok!(Storage::<Test>::ensure_bag_exists(&channel_bag_id));
- // event correctly deposited
- let owner = Content::actor_to_channel_owner(&self.actor).unwrap();
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::ChannelCreated(
- self.actor.clone(),
- channel_id,
- ChannelRecord {
- owner: owner,
- is_censored: false,
- reward_account: self.params.reward_account.clone(),
- collaborators: self.params.collaborators.clone(),
- num_videos: Zero::zero(),
- },
- self.params.clone(),
- ))
- );
- if let Some(assets) = self.params.assets.as_ref() {
- // balance accounting is correct
- let bag_deletion_prize = BalanceOf::<Test>::zero();
- let objects_deletion_prize =
- assets
- .object_creation_list
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, _| {
- acc.saturating_add(
- <Test as storage::Trait>::DataObjectDeletionPrize::get(),
- )
- });
- assert_eq!(
- balance_pre.saturating_sub(balance_post),
- bag_deletion_prize.saturating_add(objects_deletion_prize),
- );
- assert!((beg_obj_id..end_obj_id).all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, id)
- }));
- }
- } else {
- assert_eq!(balance_post, balance_pre);
- assert_eq!(beg_obj_id, end_obj_id);
- assert!(!storage::Bags::<Test>::contains_key(&channel_bag_id));
- assert!(!ChannelById::<Test>::contains_key(&channel_id));
- assert_eq!(NextChannelId::<Test>::get(), channel_id);
- }
- }
- }
- pub struct CreateVideoFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- params: VideoCreationParameters<Test>,
- channel_id: ChannelId,
- }
- impl CreateVideoFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ACCOUNT_ID),
- params: VideoCreationParameters::<Test> {
- assets: None,
- meta: None,
- },
- channel_id: ChannelId::one(), // channel index starts at 1
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_channel_id(self, channel_id: ChannelId) -> Self {
- Self { channel_id, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_assets(self, assets: StorageAssets<Test>) -> Self {
- Self {
- params: VideoCreationParameters::<Test> {
- assets: Some(assets),
- ..self.params
- },
- ..self
- }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let channel_bag_id = Content::bag_id_for_channel(&self.channel_id);
- let video_id = Content::next_video_id();
- let beg_obj_id = storage::NextDataObjectId::<Test>::get();
- let actual_result = Content::create_video(
- origin,
- self.actor.clone(),
- self.channel_id,
- self.params.clone(),
- );
- let balance_post = Balances::usable_balance(self.sender);
- let end_obj_id = storage::NextDataObjectId::<Test>::get();
- assert_eq!(actual_result, expected_result);
- if actual_result.is_ok() {
- assert!(VideoById::<Test>::contains_key(&video_id));
- assert_eq!(
- Content::next_video_id(),
- video_id.saturating_add(One::one())
- );
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::VideoCreated(
- self.actor,
- self.channel_id,
- video_id,
- self.params.clone(),
- ))
- );
- if let Some(assets) = self.params.assets.as_ref() {
- // balance accounting is correct
- let bag_deletion_prize = BalanceOf::<Test>::zero();
- let objects_deletion_prize =
- assets
- .object_creation_list
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, _| {
- acc.saturating_add(
- <Test as storage::Trait>::DataObjectDeletionPrize::get(),
- )
- });
- assert_eq!(
- balance_pre.saturating_sub(balance_post),
- bag_deletion_prize.saturating_add(objects_deletion_prize),
- );
- assert!((beg_obj_id..end_obj_id).all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, id)
- }));
- }
- } else {
- assert!(!VideoById::<Test>::contains_key(&video_id));
- assert_eq!(Content::next_video_id(), video_id);
- if self.params.assets.is_some() {
- assert_eq!(balance_pre, balance_post,);
- assert!(!(beg_obj_id..end_obj_id).any(|id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, id)
- }));
- }
- }
- }
- }
- pub struct UpdateChannelFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- channel_id: ChannelId,
- params: ChannelUpdateParameters<Test>,
- }
- impl UpdateChannelFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ACCOUNT_ID),
- channel_id: ChannelId::one(), // channel index starts at 1
- params: ChannelUpdateParameters::<Test> {
- assets_to_upload: None,
- new_meta: None,
- reward_account: None,
- assets_to_remove: BTreeSet::new(),
- collaborators: None,
- },
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_channel_id(self, channel_id: ChannelId) -> Self {
- Self { channel_id, ..self }
- }
- pub fn with_assets_to_upload(self, assets: StorageAssets<Test>) -> Self {
- Self {
- params: ChannelUpdateParameters::<Test> {
- assets_to_upload: Some(assets),
- ..self.params
- },
- ..self
- }
- }
- pub fn with_assets_to_remove(self, assets: BTreeSet<DataObjectId<Test>>) -> Self {
- Self {
- params: ChannelUpdateParameters::<Test> {
- assets_to_remove: assets,
- ..self.params
- },
- ..self
- }
- }
- pub fn with_collaborators(self, collaborators: BTreeSet<MemberId>) -> Self {
- Self {
- params: ChannelUpdateParameters::<Test> {
- collaborators: Some(collaborators),
- ..self.params
- },
- ..self
- }
- }
- pub fn with_reward_account(self, reward_account: Option<Option<AccountId>>) -> Self {
- Self {
- params: ChannelUpdateParameters::<Test> {
- reward_account,
- ..self.params
- },
- ..self
- }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let channel_pre = Content::channel_by_id(&self.channel_id);
- let bag_id_for_channel = Content::bag_id_for_channel(&self.channel_id);
- let deletion_prize_deposited =
- self.params
- .assets_to_upload
- .as_ref()
- .map_or(BalanceOf::<Test>::zero(), |assets| {
- assets
- .object_creation_list
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, _| {
- acc.saturating_add(
- <Test as storage::Trait>::DataObjectDeletionPrize::get(),
- )
- })
- });
- let deletion_prize_withdrawn = if !self.params.assets_to_remove.is_empty() {
- self.params
- .assets_to_remove
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, id| {
- acc + storage::DataObjectsById::<Test>::get(&bag_id_for_channel, id)
- .deletion_prize
- })
- } else {
- BalanceOf::<Test>::zero()
- };
- let beg_obj_id = storage::NextDataObjectId::<Test>::get();
- let actual_result = Content::update_channel(
- origin,
- self.actor.clone(),
- self.channel_id,
- self.params.clone(),
- );
- let channel_post = Content::channel_by_id(&self.channel_id);
- let end_obj_id = storage::NextDataObjectId::<Test>::get();
- let balance_post = Balances::usable_balance(self.sender);
- assert_eq!(actual_result, expected_result);
- match actual_result {
- Ok(()) => {
- let owner = channel_post.owner.clone();
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::ChannelUpdated(
- self.actor.clone(),
- self.channel_id,
- ChannelRecord {
- owner: owner,
- is_censored: channel_pre.is_censored,
- reward_account: self
- .params
- .reward_account
- .clone()
- .unwrap_or(channel_pre.reward_account),
- collaborators: self
- .params
- .collaborators
- .clone()
- .unwrap_or(channel_pre.collaborators),
- num_videos: channel_pre.num_videos,
- },
- self.params.clone(),
- ))
- );
- assert_eq!(
- balance_post.saturating_sub(balance_pre),
- deletion_prize_withdrawn.saturating_sub(deletion_prize_deposited),
- );
- if self.params.assets_to_upload.is_some() {
- assert!((beg_obj_id..end_obj_id).all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- }
- assert!(!self.params.assets_to_remove.iter().any(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- }
- Err(err) => {
- assert_eq!(channel_pre, channel_post);
- assert_eq!(balance_pre, balance_post);
- assert_eq!(beg_obj_id, end_obj_id);
- if err != storage::Error::<Test>::DataObjectDoesntExist.into() {
- assert!(self.params.assets_to_remove.iter().all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }))
- }
- }
- }
- }
- }
- pub struct UpdateVideoFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- video_id: VideoId,
- params: VideoUpdateParameters<Test>,
- }
- impl UpdateVideoFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ID),
- video_id: VideoId::one(),
- params: VideoUpdateParameters::<Test> {
- assets_to_upload: None,
- assets_to_remove: BTreeSet::new(),
- new_meta: None,
- },
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_video_id(self, video_id: VideoId) -> Self {
- Self { video_id, ..self }
- }
- pub fn with_assets_to_upload(self, assets: StorageAssets<Test>) -> Self {
- Self {
- params: VideoUpdateParameters::<Test> {
- assets_to_upload: Some(assets),
- ..self.params
- },
- ..self
- }
- }
- pub fn with_assets_to_remove(self, assets: BTreeSet<DataObjectId<Test>>) -> Self {
- Self {
- params: VideoUpdateParameters::<Test> {
- assets_to_remove: assets,
- ..self.params
- },
- ..self
- }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let video_pre = Content::video_by_id(&self.video_id);
- let bag_id_for_channel = Content::bag_id_for_channel(&video_pre.in_channel);
- let beg_obj_id = storage::NextDataObjectId::<Test>::get();
- let deletion_prize_deposited =
- self.params
- .assets_to_upload
- .as_ref()
- .map_or(BalanceOf::<Test>::zero(), |assets| {
- assets
- .object_creation_list
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, _| {
- acc.saturating_add(
- <Test as storage::Trait>::DataObjectDeletionPrize::get(),
- )
- })
- });
- let deletion_prize_withdrawn = if !self.params.assets_to_remove.is_empty() {
- self.params
- .assets_to_remove
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, obj_id| {
- acc + storage::DataObjectsById::<Test>::get(&bag_id_for_channel, obj_id)
- .deletion_prize
- })
- } else {
- BalanceOf::<Test>::zero()
- };
- let actual_result = Content::update_video(
- origin,
- self.actor.clone(),
- self.video_id,
- self.params.clone(),
- );
- let end_obj_id = storage::NextDataObjectId::<Test>::get();
- let balance_post = Balances::usable_balance(self.sender);
- let video_post = Content::video_by_id(&self.video_id);
- assert_eq!(actual_result, expected_result);
- match actual_result {
- Ok(()) => {
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::VideoUpdated(
- self.actor.clone(),
- self.video_id,
- self.params.clone()
- ))
- );
- assert_eq!(
- balance_post.saturating_sub(balance_pre),
- deletion_prize_withdrawn.saturating_sub(deletion_prize_deposited),
- );
- if self.params.assets_to_upload.is_some() {
- assert!((beg_obj_id..end_obj_id).all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- }
- assert!(!self.params.assets_to_remove.iter().any(|obj_id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, obj_id)
- }));
- }
- Err(err) => {
- assert_eq!(video_pre, video_post);
- assert_eq!(balance_pre, balance_post);
- assert_eq!(beg_obj_id, end_obj_id);
- if err != storage::Error::<Test>::DataObjectDoesntExist.into() {
- assert!(self.params.assets_to_remove.iter().all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- }
- }
- }
- }
- }
- pub struct DeleteChannelFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- channel_id: ChannelId,
- num_objects_to_delete: u64,
- }
- impl DeleteChannelFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ID),
- channel_id: ChannelId::one(),
- num_objects_to_delete: DATA_OBJECTS_NUMBER as u64,
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_num_objects_to_delete(self, num_objects_to_delete: u64) -> Self {
- Self {
- num_objects_to_delete,
- ..self
- }
- }
- pub fn with_channel_id(self, channel_id: ChannelId) -> Self {
- Self { channel_id, ..self }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let bag_id_for_channel = Content::bag_id_for_channel(&self.channel_id);
- let bag_deletion_prize = storage::Bags::<Test>::get(&bag_id_for_channel)
- .deletion_prize
- .unwrap_or(BalanceOf::<Test>::zero());
- let objects_deletion_prize =
- storage::DataObjectsById::<Test>::iter_prefix(&bag_id_for_channel)
- .fold(BalanceOf::<Test>::zero(), |acc, (_, obj)| {
- acc + obj.deletion_prize
- });
- let channel_objects_ids =
- storage::DataObjectsById::<Test>::iter_prefix(&bag_id_for_channel)
- .map(|(id, _)| id)
- .collect::<BTreeSet<_>>();
- let actual_result = Content::delete_channel(
- origin,
- self.actor.clone(),
- self.channel_id,
- self.num_objects_to_delete,
- );
- let balance_post = Balances::usable_balance(self.sender);
- assert_eq!(actual_result, expected_result);
- match actual_result {
- Ok(()) => {
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::ChannelDeleted(
- self.actor.clone(),
- self.channel_id,
- ))
- );
- let deletion_prize = bag_deletion_prize.saturating_add(objects_deletion_prize);
- assert_eq!(balance_post.saturating_sub(balance_pre), deletion_prize,);
- assert!(!<ChannelById<Test>>::contains_key(&self.channel_id));
- assert!(!channel_objects_ids.iter().any(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- assert!(!storage::Bags::<Test>::contains_key(&bag_id_for_channel));
- }
- Err(err) => {
- assert_eq!(balance_pre, balance_post);
- if err != Error::<Test>::ChannelDoesNotExist.into() {
- assert!(ChannelById::<Test>::contains_key(&self.channel_id));
- assert!(channel_objects_ids.iter().all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&bag_id_for_channel, id)
- }));
- assert!(storage::Bags::<Test>::contains_key(&bag_id_for_channel));
- }
- }
- }
- }
- }
- pub struct DeleteVideoFixture {
- sender: AccountId,
- actor: ContentActor<CuratorGroupId, CuratorId, MemberId>,
- video_id: VideoId,
- assets_to_remove: BTreeSet<DataObjectId<Test>>,
- }
- impl DeleteVideoFixture {
- pub fn default() -> Self {
- Self {
- sender: DEFAULT_MEMBER_ACCOUNT_ID,
- actor: ContentActor::Member(DEFAULT_MEMBER_ID),
- video_id: VideoId::one(),
- assets_to_remove: BTreeSet::new(),
- }
- }
- pub fn with_sender(self, sender: AccountId) -> Self {
- Self { sender, ..self }
- }
- pub fn with_actor(self, actor: ContentActor<CuratorGroupId, CuratorId, MemberId>) -> Self {
- Self { actor, ..self }
- }
- pub fn with_assets_to_remove(self, assets_to_remove: BTreeSet<DataObjectId<Test>>) -> Self {
- Self {
- assets_to_remove,
- ..self
- }
- }
- pub fn with_video_id(self, video_id: VideoId) -> Self {
- Self { video_id, ..self }
- }
- pub fn call_and_assert(&self, expected_result: DispatchResult) {
- let origin = Origin::signed(self.sender.clone());
- let balance_pre = Balances::usable_balance(self.sender);
- let video_pre = <VideoById<Test>>::get(&self.video_id);
- let channel_bag_id = Content::bag_id_for_channel(&video_pre.in_channel);
- let deletion_prize =
- self.assets_to_remove
- .iter()
- .fold(BalanceOf::<Test>::zero(), |acc, obj_id| {
- acc + storage::DataObjectsById::<Test>::get(&channel_bag_id, obj_id)
- .deletion_prize
- });
- let actual_result = Content::delete_video(
- origin,
- self.actor.clone(),
- self.video_id,
- self.assets_to_remove.clone(),
- );
- let balance_post = Balances::usable_balance(self.sender);
- assert_eq!(actual_result, expected_result);
- match actual_result {
- Ok(()) => {
- assert_eq!(
- System::events().last().unwrap().event,
- MetaEvent::content(RawEvent::VideoDeleted(self.actor.clone(), self.video_id,))
- );
- assert_eq!(balance_post.saturating_sub(balance_pre), deletion_prize);
- assert!(!self.assets_to_remove.iter().any(|obj_id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, obj_id)
- }));
- assert!(!<VideoById<Test>>::contains_key(&self.video_id));
- }
- Err(err) => {
- assert_eq!(balance_pre, balance_post);
- if err == storage::Error::<Test>::DataObjectDoesntExist.into() {
- let video_post = <VideoById<Test>>::get(&self.video_id);
- assert_eq!(video_pre, video_post);
- assert!(VideoById::<Test>::contains_key(&self.video_id));
- } else if err == Error::<Test>::VideoDoesNotExist.into() {
- assert!(self.assets_to_remove.iter().all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, id)
- }));
- } else {
- let video_post = <VideoById<Test>>::get(&self.video_id);
- assert_eq!(video_pre, video_post);
- assert!(VideoById::<Test>::contains_key(&self.video_id));
- assert!(self.assets_to_remove.iter().all(|id| {
- storage::DataObjectsById::<Test>::contains_key(&channel_bag_id, id)
- }));
- }
- }
- }
- }
- }
- // helper functions
- pub fn increase_account_balance_helper(account_id: u64, balance: u64) {
- let _ = Balances::deposit_creating(&account_id, balance);
- }
- pub fn slash_account_balance_helper(account_id: u64) {
- let _ = Balances::slash(&account_id, Balances::total_balance(&account_id));
- }
- pub fn create_data_object_candidates_helper(
- starting_ipfs_index: u8,
- number: u64,
- ) -> Vec<DataObjectCreationParameters> {
- let range = (starting_ipfs_index as u64)..((starting_ipfs_index as u64) + number);
- range
- .into_iter()
- .map(|_| DataObjectCreationParameters {
- size: DEFAULT_OBJECT_SIZE,
- ipfs_content_id: vec![1u8],
- })
- .collect()
- }
- pub fn create_data_objects_helper() -> Vec<DataObjectCreationParameters> {
- create_data_object_candidates_helper(1, DATA_OBJECTS_NUMBER)
- }
- pub fn create_initial_storage_buckets_helper() {
- // first set limits
- assert_eq!(
- Storage::<Test>::update_storage_buckets_voucher_max_limits(
- Origin::signed(STORAGE_WG_LEADER_ACCOUNT_ID),
- VOUCHER_OBJECTS_SIZE_LIMIT,
- VOUCHER_OBJECTS_NUMBER_LIMIT,
- ),
- Ok(())
- );
- // create bucket(s)
- assert_eq!(
- Storage::<Test>::create_storage_bucket(
- Origin::signed(STORAGE_WG_LEADER_ACCOUNT_ID),
- None,
- STORAGE_BUCKET_ACCEPTING_BAGS,
- STORAGE_BUCKET_OBJECTS_SIZE_LIMIT,
- STORAGE_BUCKET_OBJECTS_NUMBER_LIMIT,
- ),
- Ok(())
- );
- }
- pub fn create_default_member_owned_channel() {
- CreateChannelFixture::default()
- .with_sender(DEFAULT_MEMBER_ACCOUNT_ID)
- .with_actor(ContentActor::Member(DEFAULT_MEMBER_ID))
- .with_assets(StorageAssets::<Test> {
- expected_data_size_fee: Storage::<Test>::data_object_per_mega_byte_fee(),
- object_creation_list: create_data_objects_helper(),
- })
- .with_reward_account(DEFAULT_MEMBER_ACCOUNT_ID)
- .with_collaborators(vec![COLLABORATOR_MEMBER_ID].into_iter().collect())
- .call_and_assert(Ok(()));
- }
- pub fn create_default_curator_owned_channel() {
- let curator_group_id = curators::add_curator_to_new_group(DEFAULT_CURATOR_ID);
- CreateChannelFixture::default()
- .with_sender(DEFAULT_CURATOR_ACCOUNT_ID)
- .with_actor(ContentActor::Curator(curator_group_id, DEFAULT_CURATOR_ID))
- .with_assets(StorageAssets::<Test> {
- expected_data_size_fee: Storage::<Test>::data_object_per_mega_byte_fee(),
- object_creation_list: create_data_objects_helper(),
- })
- .with_reward_account(DEFAULT_CURATOR_ACCOUNT_ID)
- .with_collaborators(vec![COLLABORATOR_MEMBER_ID].into_iter().collect())
- .call_and_assert(Ok(()));
- }
- pub fn create_default_member_owned_channel_with_video() {
- create_default_member_owned_channel();
- CreateVideoFixture::default()
- .with_sender(DEFAULT_MEMBER_ACCOUNT_ID)
- .with_actor(ContentActor::Member(DEFAULT_MEMBER_ID))
- .with_assets(StorageAssets::<Test> {
- expected_data_size_fee: Storage::<Test>::data_object_per_mega_byte_fee(),
- object_creation_list: create_data_objects_helper(),
- })
- .with_channel_id(NextChannelId::<Test>::get() - 1)
- .call_and_assert(Ok(()));
- }
- pub fn create_default_curator_owned_channel_with_video() {
- create_default_curator_owned_channel();
- let curator_group_id = NextCuratorGroupId::<Test>::get() - 1;
- CreateVideoFixture::default()
- .with_sender(DEFAULT_CURATOR_ACCOUNT_ID)
- .with_actor(ContentActor::Curator(curator_group_id, DEFAULT_CURATOR_ID))
- .with_assets(StorageAssets::<Test> {
- expected_data_size_fee: Storage::<Test>::data_object_per_mega_byte_fee(),
- object_creation_list: create_data_objects_helper(),
- })
- .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),
- )
- }
|