def test_fails_object_down_cast(self): root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) cls = murano_class.MuranoClass(None, self.resolver, 'SomeClass', None, [root]) root_obj = root.new(None, None, None) self.assertRaises(TypeError, root_obj.cast, cls)
def test_parent_class_property_access(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) child_cls = murano_class.MuranoClass(None, self.resolver, 'Child', [cls]) cls.add_property('anotherProperty', typespec.PropertySpec({'Default': 0}, self.resolver)) obj = child_cls.new(None, None, None, {}) self.assertEqual(0, obj.anotherProperty)
def test_non_root_class_resolves_parents(self): root_cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) class_loader = mock.Mock(get_class=lambda name: root_cls) desc_cl1 = murano_class.MuranoClass(class_loader, self.resolver, 'Obj', None) desc_cl2 = murano_class.MuranoClass(class_loader, self.resolver, 'Obj', None, [root_cls]) self.assertEqual([root_cls], desc_cl1.parents) self.assertEqual([root_cls], desc_cl2.parents)
def test_object_parent_properties_initialization(self): root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) cls = murano_class.MuranoClass(None, self.resolver, 'SomeClass', None, [root]) root.new = mock.Mock() init_kwargs = {'theArg': 0} obj = murano_object.MuranoObject(cls, None, None, None) expected_calls = [mock.call().initialize(**init_kwargs)] obj.initialize(**init_kwargs) # each object should also initialize his parent objects self.assertEqual(expected_calls, root.new.mock_calls[1:])
def test_class_is_compatible(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) descendant_cls = murano_class.MuranoClass(None, self.resolver, 'DescendantCls', None, [cls]) obj = mock.Mock(spec=murano_object.MuranoObject) descendant_obj = mock.Mock(spec=murano_object.MuranoObject) obj.type = cls descendant_obj.type = descendant_cls descendant_obj.parents = [obj] self.assertTrue(cls.is_compatible(obj)) self.assertTrue(cls.is_compatible(descendant_obj)) self.assertFalse(descendant_cls.is_compatible(obj))
def test_set_parent_property(self): root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) cls = murano_class.MuranoClass(None, self.resolver, 'SomeClass', [root]) root.add_property('rootProperty', typespec.PropertySpec({'Default': 0}, self.resolver)) obj = cls.new(None, None, None, {}) with mock.patch('muranoapi.engine.helpers') as helpers_mock: with mock.patch('yaql.context.Context'): helpers_mock.evaluate = lambda val, ctx, _: val obj.set_property('rootProperty', 20) self.assertEqual(20, obj.rootProperty)
def test_new_method_not_calls_initialize(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) cls.object_class = mock.Mock() obj = cls.new(None, None, None) self.assertFalse(obj.initialize.called)
def test_proper_property_access(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) cls.add_property('someProperty', typespec.PropertySpec({'Default': 0}, self.resolver)) obj = cls.new(None, None, None, {}) self.assertEqual(0, obj.someProperty)
def test_object_up_cast(self): root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) root_alt = murano_class.MuranoClass(None, self.resolver, 'RootAlt', []) cls = murano_class.MuranoClass(None, self.resolver, 'SomeClass', [root, root_alt]) root_obj = root.new(None, None, None) cls_obj = cls.new(None, None, None) root_obj_casted2root = root_obj.cast(root) cls_obj_casted2root = cls_obj.cast(root) cls_obj_casted2root_alt = cls_obj.cast(root_alt) self.assertEqual(root_obj, root_obj_casted2root) # each object creates an _internal_ parent objects hierarchy, # so direct comparison of objects is not possible self.assertEqual(root, cls_obj_casted2root.type) self.assertEqual(root_alt, cls_obj_casted2root_alt.type)
def test_fails_internal_property_access(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) cls.add_property('__hidden', typespec.PropertySpec({'Default': 10}, self.resolver)) obj = murano_object.MuranoObject(cls, None, None, None) self.assertRaises(AttributeError, lambda: obj.__hidden)
def test_fails_on_parents_property_collision(self): root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) mother = murano_class.MuranoClass(None, self.resolver, 'Mother', [root]) father = murano_class.MuranoClass(None, self.resolver, 'Father', [root]) child = murano_class.MuranoClass(None, self.resolver, 'Child', [mother, father]) mother.add_property( 'conflictProp', typespec.PropertySpec({'Default': 0}, self.resolver)) father.add_property( 'conflictProp', typespec.PropertySpec({'Default': 0}, self.resolver)) obj = child.new(None, None, None, {}) self.assertRaises(LookupError, lambda: obj.conflictProp)
def test_new_method_calls_initialize(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) cls.object_class = mock.Mock() with mock.patch('inspect.getargspec') as spec_mock: spec_mock.return_value = Bunch(args=()) obj = cls.new(None, None, None, {}) self.assertTrue(obj.initialize.called)
def test_set_undeclared_property_as_internal(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) obj = cls.new(None, None, None, {}) obj.cast = mock.Mock(return_value=obj) prop_value = 10 obj.set_property('internalProp', prop_value, caller_class=cls) resolved_value = obj.get_property('internalProp', caller_class=cls) self.assertEqual(prop_value, resolved_value)
def test_add_property_to_class(self): prop = typespec.PropertySpec({'Default': 1}, self.resolver) cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) cls.add_property('firstPrime', prop) class_properties = cls.properties class_property = cls.get_property('firstPrime') self.assertEqual(['firstPrime'], class_properties) self.assertEqual(prop, class_property)
def test_fails_forbidden_set_property(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) cls.add_property('someProperty', typespec.PropertySpec({'Default': 0}, self.resolver)) cls.is_compatible = mock.Mock(return_value=False) obj = cls.new(None, None, None, {}) self.assertRaises(exceptions.NoWriteAccess, obj.set_property, 'someProperty', 10, caller_class=cls)
def test_class_property_search(self): void_prop = typespec.PropertySpec({'Default': 'Void'}, self.resolver) mother_prop = typespec.PropertySpec({'Default': 'Mother'}, self.resolver) father_prop = typespec.PropertySpec({'Default': 'Father'}, self.resolver) child_prop = typespec.PropertySpec({'Default': 'Child'}, self.resolver) root = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS) mother = murano_class.MuranoClass(None, self.resolver, 'Mother', [root]) father = murano_class.MuranoClass(None, self.resolver, 'Father', [root]) child = murano_class.MuranoClass(None, self.resolver, 'Child', [mother, father]) root.add_property('Void', void_prop) mother.add_property('Mother', mother_prop) father.add_property('Father', father_prop) child.add_property('Child', child_prop) self.assertEqual(child_prop, child.find_property('Child')) self.assertEqual(father_prop, child.find_property('Father')) self.assertEqual(mother_prop, child.find_property('Mother')) self.assertEqual(void_prop, child.find_property('Void'))
def get_class(self, name, create_missing=False): if name in self._loaded_types: return self._loaded_types[name] try: data = self.load_definition(name) package = self._get_package(name) except (exceptions.NoPackageForClassFound, exceptions.NoClassFound): if create_missing: data = {'Name': name} package = None else: raise namespaces = data.get('Namespaces', {}) ns_resolver = namespace_resolver.NamespaceResolver(namespaces) class_parents = data.get('Extends') if class_parents: if not isinstance(class_parents, types.ListType): class_parents = [class_parents] for i, parent_name in enumerate(class_parents): full_name = ns_resolver.resolve_name(parent_name) class_parents[i] = self.get_class(full_name) type_obj = murano_class.MuranoClass(self, ns_resolver, name, package, class_parents) properties = data.get('Properties', {}) for property_name, property_spec in properties.iteritems(): spec = typespec.PropertySpec(property_spec, ns_resolver) type_obj.add_property(property_name, spec) for method_name, payload in data.get('Workflow', {}).iteritems(): type_obj.add_method(method_name, payload) self._loaded_types[name] = type_obj return type_obj
def test_class_namespace_resolver(self): resolver = ns_resolver.NamespaceResolver({}) cls = murano_class.MuranoClass(None, resolver, ROOT_CLASS, None) self.assertEqual(resolver, cls.namespace_resolver)
def test_fails_setting_undeclared_property(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) obj = cls.new(None, None, None, {}) self.assertRaises(AttributeError, obj.set_property, 'newOne', 10)
def test_root_class_has_no_parents(self): root_class = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, ['You should not see me!']) self.assertEqual([], root_class.parents)
def test_class_initial_properties(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) self.assertEqual([], cls.properties)
def test_class_name(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) self.assertEqual(ROOT_CLASS, cls.name)
def test_fails_add_incompatible_property_to_class(self): cls = murano_class.MuranoClass(None, self.resolver, ROOT_CLASS, None) kwargs = {'name': 'sampleProperty', 'property_typespec': {}} self.assertRaises(TypeError, cls.add_property, **kwargs)