Skip to content

Commit

Permalink
Rename Value.indexed->exclude_from_indexes.
Browse files Browse the repository at this point in the history
Towards #1288.
  • Loading branch information
dhermes committed Mar 28, 2016
1 parent 9957fb5 commit a2493e2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 28 deletions.
17 changes: 9 additions & 8 deletions gcloud/datastore/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,19 +173,20 @@ def entity_from_protobuf(pb):
if meaning is not None:
entity_meanings[prop_name] = (meaning, value)

# Check if ``value_pb`` was indexed. Lists need to be special-cased
# and we require all ``indexed`` values in a list agree.
# Check if ``value_pb`` was excluded from index. Lists need to be
# special-cased and we require all ``exclude_from_indexes`` values
# in a list agree.
if is_list:
indexed_values = set(value_pb.indexed
exclude_values = set(value_pb.exclude_from_indexes
for value_pb in value_pb.list_value)
if len(indexed_values) != 1:
if len(exclude_values) != 1:
raise ValueError('For a list_value, subvalues must either all '
'be indexed or all excluded from indexes.')

if not indexed_values.pop():
if exclude_values.pop():
exclude_from_indexes.append(prop_name)
else:
if not value_pb.indexed:
if value_pb.exclude_from_indexes:
exclude_from_indexes.append(prop_name)

entity = Entity(key=key, exclude_from_indexes=exclude_from_indexes)
Expand Down Expand Up @@ -220,10 +221,10 @@ def entity_to_protobuf(entity):
# Add index information to protobuf.
if name in entity.exclude_from_indexes:
if not value_is_list:
value_pb.indexed = False
value_pb.exclude_from_indexes = True

for sub_value in value_pb.list_value:
sub_value.indexed = False
sub_value.exclude_from_indexes = True

# Add meaning information to protobuf.
if name in entity._meanings:
Expand Down
24 changes: 12 additions & 12 deletions gcloud/datastore/test_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,12 @@ def test_put_entity_w_completed_key(self):

prop_dict = dict(_property_tuples(mutated_entity))
self.assertEqual(len(prop_dict), 3)
self.assertTrue(prop_dict['foo'].indexed)
self.assertFalse(prop_dict['baz'].indexed)
self.assertTrue(prop_dict['spam'].indexed)
self.assertFalse(prop_dict['spam'].list_value[0].indexed)
self.assertFalse(prop_dict['spam'].list_value[1].indexed)
self.assertFalse(prop_dict['spam'].list_value[2].indexed)
self.assertFalse(prop_dict['foo'].exclude_from_indexes)
self.assertTrue(prop_dict['baz'].exclude_from_indexes)
self.assertFalse(prop_dict['spam'].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[0].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[1].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[2].exclude_from_indexes)
self.assertFalse('frotz' in prop_dict)

def test_put_entity_w_completed_key_prefixed_project(self):
Expand All @@ -150,12 +150,12 @@ def test_put_entity_w_completed_key_prefixed_project(self):

prop_dict = dict(_property_tuples(mutated_entity))
self.assertEqual(len(prop_dict), 3)
self.assertTrue(prop_dict['foo'].indexed)
self.assertFalse(prop_dict['baz'].indexed)
self.assertTrue(prop_dict['spam'].indexed)
self.assertFalse(prop_dict['spam'].list_value[0].indexed)
self.assertFalse(prop_dict['spam'].list_value[1].indexed)
self.assertFalse(prop_dict['spam'].list_value[2].indexed)
self.assertFalse(prop_dict['foo'].exclude_from_indexes)
self.assertTrue(prop_dict['baz'].exclude_from_indexes)
self.assertFalse(prop_dict['spam'].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[0].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[1].exclude_from_indexes)
self.assertTrue(prop_dict['spam'].list_value[2].exclude_from_indexes)
self.assertFalse('frotz' in prop_dict)

def test_delete_w_partial_key(self):
Expand Down
14 changes: 6 additions & 8 deletions gcloud/datastore/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,20 @@ def test_it(self):

unindexed_val_pb = _new_value_pb(entity_pb, 'bar')
unindexed_val_pb.integer_value = 10
unindexed_val_pb.indexed = False
unindexed_val_pb.exclude_from_indexes = True

list_val_pb1 = _new_value_pb(entity_pb, 'baz')
list_pb1 = list_val_pb1.list_value

unindexed_list_val_pb = list_pb1.add()
unindexed_list_val_pb.integer_value = 11
unindexed_list_val_pb.indexed = False
unindexed_list_val_pb.exclude_from_indexes = True

list_val_pb2 = _new_value_pb(entity_pb, 'qux')
list_pb2 = list_val_pb2.list_value

indexed_list_val_pb = list_pb2.add()
indexed_list_val_pb.integer_value = 12
indexed_list_val_pb.indexed = True

entity = self._callFUT(entity_pb)
self.assertEqual(entity.kind, _KIND)
Expand Down Expand Up @@ -125,11 +124,10 @@ def test_mismatched_value_indexed(self):

unindexed_value_pb1 = list_pb.add()
unindexed_value_pb1.integer_value = 10
unindexed_value_pb1.indexed = False
unindexed_value_pb1.exclude_from_indexes = True

unindexed_value_pb2 = list_pb.add()
unindexed_value_pb2.integer_value = 11
unindexed_value_pb2.indexed = True

with self.assertRaises(ValueError):
self._callFUT(entity_pb)
Expand Down Expand Up @@ -290,7 +288,7 @@ def test_inverts_to_protobuf(self):
# Add an integer property.
val_pb1 = _new_value_pb(original_pb, 'foo')
val_pb1.integer_value = 1337
val_pb1.indexed = False
val_pb1.exclude_from_indexes = True
# Add a string property.
val_pb2 = _new_value_pb(original_pb, 'bar')
val_pb2.string_value = u'hello'
Expand All @@ -308,11 +306,11 @@ def test_inverts_to_protobuf(self):
# Add a list property.
val_pb4 = _new_value_pb(original_pb, 'list-quux')
list_val1 = val_pb4.list_value.add()
list_val1.indexed = False
list_val1.exclude_from_indexes = True
list_val1.meaning = meaning = 22
list_val1.blob_value = b'\xe2\x98\x83'
list_val2 = val_pb4.list_value.add()
list_val2.indexed = False
list_val2.exclude_from_indexes = True
list_val2.meaning = meaning
list_val2.blob_value = b'\xe2\x98\x85'

Expand Down

0 comments on commit a2493e2

Please sign in to comment.