1212# See the License for the specific language governing permissions and
1313# limitations under the License.
1414
15- import datetime
1615import types
1716import 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