Skip to content

Commit c643d91

Browse files
authored
fix: respect transform values passed into collection.add (#7072)
closes #6826 respect transform values passed into collection.add
1 parent fe90c44 commit c643d91

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

google/cloud/firestore_v1beta1/collection.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ def add(self, document_data, document_id=None):
159159
"""
160160
if document_id is None:
161161
parent_path, expected_prefix = self._parent_info()
162-
document_pb = document_pb2.Document(
163-
fields=_helpers.encode_dict(document_data)
164-
)
162+
163+
document_pb = document_pb2.Document()
165164

166165
created_document_pb = self._client._firestore_api.create_document(
167166
parent_path,
@@ -174,7 +173,8 @@ def add(self, document_data, document_id=None):
174173

175174
new_document_id = _helpers.get_doc_id(created_document_pb, expected_prefix)
176175
document_ref = self.document(new_document_id)
177-
return created_document_pb.update_time, document_ref
176+
set_result = document_ref.set(document_data)
177+
return set_result.update_time, document_ref
178178
else:
179179
document_ref = self.document(document_id)
180180
write_result = document_ref.create(document_data)

tests/system.py

-2
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,6 @@ def test_collection_add(client, cleanup):
409409
cleanup(document_ref1)
410410
snapshot1 = document_ref1.get()
411411
assert snapshot1.to_dict() == data1
412-
assert snapshot1.create_time == update_time1
413412
assert snapshot1.update_time == update_time1
414413
assert RANDOM_ID_REGEX.match(document_ref1.id)
415414

@@ -429,7 +428,6 @@ def test_collection_add(client, cleanup):
429428
cleanup(document_ref3)
430429
snapshot3 = document_ref3.get()
431430
assert snapshot3.to_dict() == data3
432-
assert snapshot3.create_time == update_time3
433431
assert snapshot3.update_time == update_time3
434432
assert RANDOM_ID_REGEX.match(document_ref3.id)
435433

tests/unit/test_collection.py

+24-8
Original file line numberDiff line numberDiff line change
@@ -191,11 +191,21 @@ def test__parent_info_nested(self):
191191

192192
def test_add_auto_assigned(self):
193193
from google.cloud.firestore_v1beta1.proto import document_pb2
194-
from google.cloud.firestore_v1beta1 import _helpers
195194
from google.cloud.firestore_v1beta1.document import DocumentReference
195+
from google.cloud.firestore_v1beta1 import SERVER_TIMESTAMP
196+
from google.cloud.firestore_v1beta1._helpers import pbs_for_set_no_merge
196197

197198
# Create a minimal fake GAPIC add attach it to a real client.
198-
firestore_api = mock.Mock(spec=["create_document"])
199+
firestore_api = mock.Mock(spec=["create_document", "commit"])
200+
write_result = mock.Mock(
201+
update_time=mock.sentinel.update_time, spec=["update_time"]
202+
)
203+
commit_response = mock.Mock(
204+
write_results=[write_result],
205+
spec=["write_results", "commit_time"],
206+
commit_time=mock.sentinel.commit_time,
207+
)
208+
firestore_api.commit.return_value = commit_response
199209
create_doc_response = document_pb2.Document()
200210
firestore_api.create_document.return_value = create_doc_response
201211
client = _make_client()
@@ -212,20 +222,19 @@ def test_add_auto_assigned(self):
212222
create_doc_response.update_time.FromDatetime(datetime.datetime.utcnow())
213223
firestore_api.create_document.return_value = create_doc_response
214224

215-
# Actually call add() on our collection.
216-
document_data = {"been": "here"}
225+
# Actually call add() on our collection; include a transform to make
226+
# sure transforms during adds work.
227+
document_data = {"been": "here", "now": SERVER_TIMESTAMP}
217228
update_time, document_ref = collection.add(document_data)
218229

219230
# Verify the response and the mocks.
220-
self.assertIs(update_time, create_doc_response.update_time)
231+
self.assertIs(update_time, mock.sentinel.update_time)
221232
self.assertIsInstance(document_ref, DocumentReference)
222233
self.assertIs(document_ref._client, client)
223234
expected_path = collection._path + (auto_assigned_id,)
224235
self.assertEqual(document_ref._path, expected_path)
225236

226-
expected_document_pb = document_pb2.Document(
227-
fields=_helpers.encode_dict(document_data)
228-
)
237+
expected_document_pb = document_pb2.Document()
229238
firestore_api.create_document.assert_called_once_with(
230239
parent_path,
231240
collection_id=collection.id,
@@ -234,6 +243,13 @@ def test_add_auto_assigned(self):
234243
mask=None,
235244
metadata=client._rpc_metadata,
236245
)
246+
write_pbs = pbs_for_set_no_merge(document_ref._document_path, document_data)
247+
firestore_api.commit.assert_called_once_with(
248+
client._database_string,
249+
write_pbs,
250+
transaction=None,
251+
metadata=client._rpc_metadata,
252+
)
237253

238254
@staticmethod
239255
def _write_pb_for_create(document_path, document_data):

0 commit comments

Comments
 (0)