1414
1515import pytest
1616import aiounittest
17+
1718import mock
19+ from tests .unit .v1 .test__helpers import AsyncMock
1820
1921
2022class TestAsyncTransaction (aiounittest .AsyncTestCase ):
@@ -80,15 +82,10 @@ def test__clean_up(self):
8082
8183 @pytest .mark .asyncio
8284 async def test__begin (self ):
83- from google .cloud .firestore_v1 .services .firestore import (
84- client as firestore_client ,
85- )
8685 from google .cloud .firestore_v1 .types import firestore
8786
8887 # Create a minimal fake GAPIC with a dummy result.
89- firestore_api = mock .create_autospec (
90- firestore_client .FirestoreClient , instance = True
91- )
88+ firestore_api = AsyncMock ()
9289 txn_id = b"to-begin"
9390 response = firestore .BeginTransactionResponse (transaction = txn_id )
9491 firestore_api .begin_transaction .return_value = response
@@ -128,14 +125,9 @@ async def test__begin_failure(self):
128125 @pytest .mark .asyncio
129126 async def test__rollback (self ):
130127 from google .protobuf import empty_pb2
131- from google .cloud .firestore_v1 .services .firestore import (
132- client as firestore_client ,
133- )
134128
135129 # Create a minimal fake GAPIC with a dummy result.
136- firestore_api = mock .create_autospec (
137- firestore_client .FirestoreClient , instance = True
138- )
130+ firestore_api = AsyncMock ()
139131 firestore_api .rollback .return_value = empty_pb2 .Empty ()
140132
141133 # Attach the fake GAPIC to a real client.
@@ -172,14 +164,9 @@ async def test__rollback_not_allowed(self):
172164 @pytest .mark .asyncio
173165 async def test__rollback_failure (self ):
174166 from google .api_core import exceptions
175- from google .cloud .firestore_v1 .services .firestore import (
176- client as firestore_client ,
177- )
178167
179168 # Create a minimal fake GAPIC with a dummy failure.
180- firestore_api = mock .create_autospec (
181- firestore_client .FirestoreClient , instance = True
182- )
169+ firestore_api = AsyncMock ()
183170 exc = exceptions .InternalServerError ("Fire during rollback." )
184171 firestore_api .rollback .side_effect = exc
185172
@@ -207,16 +194,11 @@ async def test__rollback_failure(self):
207194
208195 @pytest .mark .asyncio
209196 async def test__commit (self ):
210- from google .cloud .firestore_v1 .services .firestore import (
211- client as firestore_client ,
212- )
213197 from google .cloud .firestore_v1 .types import firestore
214198 from google .cloud .firestore_v1 .types import write
215199
216200 # Create a minimal fake GAPIC with a dummy result.
217- firestore_api = mock .create_autospec (
218- firestore_client .FirestoreClient , instance = True
219- )
201+ firestore_api = AsyncMock ()
220202 commit_response = firestore .CommitResponse (write_results = [write .WriteResult ()])
221203 firestore_api .commit .return_value = commit_response
222204
@@ -262,14 +244,9 @@ async def test__commit_not_allowed(self):
262244 @pytest .mark .asyncio
263245 async def test__commit_failure (self ):
264246 from google .api_core import exceptions
265- from google .cloud .firestore_v1 .services .firestore import (
266- client as firestore_client ,
267- )
268247
269248 # Create a minimal fake GAPIC with a dummy failure.
270- firestore_api = mock .create_autospec (
271- firestore_client .FirestoreClient , instance = True
272- )
249+ firestore_api = AsyncMock ()
273250 exc = exceptions .InternalServerError ("Fire during commit." )
274251 firestore_api .commit .side_effect = exc
275252
@@ -304,7 +281,7 @@ async def test__commit_failure(self):
304281
305282 @pytest .mark .asyncio
306283 async def test_get_all (self ):
307- client = mock . Mock (spec = ["get_all" ])
284+ client = AsyncMock (spec = ["get_all" ])
308285 transaction = self ._make_one (client )
309286 ref1 , ref2 = mock .Mock (), mock .Mock ()
310287 result = await transaction .get_all ([ref1 , ref2 ])
@@ -315,7 +292,7 @@ async def test_get_all(self):
315292 async def test_get_document_ref (self ):
316293 from google .cloud .firestore_v1 .async_document import AsyncDocumentReference
317294
318- client = mock . Mock (spec = ["get_all" ])
295+ client = AsyncMock (spec = ["get_all" ])
319296 transaction = self ._make_one (client )
320297 ref = AsyncDocumentReference ("documents" , "doc-id" )
321298 result = await transaction .get (ref )
@@ -326,10 +303,10 @@ async def test_get_document_ref(self):
326303 async def test_get_w_query (self ):
327304 from google .cloud .firestore_v1 .async_query import AsyncQuery
328305
329- client = mock . Mock (spec = [])
306+ client = AsyncMock (spec = [])
330307 transaction = self ._make_one (client )
331- query = AsyncQuery (parent = mock . Mock (spec = []))
332- query .stream = mock . MagicMock ()
308+ query = AsyncQuery (parent = AsyncMock (spec = []))
309+ query .stream = AsyncMock ()
333310 result = await transaction .get (query )
334311 query .stream .assert_called_once_with (transaction = transaction )
335312 self .assertIs (result , query .stream .return_value )
@@ -804,14 +781,9 @@ async def _call_fut(client, write_pbs, transaction_id):
804781 @mock .patch ("google.cloud.firestore_v1.async_transaction._sleep" )
805782 @pytest .mark .asyncio
806783 async def test_success_first_attempt (self , _sleep ):
807- from google .cloud .firestore_v1 .services .firestore import (
808- client as firestore_client ,
809- )
810784
811785 # Create a minimal fake GAPIC with a dummy result.
812- firestore_api = mock .create_autospec (
813- firestore_client .FirestoreClient , instance = True
814- )
786+ firestore_api = AsyncMock ()
815787
816788 # Attach the fake GAPIC to a real client.
817789 client = _make_client ("summer" )
@@ -839,14 +811,10 @@ async def test_success_first_attempt(self, _sleep):
839811 @pytest .mark .asyncio
840812 async def test_success_third_attempt (self , _sleep ):
841813 from google .api_core import exceptions
842- from google .cloud .firestore_v1 .services .firestore import (
843- client as firestore_client ,
844- )
845814
846815 # Create a minimal fake GAPIC with a dummy result.
847- firestore_api = mock .create_autospec (
848- firestore_client .FirestoreClient , instance = True
849- )
816+ firestore_api = AsyncMock ()
817+
850818 # Make sure the first two requests fail and the third succeeds.
851819 firestore_api .commit .side_effect = [
852820 exceptions .ServiceUnavailable ("Server sleepy." ),
@@ -885,14 +853,10 @@ async def test_success_third_attempt(self, _sleep):
885853 @pytest .mark .asyncio
886854 async def test_failure_first_attempt (self , _sleep ):
887855 from google .api_core import exceptions
888- from google .cloud .firestore_v1 .services .firestore import (
889- client as firestore_client ,
890- )
891856
892857 # Create a minimal fake GAPIC with a dummy result.
893- firestore_api = mock .create_autospec (
894- firestore_client .FirestoreClient , instance = True
895- )
858+ firestore_api = AsyncMock ()
859+
896860 # Make sure the first request fails with an un-retryable error.
897861 exc = exceptions .ResourceExhausted ("We ran out of fries." )
898862 firestore_api .commit .side_effect = exc
@@ -923,14 +887,10 @@ async def test_failure_first_attempt(self, _sleep):
923887 @pytest .mark .asyncio
924888 async def test_failure_second_attempt (self , _sleep ):
925889 from google .api_core import exceptions
926- from google .cloud .firestore_v1 .services .firestore import (
927- client as firestore_client ,
928- )
929890
930891 # Create a minimal fake GAPIC with a dummy result.
931- firestore_api = mock .create_autospec (
932- firestore_client .FirestoreClient , instance = True
933- )
892+ firestore_api = AsyncMock ()
893+
934894 # Make sure the first request fails retry-able and second
935895 # fails non-retryable.
936896 exc1 = exceptions .ServiceUnavailable ("Come back next time." )
@@ -1031,15 +991,12 @@ def _make_client(project="feral-tom-cat"):
1031991
1032992def _make_transaction (txn_id , ** txn_kwargs ):
1033993 from google .protobuf import empty_pb2
1034- from google .cloud .firestore_v1 .services .firestore import client as firestore_client
1035994 from google .cloud .firestore_v1 .types import firestore
1036995 from google .cloud .firestore_v1 .types import write
1037996 from google .cloud .firestore_v1 .async_transaction import AsyncTransaction
1038997
1039998 # Create a fake GAPIC ...
1040- firestore_api = mock .create_autospec (
1041- firestore_client .FirestoreClient , instance = True
1042- )
999+ firestore_api = AsyncMock ()
10431000 # ... with a dummy ``BeginTransactionResponse`` result ...
10441001 begin_response = firestore .BeginTransactionResponse (transaction = txn_id )
10451002 firestore_api .begin_transaction .return_value = begin_response
0 commit comments