Browse Source

Add tests to DOSR

Jens Finkhaeuser 6 years ago
parent
commit
60a3a0be71
2 changed files with 80 additions and 0 deletions
  1. 79 0
      src/storage/data_object_storage_registry.rs
  2. 1 0
      src/storage/mock.rs

+ 79 - 0
src/storage/data_object_storage_registry.rs

@@ -163,3 +163,82 @@ impl<T: Trait> Module<T> {
         Ok(())
     }
 }
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::storage::mock::*;
+
+    use system::{self, EventRecord, Phase};
+
+    #[test]
+    fn initial_state() {
+        with_default_mock_builder(|| {
+            assert_eq!(
+                TestDataObjectStorageRegistry::first_relationship_id(),
+                TEST_FIRST_RELATIONSHIP_ID
+            );
+        });
+    }
+
+    #[test]
+    fn test_add_relationship() {
+        with_default_mock_builder(|| {
+            // The content needs to exist - in our mock, that's with the content ID 42
+            let res = TestDataObjectStorageRegistry::add_relationship(Origin::signed(1), 42);
+            assert!(res.is_ok());
+        });
+    }
+
+    #[test]
+    fn test_fail_adding_relationship_with_bad_content() {
+        with_default_mock_builder(|| {
+            let res = TestDataObjectStorageRegistry::add_relationship(Origin::signed(1), 24);
+            assert!(res.is_err());
+        });
+    }
+
+    #[test]
+    fn test_toggle_ready() {
+        with_default_mock_builder(|| {
+            // Create a DOSR
+            let res = TestDataObjectStorageRegistry::add_relationship(Origin::signed(1), 42);
+            assert!(res.is_ok());
+
+            // Grab DOSR ID from event
+            let dosr_id = match System::events().last().unwrap().event {
+                MetaEvent::data_object_storage_registry(
+                    data_object_storage_registry::RawEvent::DataObjectStorageRelationshipAdded(
+                        dosr_id,
+                        content_id,
+                        account_id,
+                    ),
+                ) => dosr_id,
+                _ => 0xdeadbeefu64, // invalid value, unlikely to match
+            };
+            assert_ne!(dosr_id, 0xdeadbeefu64);
+
+            // Toggling from a different account should fail
+            let res =
+                TestDataObjectStorageRegistry::set_relationship_ready(Origin::signed(2), dosr_id);
+            assert!(res.is_err());
+
+            // Toggling with the wrong ID should fail.
+            let res = TestDataObjectStorageRegistry::set_relationship_ready(
+                Origin::signed(1),
+                dosr_id + 1,
+            );
+            assert!(res.is_err());
+
+            // Toggling with the correct ID and origin should succeed
+            let res =
+                TestDataObjectStorageRegistry::set_relationship_ready(Origin::signed(1), dosr_id);
+            assert!(res.is_ok());
+            assert_eq!(System::events().last().unwrap().event,
+                MetaEvent::data_object_storage_registry(data_object_storage_registry::RawEvent::DataObjectStorageRelationshipReadyUpdated(
+                    dosr_id,
+                    true,
+                )));
+        });
+    }
+}

+ 1 - 0
src/storage/mock.rs

@@ -198,6 +198,7 @@ pub type TestDataObjectTypeRegistry = data_object_type_registry::Module<Test>;
 pub type TestDataObjectType = data_object_type_registry::DataObjectType;
 pub type TestDataDirectory = data_directory::Module<Test>;
 pub type TestDataObject = data_directory::DataObject<Test>;
+pub type TestDataObjectStorageRegistry = data_object_storage_registry::Module<Test>;
 
 pub const TEST_FIRST_DATA_OBJECT_TYPE_ID: u64 = 1000;
 pub const TEST_FIRST_CONTENT_ID: u64 = 2000;