Browse Source

transaction extrinsic success case test coverage added, issue fixed

iorveth 4 years ago
parent
commit
fee64998ca

+ 14 - 14
runtime-modules/content-directory/src/lib.rs

@@ -1695,13 +1695,13 @@ decl_module! {
             // == MUTATION SAFE ==
             //
 
-            // This Vec holds the T::EntityId of the entity created as a result of executing a `CreateEntity` `Operation`
-            let mut entity_created_in_operation = vec![];
+            // This BTreeMap holds the T::EntityId of the entity created as a result of executing a `CreateEntity` `Operation`
+            let mut entity_created_in_operation = BTreeMap::new();
 
             // Create raw origin
             let raw_origin = origin.into().map_err(|_| ERROR_ORIGIN_CANNOT_BE_MADE_INTO_RAW_ORIGIN)?;
 
-            for operation_type in operations.into_iter() {
+            for (index, operation_type) in operations.into_iter().enumerate() {
                 let origin = T::Origin::from(raw_origin.clone());
                 let actor = actor.clone();
                 match operation_type {
@@ -1710,16 +1710,7 @@ decl_module! {
 
                         // entity id of newly created entity
                         let entity_id = Self::next_entity_id() - T::EntityId::one();
-                        entity_created_in_operation.push(entity_id);
-                    },
-                    OperationType::UpdatePropertyValues(update_property_values_operation) => {
-                        let entity_id = operations::parametrized_entity_to_entity_id(
-                            &entity_created_in_operation, update_property_values_operation.entity_id
-                        )?;
-                        let property_values = operations::parametrized_property_values_to_property_values(
-                            &entity_created_in_operation, update_property_values_operation.new_parametrized_property_values
-                        )?;
-                        Self::update_entity_property_values(origin, actor, entity_id, property_values)?;
+                        entity_created_in_operation.insert(index, entity_id);
                     },
                     OperationType::AddSchemaSupportToEntity(add_schema_support_to_entity_operation) => {
                         let entity_id = operations::parametrized_entity_to_entity_id(
@@ -1730,7 +1721,16 @@ decl_module! {
                             &entity_created_in_operation, add_schema_support_to_entity_operation.parametrized_property_values
                         )?;
                         Self::add_schema_support_to_entity(origin, actor, entity_id, schema_id, property_values)?;
-                    }
+                    },
+                    OperationType::UpdatePropertyValues(update_property_values_operation) => {
+                        let entity_id = operations::parametrized_entity_to_entity_id(
+                            &entity_created_in_operation, update_property_values_operation.entity_id
+                        )?;
+                        let property_values = operations::parametrized_property_values_to_property_values(
+                            &entity_created_in_operation, update_property_values_operation.new_parametrized_property_values
+                        )?;
+                        Self::update_entity_property_values(origin, actor, entity_id, property_values)?;
+                    },
                 }
             }
 

+ 8 - 0
runtime-modules/content-directory/src/mock.rs

@@ -819,6 +819,14 @@ pub fn transfer_entity_ownership(
     )
 }
 
+pub fn transaction(
+    origin: u64,
+    actor: Actor<Runtime>,
+    operations: Vec<OperationType<Runtime>>,
+) -> Result<(), &'static str> {
+    TestModule::transaction(Origin::signed(origin), actor, operations)
+}
+
 impl From<InboundReferenceCounter> for EntityReferenceCounterSideEffect {
     fn from(inbound_rc: InboundReferenceCounter) -> Self {
         Self {

+ 5 - 5
runtime-modules/content-directory/src/operations.rs

@@ -59,7 +59,7 @@ pub enum OperationType<T: Trait> {
 }
 
 pub fn parametrized_entity_to_entity_id<T: Trait>(
-    created_entities: &[T::EntityId],
+    created_entities: &BTreeMap<usize, T::EntityId>,
     entity: ParameterizedEntity<T>,
 ) -> Result<T::EntityId, &'static str> {
     match entity {
@@ -67,14 +67,14 @@ pub fn parametrized_entity_to_entity_id<T: Trait>(
         ParameterizedEntity::InternalEntityJustAdded(op_index_u32) => {
             let op_index = op_index_u32 as usize;
             Ok(*created_entities
-                .get(op_index)
+                .get(&op_index)
                 .ok_or("EntityNotCreatedByOperation")?)
         }
     }
 }
 
 pub fn parametrized_property_values_to_property_values<T: Trait>(
-    created_entities: &[T::EntityId],
+    created_entities: &BTreeMap<usize, T::EntityId>,
     parametrized_property_values: Vec<ParametrizedClassPropertyValue<T>>,
 ) -> Result<BTreeMap<PropertyId, PropertyValue<T>>, &'static str> {
     let mut class_property_values = BTreeMap::new();
@@ -88,7 +88,7 @@ pub fn parametrized_property_values_to_property_values<T: Trait>(
                 // Verify that referenced entity was indeed created created
                 let op_index = entity_created_in_operation_index as usize;
                 let entity_id = created_entities
-                    .get(op_index)
+                    .get(&op_index)
                     .ok_or("EntityNotCreatedByOperation")?;
                 PropertyValue::Single(SinglePropertyValue::new(Value::Reference(*entity_id)))
             }
@@ -103,7 +103,7 @@ pub fn parametrized_property_values_to_property_values<T: Trait>(
                         ) => {
                             let op_index = entity_created_in_operation_index as usize;
                             let entity_id = created_entities
-                                .get(op_index)
+                                .get(&op_index)
                                 .ok_or("EntityNotCreatedByOperation")?;
                             entities.push(*entity_id);
                         }

+ 1 - 1
runtime-modules/content-directory/src/schema.rs

@@ -103,7 +103,7 @@ impl<T: Trait> VecPropertyType<T> {
 /// `Type` enum wrapper
 #[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
 #[derive(Encode, Decode, Clone, Copy, PartialEq, Eq, Debug)]
-pub struct SingleValuePropertyType<T: Trait>(Type<T>);
+pub struct SingleValuePropertyType<T: Trait>(pub Type<T>);
 
 impl<T: Trait> Default for SingleValuePropertyType<T> {
     fn default() -> Self {

+ 72 - 0
runtime-modules/content-directory/src/tests.rs

@@ -1219,3 +1219,75 @@ fn transfer_entity_ownership_success() {
         );
     })
 }
+
+#[test]
+fn transaction_success() {
+    with_test_externalities(|| {
+        // Create class with default permissions
+        assert_ok!(create_simple_class_with_default_permissions(LEAD_ORIGIN));
+
+        // Create single reference property
+        let property_type_reference =
+            SingleValuePropertyType(Type::Reference(FIRST_CLASS_ID, true));
+        let property = Property::<Runtime>::with_name_and_type(
+            PropertyNameLengthConstraint::get().max() as usize,
+            PropertyType::Single(property_type_reference),
+        );
+
+        // Add Schema to the Class
+        assert_ok!(add_class_schema(
+            LEAD_ORIGIN,
+            FIRST_CLASS_ID,
+            BTreeSet::new(),
+            vec![property]
+        ));
+
+        let operations = vec![
+            OperationType::CreateEntity(CreateEntityOperation {
+                class_id: FIRST_CLASS_ID,
+            }),
+            OperationType::AddSchemaSupportToEntity(AddSchemaSupportToEntityOperation {
+                entity_id: ParameterizedEntity::InternalEntityJustAdded(0), // index 0 (prior operation)
+                schema_id: 0,
+                parametrized_property_values: vec![ParametrizedClassPropertyValue {
+                    in_class_index: 0,
+                    value: ParametrizedPropertyValue::InternalEntityJustAdded(0),
+                }],
+            }),
+            OperationType::CreateEntity(CreateEntityOperation {
+                class_id: FIRST_CLASS_ID,
+            }),
+            OperationType::UpdatePropertyValues(UpdatePropertyValuesOperation {
+                entity_id: ParameterizedEntity::InternalEntityJustAdded(0), // index 0 (prior operation)
+                new_parametrized_property_values: vec![ParametrizedClassPropertyValue {
+                    in_class_index: 0,
+                    value: ParametrizedPropertyValue::InternalEntityJustAdded(2),
+                }],
+            }),
+        ];
+
+        // Runtime state before tested call
+
+        // Events number before tested call
+        let number_of_events_before_calls = System::events().len();
+
+        let actor = Actor::Lead;
+
+        // Number of operations to be performed
+        let operations_count = operations.len();
+
+        // Complete transaction
+        assert_ok!(transaction(LEAD_ORIGIN, actor.clone(), operations));
+
+        // Runtime tested state after call
+
+        let entity_ownership_transfered_event =
+            get_test_event(RawEvent::TransactionCompleted(actor));
+
+        // Last event checked
+        assert_event_success(
+            entity_ownership_transfered_event,
+            number_of_events_before_calls + operations_count + 1,
+        );
+    })
+}