Add Resource.name property

We've been able to set name_attribute for a while now, but it's not
actually hooked up to anything. We've also been attempting to access it
through Resource.find and getting lucky that resources typically do have
a name property set. This change adds it so we can reliably access it
and provide the fallback that we've sort of been providing all along.
You can also set the name here, which sets the underlying attribute
pointed to by name_attribute.

Change-Id: I081816adebbebf26339b5b57e3f79fc7b10649f7
This commit is contained in:
Brian Curtin
2015-02-14 19:22:01 -06:00
parent 3361e7e86c
commit 7490f1dc35
2 changed files with 34 additions and 0 deletions

View File

@@ -331,6 +331,23 @@ class Resource(collections.MutableMapping):
def id(self):
del self._attrs[self.id_attribute]
@property
def name(self):
"""The name associated with this resource.
The true value of the ``name`` property comes from the
attribute set as :data:`name_attribute`.
"""
return self._attrs.get(self.name_attribute, None)
@name.setter
def name(self, value):
self._attrs[self.name_attribute] = value
@name.deleter
def name(self):
del self._attrs[self.name_attribute]
@property
def is_dirty(self):
"""True if the resource needs to be updated to the remote."""

View File

@@ -547,6 +547,23 @@ class TestFind(base.TestCase):
faker.id_attribute = 'id'
self.assertEqual(fake_id, faker.id)
def test_name_attribute(self):
class Person_ES(resource.Resource):
name_attribute = "nombre"
nombre = resource.prop('nombre')
name = "Brian"
args = {'nombre': name}
person = Person_ES(args)
self.assertEqual(person.nombre, name)
self.assertEqual(person.name, name)
new_name = "Julien"
person.name = new_name
self.assertEqual(person.nombre, new_name)
self.assertEqual(person.name, new_name)
def test_boolstr_prop(self):
faker = FakeResource(fake_data)
self.assertEqual(True, faker.enabled)