@@ -463,74 +463,90 @@ def test_delete_with_option(self):
463
463
)
464
464
self ._delete_helper (last_update_time = timestamp_pb )
465
465
466
- def test_get_w_single_field_path (self ):
467
- client = mock .Mock (spec = [])
466
+ def _get_helper (
467
+ self , field_paths = None , use_transaction = False , not_found = False ):
468
+ from google .api_core .exceptions import NotFound
469
+ from google .cloud .firestore_v1beta1 .proto import common_pb2
470
+ from google .cloud .firestore_v1beta1 .proto import document_pb2
471
+ from google .cloud .firestore_v1beta1 .transaction import Transaction
468
472
469
- document = self ._make_one ('yellow' , 'mellow' , client = client )
470
- with self .assertRaises (ValueError ):
471
- document .get ('foo' )
473
+ # Create a minimal fake GAPIC with a dummy response.
474
+ create_time = 123
475
+ update_time = 234
476
+ firestore_api = mock .Mock (spec = ['get_document' ])
477
+ response = mock .create_autospec (document_pb2 .Document )
478
+ response .fields = {}
479
+ response .create_time = create_time
480
+ response .update_time = update_time
481
+
482
+ if not_found :
483
+ firestore_api .get_document .side_effect = NotFound ('testing' )
484
+ else :
485
+ firestore_api .get_document .return_value = response
472
486
473
- def test_get_success (self ):
474
- # Create a minimal fake client with a dummy response.
475
- response_iterator = iter ([mock .sentinel .snapshot ])
476
- client = mock .Mock (spec = ['get_all' ])
477
- client .get_all .return_value = response_iterator
487
+ client = _make_client ('donut-base' )
488
+ client ._firestore_api_internal = firestore_api
478
489
479
- # Actually make a document and call get().
480
- document = self ._make_one ('yellow' , 'mellow' , client = client )
481
- snapshot = document .get ()
490
+ document = self ._make_one ('where' , 'we-are' , client = client )
482
491
483
- # Verify the response and the mocks.
484
- self .assertIs (snapshot , mock .sentinel .snapshot )
485
- client .get_all .assert_called_once_with (
486
- [document ], field_paths = None , transaction = None )
492
+ if use_transaction :
493
+ transaction = Transaction (client )
494
+ transaction_id = transaction ._id = b'asking-me-2'
495
+ else :
496
+ transaction = None
497
+
498
+ snapshot = document .get (
499
+ field_paths = field_paths , transaction = transaction )
500
+
501
+ self .assertIs (snapshot .reference , document )
502
+ if not_found :
503
+ self .assertIsNone (snapshot ._data )
504
+ self .assertFalse (snapshot .exists )
505
+ self .assertIsNone (snapshot .read_time )
506
+ self .assertIsNone (snapshot .create_time )
507
+ self .assertIsNone (snapshot .update_time )
508
+ else :
509
+ self .assertEqual (snapshot .to_dict (), {})
510
+ self .assertTrue (snapshot .exists )
511
+ self .assertIsNone (snapshot .read_time )
512
+ self .assertIs (snapshot .create_time , create_time )
513
+ self .assertIs (snapshot .update_time , update_time )
514
+
515
+ # Verify the request made to the API
516
+ if field_paths is not None :
517
+ mask = common_pb2 .DocumentMask (field_paths = sorted (field_paths ))
518
+ else :
519
+ mask = None
487
520
488
- def test_get_with_transaction (self ):
489
- from google .cloud .firestore_v1beta1 .client import Client
490
- from google .cloud .firestore_v1beta1 .transaction import Transaction
521
+ if use_transaction :
522
+ expected_transaction_id = transaction_id
523
+ else :
524
+ expected_transaction_id = None
491
525
492
- # Create a minimal fake client with a dummy response.
493
- response_iterator = iter ([mock .sentinel .snapshot ])
494
- client = mock .create_autospec (Client , instance = True )
495
- client .get_all .return_value = response_iterator
526
+ firestore_api .get_document .assert_called_once_with (
527
+ document ._document_path ,
528
+ mask = mask ,
529
+ transaction = expected_transaction_id ,
530
+ metadata = client ._rpc_metadata )
496
531
497
- # Actually make a document and call get().
498
- document = self ._make_one ('yellow' , 'mellow' , client = client )
499
- transaction = Transaction (client )
500
- transaction ._id = b'asking-me-2'
501
- snapshot = document .get (transaction = transaction )
532
+ def test_get_not_found (self ):
533
+ self ._get_helper (not_found = True )
502
534
503
- # Verify the response and the mocks.
504
- self .assertIs (snapshot , mock .sentinel .snapshot )
505
- client .get_all .assert_called_once_with (
506
- [document ], field_paths = None , transaction = transaction )
535
+ def test_get_default (self ):
536
+ self ._get_helper ()
507
537
508
- def test_get_not_found (self ):
509
- from google .cloud .firestore_v1beta1 .document import DocumentSnapshot
538
+ def test_get_w_string_field_path (self ):
539
+ with self .assertRaises (ValueError ):
540
+ self ._get_helper (field_paths = 'foo' )
510
541
511
- # Create a minimal fake client with a dummy response.
512
- read_time = 123
513
- expected = DocumentSnapshot (None , None , False , read_time , None , None )
514
- response_iterator = iter ([expected ])
515
- client = mock .Mock (
516
- _database_string = 'sprinklez' ,
517
- spec = ['_database_string' , 'get_all' ])
518
- client .get_all .return_value = response_iterator
519
-
520
- # Actually make a document and call get().
521
- document = self ._make_one ('house' , 'cowse' , client = client )
522
- field_paths = ['x.y' , 'x.z' , 't' ]
523
- snapshot = document .get (field_paths = field_paths )
524
- self .assertIsNone (snapshot .reference )
525
- self .assertIsNone (snapshot ._data )
526
- self .assertFalse (snapshot .exists )
527
- self .assertEqual (snapshot .read_time , expected .read_time )
528
- self .assertIsNone (snapshot .create_time )
529
- self .assertIsNone (snapshot .update_time )
542
+ def test_get_with_field_path (self ):
543
+ self ._get_helper (field_paths = ['foo' ])
530
544
531
- # Verify the response and the mocks.
532
- client .get_all .assert_called_once_with (
533
- [document ], field_paths = field_paths , transaction = None )
545
+ def test_get_with_multiple_field_paths (self ):
546
+ self ._get_helper (field_paths = ['foo' , 'bar.baz' ])
547
+
548
+ def test_get_with_transaction (self ):
549
+ self ._get_helper (use_transaction = True )
534
550
535
551
def _collections_helper (self , page_size = None ):
536
552
from google .api_core .page_iterator import Iterator
0 commit comments