def _make_query_response(**kwargs): # kwargs supported are ``skipped_results``, ``name`` and ``data`` from google.cloud.firestore_v1beta1.proto import document_pb2 from google.cloud.firestore_v1beta1.proto import firestore_pb2 from google.cloud._helpers import _datetime_to_pb_timestamp from google.cloud.firestore_v1beta1 import _helpers now = datetime.datetime.utcnow() read_time = _datetime_to_pb_timestamp(now) kwargs['read_time'] = read_time name = kwargs.pop('name', None) data = kwargs.pop('data', None) if name is not None and data is not None: document_pb = document_pb2.Document( name=name, fields=_helpers.encode_dict(data), ) delta = datetime.timedelta(seconds=100) update_time = _datetime_to_pb_timestamp(now - delta) create_time = _datetime_to_pb_timestamp(now - 2 * delta) document_pb.update_time.CopyFrom(update_time) document_pb.create_time.CopyFrom(create_time) kwargs['document'] = document_pb return firestore_pb2.RunQueryResponse(**kwargs)
def test_run_query(self, mock_create_stub): # Mock gRPC layer grpc_stub = mock.Mock() mock_create_stub.return_value = grpc_stub client = firestore_client.FirestoreClient() # Mock request parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') # Mock response transaction = b'-34' skipped_results = 880286183 expected_response = { 'transaction': transaction, 'skipped_results': skipped_results } expected_response = firestore_pb2.RunQueryResponse(**expected_response) grpc_stub.RunQuery.return_value = iter([expected_response]) response = client.run_query(parent) resources = list(response) self.assertEqual(1, len(resources)) self.assertEqual(expected_response, resources[0]) grpc_stub.RunQuery.assert_called_once() args, kwargs = grpc_stub.RunQuery.call_args self.assertEqual(len(args), 2) self.assertEqual(len(kwargs), 1) self.assertIn('metadata', kwargs) actual_request = args[0] expected_request = firestore_pb2.RunQueryRequest(parent=parent) self.assertEqual(expected_request, actual_request)
def test_run_query(self): # Setup Expected Response transaction = b"-34" skipped_results = 880286183 expected_response = { "transaction": transaction, "skipped_results": skipped_results, } expected_response = firestore_pb2.RunQueryResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) patch = mock.patch("google.api_core.grpc_helpers.create_channel") with patch as create_channel: create_channel.return_value = channel client = firestore_client.FirestoreClient() # Setup Request parent = client.any_path_path("[PROJECT]", "[DATABASE]", "[DOCUMENT]", "[ANY_PATH]") response = client.run_query(parent) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.RunQueryRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request
def test_run_query(self): # Setup Expected Response transaction = b'-34' skipped_results = 880286183 expected_response = { 'transaction': transaction, 'skipped_results': skipped_results } expected_response = firestore_pb2.RunQueryResponse(**expected_response) # Mock the API response channel = ChannelStub(responses=[iter([expected_response])]) client = firestore_client.FirestoreClient(channel=channel) # Setup Request parent = client.any_path_path('[PROJECT]', '[DATABASE]', '[DOCUMENT]', '[ANY_PATH]') response = client.run_query(parent) resources = list(response) assert len(resources) == 1 assert expected_response == resources[0] assert len(channel.requests) == 1 expected_request = firestore_pb2.RunQueryRequest(parent=parent) actual_request = channel.requests[0][1] assert expected_request == actual_request