Skip to content

Commit

Permalink
Upgrading Entity.property to properties map in datastore.
Browse files Browse the repository at this point in the history
Towards #1288.
  • Loading branch information
dhermes committed Mar 28, 2016
1 parent e7a6e20 commit 08789e3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
7 changes: 2 additions & 5 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ def _new_value_pb(entity_pb, name):
:rtype: :class:`gcloud.datastore._generated.entity_pb2.Value`
:returns: The new ``Value`` protobuf that was added to the entity.
"""
property_pb = entity_pb.property.add()
property_pb.name = name
return property_pb.value
return entity_pb.properties.get_or_create(name)


def _property_tuples(entity_pb):
Expand All @@ -139,8 +137,7 @@ def _property_tuples(entity_pb):
:returns: An iterator that yields tuples of a name and ``Value``
corresponding to properties on the entity.
"""
for property_pb in entity_pb.property:
yield property_pb.name, property_pb.value
return six.iteritems(entity_pb.properties)


def entity_from_protobuf(pb):
Expand Down
8 changes: 4 additions & 4 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@ def test_it(self):
result = self._callFUT(entity_pb, name)

self.assertTrue(isinstance(result, entity_pb2.Value))
self.assertEqual(len(entity_pb.property), 1)
self.assertEqual(entity_pb.property[0].name, name)
self.assertEqual(entity_pb.property[0].value, result)
self.assertEqual(len(entity_pb.properties), 1)
self.assertEqual(entity_pb.properties[name], result)


class Test__property_tuples(unittest2.TestCase):
Expand All @@ -53,7 +52,8 @@ def test_it(self):

result = self._callFUT(entity_pb)
self.assertTrue(isinstance(result, types.GeneratorType))
self.assertEqual(list(result), [(name1, val_pb1), (name2, val_pb2)])
self.assertEqual(sorted(result),
sorted([(name1, val_pb1), (name2, val_pb2)]))


class Test_entity_from_protobuf(unittest2.TestCase):
Expand Down

0 comments on commit 08789e3

Please sign in to comment.