Skip to content

Commit 4e509db

Browse files
committed
Updating unit tests for GAPIC datastore run_query change.
1 parent 341cc96 commit 4e509db

File tree

3 files changed

+155
-194
lines changed

3 files changed

+155
-194
lines changed

datastore/unit_tests/test__gax.py

Lines changed: 2 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -200,46 +200,6 @@ def test_lookup(self):
200200
self.assertEqual(stub.method_calls,
201201
[(request_pb, 'Lookup')])
202202

203-
def test_run_query(self):
204-
return_val = object()
205-
stub = _GRPCStub(return_val)
206-
datastore_api, _ = self._make_one(stub=stub)
207-
208-
request_pb = mock.Mock(project_id=None, spec=['project_id'])
209-
project = 'PROJECT'
210-
result = datastore_api.run_query(project, request_pb)
211-
self.assertIs(result, return_val)
212-
self.assertEqual(request_pb.project_id, project)
213-
self.assertEqual(stub.method_calls,
214-
[(request_pb, 'RunQuery')])
215-
216-
def _run_query_failure_helper(self, exc, err_class):
217-
stub = _GRPCStub(side_effect=exc)
218-
datastore_api, _ = self._make_one(stub=stub)
219-
220-
request_pb = mock.Mock(project_id=None, spec=['project_id'])
221-
project = 'PROJECT'
222-
with self.assertRaises(err_class):
223-
datastore_api.run_query(project, request_pb)
224-
225-
self.assertEqual(request_pb.project_id, project)
226-
self.assertEqual(stub.method_calls,
227-
[(request_pb, 'RunQuery')])
228-
229-
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
230-
def test_run_query_invalid_argument(self):
231-
from grpc import StatusCode
232-
from grpc._channel import _RPCState
233-
from google.cloud.exceptions import BadRequest
234-
from google.cloud.exceptions import GrpcRendezvous
235-
236-
details = ('Cannot have inequality filters on multiple '
237-
'properties: [created, priority]')
238-
exc_state = _RPCState((), None, None,
239-
StatusCode.INVALID_ARGUMENT, details)
240-
exc = GrpcRendezvous(exc_state, None, None, None)
241-
self._run_query_failure_helper(exc, BadRequest)
242-
243203

244204
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
245205
class TestGAPICDatastoreAPI(unittest.TestCase):
@@ -307,20 +267,13 @@ def test_it(self, make_chan, mock_klass):
307267

308268
class _GRPCStub(object):
309269

310-
def __init__(self, return_val=None, side_effect=Exception):
270+
def __init__(self, return_val=None):
311271
self.return_val = return_val
312-
self.side_effect = side_effect
313272
self.method_calls = []
314273

315274
def _method(self, request_pb, name):
316275
self.method_calls.append((request_pb, name))
317-
if self.side_effect is Exception:
318-
return self.return_val
319-
else:
320-
raise self.side_effect
276+
return self.return_val
321277

322278
def Lookup(self, request_pb):
323279
return self._method(request_pb, 'Lookup')
324-
325-
def RunQuery(self, request_pb):
326-
return self._method(request_pb, 'RunQuery')

datastore/unit_tests/test__http.py

Lines changed: 105 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,6 @@ def _get_target_class():
161161

162162
return Connection
163163

164-
def _make_query_pb(self, kind):
165-
from google.cloud.proto.datastore.v1 import query_pb2
166-
167-
pb = query_pb2.Query()
168-
pb.kind.add().name = kind
169-
return pb
170-
171164
def _make_one(self, client, use_grpc=False):
172165
with mock.patch('google.cloud.datastore._http._USE_GRPC',
173166
new=use_grpc):
@@ -279,14 +272,14 @@ def test_lookup_single_key_empty_response_w_eventual(self):
279272
self.assertEqual(request.read_options.transaction, b'')
280273

281274
def test_lookup_single_key_empty_response_w_eventual_and_transaction(self):
282-
PROJECT = 'PROJECT'
283-
TRANSACTION = b'TRANSACTION'
284-
key_pb = _make_key_pb(PROJECT)
275+
project = 'PROJECT'
276+
transaction = b'TRANSACTION'
277+
key_pb = _make_key_pb(project)
285278

286279
client = mock.Mock(spec=['_base_url'])
287280
conn = self._make_one(client)
288-
self.assertRaises(ValueError, conn.lookup, PROJECT, key_pb,
289-
eventual=True, transaction_id=TRANSACTION)
281+
self.assertRaises(ValueError, conn.lookup, project, [key_pb],
282+
eventual=True, transaction_id=transaction)
290283

291284
def test_lookup_single_key_empty_response_w_transaction(self):
292285
from google.cloud.proto.datastore.v1 import datastore_pb2
@@ -472,133 +465,152 @@ def test_lookup_multiple_keys_w_deferred(self):
472465
self.assertEqual(key_pb1, keys[0])
473466
self.assertEqual(key_pb2, keys[1])
474467

468+
469+
class TestHTTPDatastoreAPI(unittest.TestCase):
470+
471+
@staticmethod
472+
def _get_target_class():
473+
from google.cloud.datastore._http import HTTPDatastoreAPI
474+
475+
return HTTPDatastoreAPI
476+
477+
def _make_one(self, *args, **kwargs):
478+
return self._get_target_class()(*args, **kwargs)
479+
480+
@staticmethod
481+
def _make_query_pb(kind):
482+
from google.cloud.proto.datastore.v1 import query_pb2
483+
484+
return query_pb2.Query(
485+
kind=[query_pb2.KindExpression(name=kind)],
486+
)
487+
488+
def test_constructor(self):
489+
client = object()
490+
ds_api = self._make_one(client)
491+
self.assertIs(ds_api.client, client)
492+
475493
def test_run_query_w_eventual_no_transaction(self):
476494
from google.cloud.proto.datastore.v1 import datastore_pb2
495+
from google.cloud.proto.datastore.v1 import entity_pb2
477496
from google.cloud.proto.datastore.v1 import query_pb2
478497

479498
project = 'PROJECT'
480499
kind = 'Nonesuch'
481500
cursor = b'\x00'
482-
q_pb = self._make_query_pb(kind)
483-
rsp_pb = datastore_pb2.RunQueryResponse()
484-
rsp_pb.batch.end_cursor = cursor
485-
no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS
486-
rsp_pb.batch.more_results = no_more
487-
rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL
501+
query_pb = self._make_query_pb(kind)
502+
partition_id = entity_pb2.PartitionId(project_id=project)
503+
read_options = datastore_pb2.ReadOptions(
504+
read_consistency=datastore_pb2.ReadOptions.EVENTUAL)
505+
rsp_pb = datastore_pb2.RunQueryResponse(
506+
batch=query_pb2.QueryResultBatch(
507+
entity_result_type=query_pb2.EntityResult.FULL,
508+
end_cursor=cursor,
509+
more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS,
510+
)
511+
)
488512

489513
# Create mock HTTP and client with response.
490514
http = Http({'status': '200'}, rsp_pb.SerializeToString())
491515
client = mock.Mock(
492516
_http=http, _base_url='test.invalid', spec=['_http', '_base_url'])
493517

494518
# Make request.
495-
conn = self._make_one(client)
496-
response = conn.run_query(project, q_pb, eventual=True)
519+
ds_api = self._make_one(client)
520+
response = ds_api.run_query(
521+
project, partition_id, read_options, query=query_pb)
497522

498523
# Check the result and verify the callers.
499524
self.assertEqual(response, rsp_pb)
500-
uri = _build_expected_url(conn.api_base_url, project, 'runQuery')
525+
uri = _build_expected_url(client._base_url, project, 'runQuery')
501526
cw = http._called_with
502527
_verify_protobuf_call(self, cw, uri)
503528
request = datastore_pb2.RunQueryRequest()
504529
request.ParseFromString(cw['body'])
505-
self.assertEqual(request.partition_id.namespace_id, '')
506-
self.assertEqual(request.query, q_pb)
507-
self.assertEqual(request.read_options.read_consistency,
508-
datastore_pb2.ReadOptions.EVENTUAL)
509-
self.assertEqual(request.read_options.transaction, b'')
530+
self.assertEqual(request.partition_id, partition_id)
531+
self.assertEqual(request.query, query_pb)
532+
self.assertEqual(request.read_options, read_options)
510533

511534
def test_run_query_wo_eventual_w_transaction(self):
512535
from google.cloud.proto.datastore.v1 import datastore_pb2
536+
from google.cloud.proto.datastore.v1 import entity_pb2
513537
from google.cloud.proto.datastore.v1 import query_pb2
514538

515539
project = 'PROJECT'
516540
kind = 'Nonesuch'
517541
cursor = b'\x00'
518542
transaction = b'TRANSACTION'
519-
q_pb = self._make_query_pb(kind)
520-
rsp_pb = datastore_pb2.RunQueryResponse()
521-
rsp_pb.batch.end_cursor = cursor
522-
no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS
523-
rsp_pb.batch.more_results = no_more
524-
rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL
543+
query_pb = self._make_query_pb(kind)
544+
partition_id = entity_pb2.PartitionId(project_id=project)
545+
read_options = datastore_pb2.ReadOptions(transaction=transaction)
546+
rsp_pb = datastore_pb2.RunQueryResponse(
547+
batch=query_pb2.QueryResultBatch(
548+
entity_result_type=query_pb2.EntityResult.FULL,
549+
end_cursor=cursor,
550+
more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS,
551+
)
552+
)
525553

526554
# Create mock HTTP and client with response.
527555
http = Http({'status': '200'}, rsp_pb.SerializeToString())
528556
client = mock.Mock(
529557
_http=http, _base_url='test.invalid', spec=['_http', '_base_url'])
530558

531559
# Make request.
532-
conn = self._make_one(client)
533-
response = conn.run_query(
534-
project, q_pb, transaction_id=transaction)
560+
ds_api = self._make_one(client)
561+
response = ds_api.run_query(
562+
project, partition_id, read_options, query=query_pb)
535563

536564
# Check the result and verify the callers.
537565
self.assertEqual(response, rsp_pb)
538-
uri = _build_expected_url(conn.api_base_url, project, 'runQuery')
566+
uri = _build_expected_url(client._base_url, project, 'runQuery')
539567
cw = http._called_with
540568
_verify_protobuf_call(self, cw, uri)
541569
request = datastore_pb2.RunQueryRequest()
542570
request.ParseFromString(cw['body'])
543-
self.assertEqual(request.partition_id.namespace_id, '')
544-
self.assertEqual(request.query, q_pb)
545-
self.assertEqual(
546-
request.read_options.read_consistency,
547-
datastore_pb2.ReadOptions.READ_CONSISTENCY_UNSPECIFIED)
548-
self.assertEqual(request.read_options.transaction, transaction)
549-
550-
def test_run_query_w_eventual_and_transaction(self):
551-
from google.cloud.proto.datastore.v1 import datastore_pb2
552-
from google.cloud.proto.datastore.v1 import query_pb2
553-
554-
PROJECT = 'PROJECT'
555-
KIND = 'Nonesuch'
556-
CURSOR = b'\x00'
557-
TRANSACTION = b'TRANSACTION'
558-
q_pb = self._make_query_pb(KIND)
559-
rsp_pb = datastore_pb2.RunQueryResponse()
560-
rsp_pb.batch.end_cursor = CURSOR
561-
no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS
562-
rsp_pb.batch.more_results = no_more
563-
rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL
564-
565-
client = mock.Mock(spec=['_base_url'])
566-
conn = self._make_one(client)
567-
self.assertRaises(ValueError, conn.run_query, PROJECT, q_pb,
568-
eventual=True, transaction_id=TRANSACTION)
571+
self.assertEqual(request.partition_id, partition_id)
572+
self.assertEqual(request.query, query_pb)
573+
self.assertEqual(request.read_options, read_options)
569574

570575
def test_run_query_wo_namespace_empty_result(self):
571576
from google.cloud.proto.datastore.v1 import datastore_pb2
577+
from google.cloud.proto.datastore.v1 import entity_pb2
572578
from google.cloud.proto.datastore.v1 import query_pb2
573579

574580
project = 'PROJECT'
575581
kind = 'Nonesuch'
576582
cursor = b'\x00'
577-
q_pb = self._make_query_pb(kind)
578-
rsp_pb = datastore_pb2.RunQueryResponse()
579-
rsp_pb.batch.end_cursor = cursor
580-
no_more = query_pb2.QueryResultBatch.NO_MORE_RESULTS
581-
rsp_pb.batch.more_results = no_more
582-
rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL
583+
query_pb = self._make_query_pb(kind)
584+
partition_id = entity_pb2.PartitionId(project_id=project)
585+
read_options = datastore_pb2.ReadOptions()
586+
rsp_pb = datastore_pb2.RunQueryResponse(
587+
batch=query_pb2.QueryResultBatch(
588+
entity_result_type=query_pb2.EntityResult.FULL,
589+
end_cursor=cursor,
590+
more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS,
591+
)
592+
)
583593

584594
# Create mock HTTP and client with response.
585595
http = Http({'status': '200'}, rsp_pb.SerializeToString())
586596
client = mock.Mock(
587597
_http=http, _base_url='test.invalid', spec=['_http', '_base_url'])
588598

589599
# Make request.
590-
conn = self._make_one(client)
591-
response = conn.run_query(project, q_pb)
600+
ds_api = self._make_one(client)
601+
response = ds_api.run_query(
602+
project, partition_id, read_options, query=query_pb)
592603

593604
# Check the result and verify the callers.
594605
self.assertEqual(response, rsp_pb)
595-
uri = _build_expected_url(conn.api_base_url, project, 'runQuery')
606+
uri = _build_expected_url(client._base_url, project, 'runQuery')
596607
cw = http._called_with
597608
_verify_protobuf_call(self, cw, uri)
598609
request = datastore_pb2.RunQueryRequest()
599610
request.ParseFromString(cw['body'])
600-
self.assertEqual(request.partition_id.namespace_id, '')
601-
self.assertEqual(request.query, q_pb)
611+
self.assertEqual(request.partition_id, partition_id)
612+
self.assertEqual(request.query, query_pb)
613+
self.assertEqual(request.read_options, read_options)
602614

603615
def test_run_query_w_namespace_nonempty_result(self):
604616
from google.cloud.proto.datastore.v1 import datastore_pb2
@@ -607,49 +619,40 @@ def test_run_query_w_namespace_nonempty_result(self):
607619

608620
project = 'PROJECT'
609621
kind = 'Kind'
610-
entity = entity_pb2.Entity()
611-
q_pb = self._make_query_pb(kind)
612-
rsp_pb = datastore_pb2.RunQueryResponse()
613-
rsp_pb.batch.entity_results.add(entity=entity)
614-
rsp_pb.batch.entity_result_type = query_pb2.EntityResult.FULL
615-
rsp_pb.batch.more_results = query_pb2.QueryResultBatch.NO_MORE_RESULTS
622+
namespace = 'NS'
623+
query_pb = self._make_query_pb(kind)
624+
partition_id = entity_pb2.PartitionId(
625+
project_id=project, namespace_id=namespace)
626+
read_options = datastore_pb2.ReadOptions()
627+
rsp_pb = datastore_pb2.RunQueryResponse(
628+
batch=query_pb2.QueryResultBatch(
629+
entity_result_type=query_pb2.EntityResult.FULL,
630+
entity_results=[
631+
query_pb2.EntityResult(entity=entity_pb2.Entity()),
632+
],
633+
more_results=query_pb2.QueryResultBatch.NO_MORE_RESULTS,
634+
)
635+
)
616636

617637
# Create mock HTTP and client with response.
618638
http = Http({'status': '200'}, rsp_pb.SerializeToString())
619639
client = mock.Mock(
620640
_http=http, _base_url='test.invalid', spec=['_http', '_base_url'])
621641

622642
# Make request.
623-
conn = self._make_one(client)
624-
namespace = 'NS'
625-
response = conn.run_query(project, q_pb, namespace=namespace)
643+
ds_api = self._make_one(client)
644+
response = ds_api.run_query(
645+
project, partition_id, read_options, query=query_pb)
626646

627647
# Check the result and verify the callers.
628648
self.assertEqual(response, rsp_pb)
629649
cw = http._called_with
630-
uri = _build_expected_url(conn.api_base_url, project, 'runQuery')
650+
uri = _build_expected_url(client._base_url, project, 'runQuery')
631651
_verify_protobuf_call(self, cw, uri)
632652
request = datastore_pb2.RunQueryRequest()
633653
request.ParseFromString(cw['body'])
634-
self.assertEqual(request.partition_id.namespace_id, namespace)
635-
self.assertEqual(request.query, q_pb)
636-
637-
638-
class TestHTTPDatastoreAPI(unittest.TestCase):
639-
640-
@staticmethod
641-
def _get_target_class():
642-
from google.cloud.datastore._http import HTTPDatastoreAPI
643-
644-
return HTTPDatastoreAPI
645-
646-
def _make_one(self, *args, **kwargs):
647-
return self._get_target_class()(*args, **kwargs)
648-
649-
def test_constructor(self):
650-
client = object()
651-
ds_api = self._make_one(client)
652-
self.assertIs(ds_api.client, client)
654+
self.assertEqual(request.partition_id, partition_id)
655+
self.assertEqual(request.query, query_pb)
653656

654657
def test_begin_transaction(self):
655658
from google.cloud.proto.datastore.v1 import datastore_pb2

0 commit comments

Comments
 (0)