async def test_stream_second_response_in_empty_stream(self): # Create a minimal fake GAPIC with a dummy response. firestore_api = AsyncMock(spec=["run_query"]) empty_response1 = _make_query_response() empty_response2 = _make_query_response() run_query_response = AsyncIter([empty_response1, empty_response2]) firestore_api.run_query.return_value = run_query_response # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("dah", "dah", "dum") query = self._make_one(parent) get_response = query.stream() self.assertIsInstance(get_response, types.AsyncGeneratorType) self.assertEqual([x async for x in get_response], []) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, )
async def test_get_limit_to_last(self): from google.cloud import firestore from google.cloud.firestore_v1.base_query import _enum_from_direction # Create a minimal fake GAPIC. firestore_api = AsyncMock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("dee") # Add a dummy response to the minimal fake GAPIC. _, expected_prefix = parent._parent_info() name = "{}/sleep".format(expected_prefix) data = {"snooze": 10} data2 = {"snooze": 20} response_pb = _make_query_response(name=name, data=data) response_pb2 = _make_query_response(name=name, data=data2) firestore_api.run_query.return_value = AsyncIter( [response_pb2, response_pb]) # Execute the query and check the response. query = self._make_one(parent) query = query.order_by( "snooze", direction=firestore.AsyncQuery.DESCENDING).limit_to_last(2) returned = await query.get() self.assertIsInstance(returned, list) self.assertEqual( query._orders[0].direction, _enum_from_direction(firestore.AsyncQuery.ASCENDING), ) self.assertEqual(len(returned), 2) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("dee", "sleep")) self.assertEqual(snapshot.to_dict(), data) snapshot2 = returned[1] self.assertEqual(snapshot2.reference._path, ("dee", "sleep")) self.assertEqual(snapshot2.to_dict(), data2) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, )
def test_get(self): # Create a minimal fake GAPIC. firestore_api = mock.Mock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("dee") # Add a dummy response to the minimal fake GAPIC. _, expected_prefix = parent._parent_info() name = "{}/sleep".format(expected_prefix) data = {"snooze": 10} response_pb = _make_query_response(name=name, data=data) firestore_api.run_query.return_value = iter([response_pb]) # Execute the query and check the response. query = self._make_one(parent) returned = query.get() self.assertIsInstance(returned, list) self.assertEqual(len(returned), 1) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("dee", "sleep")) self.assertEqual(snapshot.to_dict(), data)
async def test_stream_w_collection_group(self): # Create a minimal fake GAPIC. firestore_api = AsyncMock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("charles") other = client.collection("dora") # Add two dummy responses to the minimal fake GAPIC. _, other_prefix = other._parent_info() name = "{}/bark".format(other_prefix) data = {"lee": "hoop"} response_pb1 = _make_query_response(name=name, data=data) response_pb2 = _make_query_response() firestore_api.run_query.return_value = AsyncIter( [response_pb1, response_pb2]) # Execute the query and check the response. query = self._make_one(parent) query._all_descendants = True get_response = query.stream() self.assertIsInstance(get_response, types.AsyncGeneratorType) returned = [x async for x in get_response] self.assertEqual(len(returned), 1) snapshot = returned[0] to_match = other.document("bark") self.assertEqual(snapshot.reference._document_path, to_match._document_path) self.assertEqual(snapshot.to_dict(), data) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, )
async def test_stream_with_skipped_results(self): # Create a minimal fake GAPIC. firestore_api = AsyncMock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("talk", "and", "chew-gum") # Add two dummy responses to the minimal fake GAPIC. _, expected_prefix = parent._parent_info() response_pb1 = _make_query_response(skipped_results=1) name = "{}/clock".format(expected_prefix) data = {"noon": 12, "nested": {"bird": 10.5}} response_pb2 = _make_query_response(name=name, data=data) firestore_api.run_query.return_value = AsyncIter( [response_pb1, response_pb2]) # Execute the query and check the response. query = self._make_one(parent) get_response = query.stream() self.assertIsInstance(get_response, types.AsyncGeneratorType) returned = [x async for x in get_response] self.assertEqual(len(returned), 1) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("talk", "and", "chew-gum", "clock")) self.assertEqual(snapshot.to_dict(), data) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, )
def test_stream_empty_after_first_response(self): # Create a minimal fake GAPIC. firestore_api = mock.Mock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("charles") # Add two dummy responses to the minimal fake GAPIC. _, expected_prefix = parent._parent_info() name = "{}/bark".format(expected_prefix) data = {"lee": "hoop"} response_pb1 = _make_query_response(name=name, data=data) response_pb2 = _make_query_response() firestore_api.run_query.return_value = iter( [response_pb1, response_pb2]) # Execute the query and check the response. query = self._make_one(parent) get_response = query.stream() self.assertIsInstance(get_response, types.GeneratorType) returned = list(get_response) self.assertEqual(len(returned), 1) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("charles", "bark")) self.assertEqual(snapshot.to_dict(), data) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, )
async def _get_helper(self, retry=None, timeout=None): from google.cloud.firestore_v1 import _helpers # Create a minimal fake GAPIC. firestore_api = AsyncMock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Make a **real** collection reference as parent. parent = client.collection("dee") # Add a dummy response to the minimal fake GAPIC. _, expected_prefix = parent._parent_info() name = "{}/sleep".format(expected_prefix) data = {"snooze": 10} response_pb = _make_query_response(name=name, data=data) firestore_api.run_query.return_value = AsyncIter([response_pb]) kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) # Execute the query and check the response. query = self._make_one(parent) returned = await query.get(**kwargs) self.assertIsInstance(returned, list) self.assertEqual(len(returned), 1) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("dee", "sleep")) self.assertEqual(snapshot.to_dict(), data) # Verify the mock call. parent_path, _ = parent._parent_info() firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": None, }, metadata=client._rpc_metadata, **kwargs, )
async def test_stream_with_transaction(self): # Create a minimal fake GAPIC. firestore_api = AsyncMock(spec=["run_query"]) # Attach the fake GAPIC to a real client. client = _make_client() client._firestore_api_internal = firestore_api # Create a real-ish transaction for this client. transaction = client.transaction() txn_id = b"\x00\x00\x01-work-\xf2" transaction._id = txn_id # Make a **real** collection reference as parent. parent = client.collection("declaration") # Add a dummy response to the minimal fake GAPIC. parent_path, expected_prefix = parent._parent_info() name = "{}/burger".format(expected_prefix) data = {"lettuce": b"\xee\x87"} response_pb = _make_query_response(name=name, data=data) firestore_api.run_query.return_value = AsyncIter([response_pb]) # Execute the query and check the response. query = self._make_one(parent) get_response = query.stream(transaction=transaction) self.assertIsInstance(get_response, types.AsyncGeneratorType) returned = [x async for x in get_response] self.assertEqual(len(returned), 1) snapshot = returned[0] self.assertEqual(snapshot.reference._path, ("declaration", "burger")) self.assertEqual(snapshot.to_dict(), data) # Verify the mock call. firestore_api.run_query.assert_called_once_with( request={ "parent": parent_path, "structured_query": query._to_protobuf(), "transaction": txn_id, }, metadata=client._rpc_metadata, )