def _create_helper(self, retry=None, timeout=None): from google.cloud.firestore_v1 import _helpers # Create a minimal fake GAPIC with a dummy response. firestore_api = mock.Mock() firestore_api.commit.mock_add_spec(spec=["commit"]) firestore_api.commit.return_value = self._make_commit_repsonse() # Attach the fake GAPIC to a real client. client = _make_client("dignity") client._firestore_api_internal = firestore_api # Actually make a document and call create(). document = self._make_one("foo", "twelve", client=client) document_data = {"hello": "goodbye", "count": 99} kwargs = _helpers.make_retry_timeout_kwargs(retry, timeout) write_result = document.create(document_data, **kwargs) # Verify the response and the mocks. self.assertIs(write_result, mock.sentinel.write_result) write_pb = self._write_pb_for_create(document._document_path, document_data) firestore_api.commit.assert_called_once_with( request={ "database": client._database_string, "writes": [write_pb], "transaction": None, }, metadata=client._rpc_metadata, **kwargs, )
def test_create_empty(self): # Create a minimal fake GAPIC with a dummy response. from google.cloud.firestore_v1.document import DocumentReference from google.cloud.firestore_v1.document import DocumentSnapshot firestore_api = mock.Mock(spec=["commit"]) document_reference = mock.create_autospec(DocumentReference) snapshot = mock.create_autospec(DocumentSnapshot) snapshot.exists = True document_reference.get.return_value = snapshot firestore_api.commit.return_value = self._make_commit_repsonse( write_results=[document_reference]) # Attach the fake GAPIC to a real client. client = _make_client("dignity") client._firestore_api_internal = firestore_api client.get_all = mock.MagicMock() client.get_all.exists.return_value = True # Actually make a document and call create(). document = self._make_one("foo", "twelve", client=client) document_data = {} write_result = document.create(document_data) self.assertTrue(write_result.get().exists)