Esempio n. 1
0
 def test_patch_dict(self):
     path = 'foo.bar.baz'
     fake_dict = mock.MagicMock(spec=dict)
     # Make the dict return itself to test whether all the parts are called.
     fake_dict.get.return_value = fake_dict
     helpers.patch_dict(fake_dict, path, None)
     fake_dict.get.assert_has_calls([mock.call('foo'), mock.call('bar')])
     fake_dict.pop.assert_not_called()
Esempio n. 2
0
        def template(engine,
                     value,
                     type_,
                     exclude_properties=None,
                     default_type=None,
                     version_spec=None):
            object_store = helpers.get_object_store()
            passkey = None
            if not default_type:
                default_type = type_
            murano_class = type_.type
            if value is None:
                return None
            if isinstance(value, dsl_types.MuranoObject):
                obj = value
            elif isinstance(value, dsl_types.MuranoObjectInterface):
                obj = value.object
            elif isinstance(value, utils.MappingType):
                passkey = utils.create_marker('<Contract Passkey>')
                if exclude_properties:
                    parsed = helpers.parse_object_definition(
                        value, calling_type, context)
                    props = dsl.to_mutable(parsed['properties'], engine)
                    for p in exclude_properties:
                        helpers.patch_dict(props, p, passkey)
                    parsed['properties'] = props
                    value = helpers.assemble_object_definition(parsed)
                with helpers.thread_local_attribute(
                        constants.TL_CONTRACT_PASSKEY, passkey):
                    with helpers.thread_local_attribute(
                            constants.TL_OBJECTS_DRY_RUN, True):
                        obj = object_store.load(value,
                                                owner,
                                                context=context,
                                                default_type=default_type,
                                                scope_type=calling_type)
            else:
                raise exceptions.ContractViolationException(
                    'Value {0} cannot be represented as class {1}'.format(
                        format_scalar(value), type_))
            if not helpers.is_instance_of(
                    obj, murano_class.name, version_spec
                    or helpers.get_type(root_context)):
                raise exceptions.ContractViolationException(
                    'Object of type {0} is not compatible with '
                    'requested type {1}'.format(obj.type.name, type_))

            with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                                passkey):
                result = serializer.serialize(obj.real_this,
                                              object_store.executor,
                                              dsl_types.DumpTypes.Mixed)
                if exclude_properties:
                    for p in exclude_properties:
                        helpers.patch_dict(result, p, utils.NO_VALUE)
                return result
Esempio n. 3
0
 def finalize(self):
     if self.value is None:
         return None
     object_store = helpers.get_object_store()
     passkey = getattr(self.value, '__passkey__', None)
     with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                         passkey):
         result = serializer.serialize(self.value.real_this,
                                       object_store.executor,
                                       dsl_types.DumpTypes.Mixed)
         if self.exclude_properties:
             for p in self.exclude_properties:
                 helpers.patch_dict(result, p, utils.NO_VALUE)
         return result
Esempio n. 4
0
 def transform(self):
     object_store = helpers.get_object_store()
     if self.value is None:
         return None
     if isinstance(self.value, dsl_types.MuranoObject):
         obj = self.value
     elif isinstance(self.value, dsl_types.MuranoObjectInterface):
         obj = self.value.object
     elif isinstance(self.value, utils.MappingType):
         passkey = utils.create_marker('<Contract Passkey>')
         if self.exclude_properties:
             parsed = helpers.parse_object_definition(
                 self.value, self.calling_type, self.context)
             props = dsl.to_mutable(parsed['properties'], self.engine)
             for p in self.exclude_properties:
                 helpers.patch_dict(props, p, passkey)
             parsed['properties'] = props
             value = helpers.assemble_object_definition(parsed)
         else:
             value = self.value
         with helpers.thread_local_attribute(constants.TL_CONTRACT_PASSKEY,
                                             passkey):
             with helpers.thread_local_attribute(
                     constants.TL_OBJECTS_DRY_RUN, True):
                 obj = object_store.load(value,
                                         self.owner,
                                         context=self.context,
                                         default_type=self.default_type,
                                         scope_type=self.calling_type)
                 obj.__passkey__ = passkey
     else:
         raise exceptions.ContractViolationException(
             'Value {0} cannot be represented as class {1}'.format(
                 helpers.format_scalar(self.value), self.type))
     self.value = obj
     return self.validate()
Esempio n. 5
0
 def test_patch_dict_without_dict(self):
     path = 'foo.bar.baz'
     not_a_dict = mock.Mock()
     helpers.patch_dict(not_a_dict, path, None)
     not_a_dict.get.assert_not_called()
     not_a_dict.pop.assert_not_called()