Skip to content

Commit

Permalink
Merge pull request #1454 from dhermes/rename-op-1288-2
Browse files Browse the repository at this point in the history
Handling datastore renames on CompositeFilter and PropertyFilter.
  • Loading branch information
dhermes committed Feb 12, 2016
2 parents 6a3b032 + 1d92031 commit eadd0e5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 19 deletions.
12 changes: 6 additions & 6 deletions gcloud/datastore/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -483,25 +483,25 @@ def _pb_from_query(query):
pb.kind.add().name = query.kind

composite_filter = pb.filter.composite_filter
composite_filter.operator = _query_pb2.CompositeFilter.AND
composite_filter.op = _query_pb2.CompositeFilter.AND

if query.ancestor:
ancestor_pb = helpers._prepare_key_for_request(
query.ancestor.to_protobuf())

# Filter on __key__ HAS_ANCESTOR == ancestor.
ancestor_filter = composite_filter.filter.add().property_filter
ancestor_filter = composite_filter.filters.add().property_filter
ancestor_filter.property.name = '__key__'
ancestor_filter.operator = _query_pb2.PropertyFilter.HAS_ANCESTOR
ancestor_filter.op = _query_pb2.PropertyFilter.HAS_ANCESTOR
ancestor_filter.value.key_value.CopyFrom(ancestor_pb)

for property_name, operator, value in query.filters:
pb_op_enum = query.OPERATORS.get(operator)

# Add the specific filter
property_filter = composite_filter.filter.add().property_filter
property_filter = composite_filter.filters.add().property_filter
property_filter.property.name = property_name
property_filter.operator = pb_op_enum
property_filter.op = pb_op_enum

# Set the value to filter on based on the type.
if property_name == '__key__':
Expand All @@ -511,7 +511,7 @@ def _pb_from_query(query):
else:
helpers._set_protobuf_value(property_filter.value, value)

if not composite_filter.filter:
if not composite_filter.filters:
pb.ClearField('filter')

for prop in query.order:
Expand Down
5 changes: 3 additions & 2 deletions gcloud/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,9 @@ def test_run_query_wo_eventual_w_transaction(self):
request.ParseFromString(cw['body'])
self.assertEqual(request.partition_id.namespace_id, '')
self.assertEqual(request.query, q_pb)
self.assertEqual(request.read_options.read_consistency,
datastore_pb2.ReadOptions.DEFAULT)
self.assertEqual(
request.read_options.read_consistency,
datastore_pb2.ReadOptions.READ_CONSISTENCY_UNSPECIFIED)
self.assertEqual(request.read_options.transaction, TRANSACTION)

def test_run_query_w_eventual_and_transaction(self):
Expand Down
23 changes: 12 additions & 11 deletions gcloud/datastore/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,9 @@ def test_empty(self):
self.assertEqual(list(pb.distinct_on), [])
self.assertEqual(pb.filter.property_filter.property.name, '')
cfilter = pb.filter.composite_filter
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
self.assertEqual(list(cfilter.filter), [])
self.assertEqual(cfilter.op,
query_pb2.CompositeFilter.OPERATOR_UNSPECIFIED)
self.assertEqual(list(cfilter.filters), [])
self.assertEqual(pb.start_cursor, b'')
self.assertEqual(pb.end_cursor, b'')
self.assertEqual(pb.limit.value, 0)
Expand All @@ -584,9 +585,9 @@ def test_ancestor(self):
ancestor = Key('Ancestor', 123, project='PROJECT')
pb = self._callFUT(_Query(ancestor=ancestor))
cfilter = pb.filter.composite_filter
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filter), 1)
pfilter = cfilter.filter[0].property_filter
self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filters), 1)
pfilter = cfilter.filters[0].property_filter
self.assertEqual(pfilter.property.name, '__key__')
ancestor_pb = _prepare_key_for_request(ancestor.to_protobuf())
self.assertEqual(pfilter.value.key_value, ancestor_pb)
Expand All @@ -600,9 +601,9 @@ def test_filter(self):
}
pb = self._callFUT(query)
cfilter = pb.filter.composite_filter
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filter), 1)
pfilter = cfilter.filter[0].property_filter
self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filters), 1)
pfilter = cfilter.filters[0].property_filter
self.assertEqual(pfilter.property.name, 'name')
self.assertEqual(pfilter.value.string_value, u'John')

Expand All @@ -618,9 +619,9 @@ def test_filter_key(self):
}
pb = self._callFUT(query)
cfilter = pb.filter.composite_filter
self.assertEqual(cfilter.operator, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filter), 1)
pfilter = cfilter.filter[0].property_filter
self.assertEqual(cfilter.op, query_pb2.CompositeFilter.AND)
self.assertEqual(len(cfilter.filters), 1)
pfilter = cfilter.filters[0].property_filter
self.assertEqual(pfilter.property.name, '__key__')
key_pb = _prepare_key_for_request(key.to_protobuf())
self.assertEqual(pfilter.value.key_value, key_pb)
Expand Down

0 comments on commit eadd0e5

Please sign in to comment.