def _set_from_db_object(self, context, db_object, fields=None): fields = set(fields or self.fields) - {'traits'} super(Node, self)._set_from_db_object(context, db_object, fields) self.traits = object_base.obj_make_list( context, objects.TraitList(context), objects.Trait, db_object['traits']) self.traits.obj_reset_changes()
def get_test_node(ctxt, **kw): """Return a Node object with appropriate attributes. NOTE: The object leaves the attributes marked as changed, such that a create() could be used to commit it to the DB. """ kw['object_type'] = 'node' get_db_node_checked = check_keyword_arguments(db_utils.get_test_node) db_node = get_db_node_checked(**kw) # Let DB generate ID if it isn't specified explicitly if 'id' not in kw: del db_node['id'] node = objects.Node(ctxt) for key in db_node: if key == 'traits': # convert list of strings to object raw_traits = db_node['traits'] trait_list = [] for raw_trait in raw_traits: trait = objects.Trait(ctxt, trait=raw_trait) trait_list.append(trait) node.traits = objects.TraitList(ctxt, objects=trait_list) node.traits.obj_reset_changes() else: setattr(node, key, db_node[key]) return node
def test_get_trait_names(self): trait = objects.Trait(context=self.context, node_id=self.fake_trait['node_id'], trait=self.fake_trait['trait']) trait_list = objects.TraitList(context=self.context, objects=[trait]) result = trait_list.get_trait_names() self.assertEqual([self.fake_trait['trait']], result)
def test_trait_unsupported_set_no_remove_non_default(self): # traits set, should be set to default. node = obj_utils.get_test_node(self.ctxt, **self.fake_node) node.traits = objects.TraitList(self.ctxt) node.traits.obj_reset_changes() node.obj_reset_changes() node._convert_to_version("1.22", False) self.assertIsNone(node.traits) self.assertEqual({'traits': None}, node.obj_get_changes())
def test_traits_supported_set(self): # traits set, no change required. node = obj_utils.get_test_node(self.ctxt, **self.fake_node) traits = objects.TraitList(objects=[objects.Trait('CUSTOM_TRAIT')]) traits.obj_reset_changes() node.traits = traits node.obj_reset_changes() node._convert_to_version("1.23") self.assertEqual(traits, node.traits) self.assertEqual({}, node.obj_get_changes())
def test_save_with_traits(self): uuid = self.fake_node['uuid'] with mock.patch.object(self.dbapi, 'get_node_by_uuid', autospec=True) as mock_get_node: mock_get_node.return_value = self.fake_node with mock.patch.object(self.dbapi, 'update_node', autospec=True) as mock_update_node: n = objects.Node.get(self.context, uuid) trait = objects.Trait(self.context, node_id=n.id, trait='CUSTOM_1') n.traits = objects.TraitList(self.context, objects=[trait]) self.assertRaises(exception.BadRequest, n.save) self.assertFalse(mock_update_node.called)
def test_as_dict(self): trait = objects.Trait(context=self.context, node_id=self.fake_trait['node_id'], trait=self.fake_trait['trait']) trait_list = objects.TraitList(context=self.context, objects=[trait]) result = trait_list.as_dict() expected = { 'objects': [{ 'node_id': self.fake_trait['node_id'], 'trait': self.fake_trait['trait'] }] } self.assertEqual(expected, result)
def test_create_with_traits(self): node = obj_utils.get_test_node(self.ctxt, **self.fake_node) trait = objects.Trait(self.context, node_id=node.id, trait='CUSTOM_1') node.traits = objects.TraitList(self.context, objects=[trait]) self.assertRaises(exception.BadRequest, node.create)