Эх сурвалжийг харах

Storage: add_content test coverage upgrade

iorveth 4 жил өмнө
parent
commit
81ae4b6a76

+ 115 - 0
runtime-modules/storage/src/tests/data_directory.rs

@@ -81,6 +81,121 @@ fn add_content_uploading_blocked() {
         });
 }
 
+#[test]
+fn add_content_size_limit_reached() {
+    with_default_mock_builder(|| {
+        let sender = 1u64;
+
+        let owner = StorageObjectOwner::Member(1u64);
+
+        let content_parameters = ContentParameters {
+            content_id: 1,
+            type_id: 1234,
+            size: DefaultQuota::get().size_limit + 1,
+            ipfs_content_id: vec![1, 2, 3, 4],
+        };
+
+        // Make an attempt to register a content, when uploading is blocked.
+        let res =
+            TestDataDirectory::add_content(Origin::signed(sender), owner, vec![content_parameters]);
+        assert_eq!(res, Err(Error::<Test>::QuotaSizeLimitExceeded.into()));
+    });
+}
+
+#[test]
+fn add_content_objects_limit_reached() {
+    with_default_mock_builder(|| {
+        let sender = 1u64;
+
+        let owner = StorageObjectOwner::Member(1u64);
+
+        let mut content = vec![];
+
+        for i in 0..=DefaultQuota::get().objects_limit {
+            let content_parameters = ContentParameters {
+                content_id: i + 1,
+                type_id: 1234,
+                size: 0,
+                ipfs_content_id: vec![1, 2, 3, 4],
+            };
+            content.push(content_parameters);
+        }
+
+        // Make an attempt to register a content, when uploading is blocked.
+        let res = TestDataDirectory::add_content(Origin::signed(sender), owner, content);
+        assert_eq!(res, Err(Error::<Test>::QuotaObjectsLimitExceeded.into()));
+    });
+}
+
+#[test]
+fn add_content_global_size_limit_reached() {
+    let global_quota_size_limit = 0;
+    let global_quota_objects_limit = 50;
+
+    ExtBuilder::default()
+        .global_quota(Quota::new(
+            global_quota_size_limit,
+            global_quota_objects_limit,
+        ))
+        .build()
+        .execute_with(|| {
+            let sender = 1u64;
+
+            let owner = StorageObjectOwner::Member(1u64);
+
+            let content_parameters = ContentParameters {
+                content_id: 1,
+                type_id: 1234,
+                size: global_quota_size_limit + 1,
+                ipfs_content_id: vec![1, 2, 3, 4],
+            };
+
+            // Make an attempt to register a content, when uploading is blocked.
+            let res = TestDataDirectory::add_content(
+                Origin::signed(sender),
+                owner,
+                vec![content_parameters],
+            );
+            assert_eq!(res, Err(Error::<Test>::GlobalQuotaSizeLimitExceeded.into()));
+        });
+}
+
+#[test]
+fn add_content_global_objects_limit_reached() {
+    let global_quota_size_limit = 50000;
+    let global_quota_objects_limit = 0;
+
+    ExtBuilder::default()
+        .global_quota(Quota::new(
+            global_quota_size_limit,
+            global_quota_objects_limit,
+        ))
+        .build()
+        .execute_with(|| {
+            let sender = 1u64;
+
+            let owner = StorageObjectOwner::Member(1u64);
+
+            let content_parameters = ContentParameters {
+                content_id: 1,
+                type_id: 1234,
+                size: 0,
+                ipfs_content_id: vec![1, 2, 3, 4],
+            };
+
+            // Make an attempt to register a content, when uploading is blocked.
+            let res = TestDataDirectory::add_content(
+                Origin::signed(sender),
+                owner,
+                vec![content_parameters],
+            );
+            assert_eq!(
+                res,
+                Err(Error::<Test>::GlobalQuotaObjectsLimitExceeded.into())
+            );
+        });
+}
+
 #[test]
 fn accept_and_reject_content_fail_with_invalid_storage_provider() {
     with_default_mock_builder(|| {

+ 6 - 1
runtime-modules/storage/src/tests/mock.rs

@@ -11,7 +11,7 @@ use sp_runtime::{
 };
 
 use crate::data_directory::ContentIdExists;
-use crate::data_directory::Quota;
+pub use crate::data_directory::Quota;
 pub use crate::data_directory::{ContentParameters, StorageObjectOwner};
 use crate::data_object_type_registry::IsActiveDataObjectType;
 use crate::ContentId;
@@ -288,6 +288,11 @@ impl ExtBuilder {
         self
     }
 
+    pub fn global_quota(mut self, global_quota: Quota) -> Self {
+        self.global_quota = global_quota;
+        self
+    }
+
     pub fn build(self) -> sp_io::TestExternalities {
         let mut t = system::GenesisConfig::default()
             .build_storage::<Test>()