Skip to content

Commit ac0c96b

Browse files
authored
fix(firestore): simplify 'Collection.add', avoid spurious API call (#9634)
Closes #9629
1 parent dd99d3e commit ac0c96b

2 files changed

Lines changed: 15 additions & 43 deletions

File tree

packages/google-cloud-firestore/google/cloud/firestore_v1/collection.py

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
from google.cloud.firestore_v1 import _helpers
2222
from google.cloud.firestore_v1 import query as query_mod
23-
from google.cloud.firestore_v1.proto import document_pb2
2423
from google.cloud.firestore_v1.watch import Watch
2524
from google.cloud.firestore_v1 import document
2625

@@ -157,27 +156,11 @@ def add(self, document_data, document_id=None):
157156
and the document already exists.
158157
"""
159158
if document_id is None:
160-
parent_path, expected_prefix = self._parent_info()
161-
162-
document_pb = document_pb2.Document()
163-
164-
created_document_pb = self._client._firestore_api.create_document(
165-
parent_path,
166-
collection_id=self.id,
167-
document_id=None,
168-
document=document_pb,
169-
mask=None,
170-
metadata=self._client._rpc_metadata,
171-
)
159+
document_id = _auto_id()
172160

173-
new_document_id = _helpers.get_doc_id(created_document_pb, expected_prefix)
174-
document_ref = self.document(new_document_id)
175-
set_result = document_ref.set(document_data)
176-
return set_result.update_time, document_ref
177-
else:
178-
document_ref = self.document(document_id)
179-
write_result = document_ref.create(document_data)
180-
return write_result.update_time, document_ref
161+
document_ref = self.document(document_id)
162+
write_result = document_ref.create(document_data)
163+
return write_result.update_time, document_ref
181164

182165
def list_documents(self, page_size=None):
183166
"""List all subdocuments of the current collection.

packages/google-cloud-firestore/tests/unit/v1/test_collection.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import datetime
1615
import types
1716
import unittest
1817

@@ -193,7 +192,7 @@ def test_add_auto_assigned(self):
193192
from google.cloud.firestore_v1.proto import document_pb2
194193
from google.cloud.firestore_v1.document import DocumentReference
195194
from google.cloud.firestore_v1 import SERVER_TIMESTAMP
196-
from google.cloud.firestore_v1._helpers import pbs_for_set_no_merge
195+
from google.cloud.firestore_v1._helpers import pbs_for_create
197196

198197
# Create a minimal fake GAPIC add attach it to a real client.
199198
firestore_api = mock.Mock(spec=["create_document", "commit"])
@@ -214,42 +213,32 @@ def test_add_auto_assigned(self):
214213
# Actually make a collection.
215214
collection = self._make_one("grand-parent", "parent", "child", client=client)
216215

217-
# Add a dummy response for the fake GAPIC.
218-
parent_path = collection.parent._document_path
219-
auto_assigned_id = "cheezburger"
220-
name = "{}/{}/{}".format(parent_path, collection.id, auto_assigned_id)
221-
create_doc_response = document_pb2.Document(name=name)
222-
create_doc_response.update_time.FromDatetime(datetime.datetime.utcnow())
223-
firestore_api.create_document.return_value = create_doc_response
224-
225216
# Actually call add() on our collection; include a transform to make
226217
# sure transforms during adds work.
227218
document_data = {"been": "here", "now": SERVER_TIMESTAMP}
228-
update_time, document_ref = collection.add(document_data)
219+
220+
patch = mock.patch("google.cloud.firestore_v1.collection._auto_id")
221+
random_doc_id = "DEADBEEF"
222+
with patch as patched:
223+
patched.return_value = random_doc_id
224+
update_time, document_ref = collection.add(document_data)
229225

230226
# Verify the response and the mocks.
231227
self.assertIs(update_time, mock.sentinel.update_time)
232228
self.assertIsInstance(document_ref, DocumentReference)
233229
self.assertIs(document_ref._client, client)
234-
expected_path = collection._path + (auto_assigned_id,)
230+
expected_path = collection._path + (random_doc_id,)
235231
self.assertEqual(document_ref._path, expected_path)
236232

237-
expected_document_pb = document_pb2.Document()
238-
firestore_api.create_document.assert_called_once_with(
239-
parent_path,
240-
collection_id=collection.id,
241-
document_id=None,
242-
document=expected_document_pb,
243-
mask=None,
244-
metadata=client._rpc_metadata,
245-
)
246-
write_pbs = pbs_for_set_no_merge(document_ref._document_path, document_data)
233+
write_pbs = pbs_for_create(document_ref._document_path, document_data)
247234
firestore_api.commit.assert_called_once_with(
248235
client._database_string,
249236
write_pbs,
250237
transaction=None,
251238
metadata=client._rpc_metadata,
252239
)
240+
# Since we generate the ID locally, we don't call 'create_document'.
241+
firestore_api.create_document.assert_not_called()
253242

254243
@staticmethod
255244
def _write_pb_for_create(document_path, document_data):

0 commit comments

Comments
 (0)