@@ -590,10 +590,28 @@ def test_probably_has_next_finished():
590590 @pytest .mark .usefixtures ("in_context" )
591591 @mock .patch ("google.cloud.ndb._datastore_query._datastore_run_query" )
592592 def test__next_batch (_datastore_run_query ):
593+ entity1 = mock .Mock (
594+ key = entity_pb2 .Key (
595+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
596+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 42 )],
597+ )
598+ )
599+ entity2 = mock .Mock (
600+ key = entity_pb2 .Key (
601+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
602+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 43 )],
603+ )
604+ )
605+ entity3 = mock .Mock (
606+ key = entity_pb2 .Key (
607+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
608+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 44 )],
609+ )
610+ )
593611 entity_results = [
594- mock .Mock (entity = " entity1" , cursor = b"a" ),
595- mock .Mock (entity = " entity2" , cursor = b"b" ),
596- mock .Mock (entity = " entity3" , cursor = b"c" ),
612+ mock .Mock (entity = entity1 , cursor = b"a" ),
613+ mock .Mock (entity = entity2 , cursor = b"b" ),
614+ mock .Mock (entity = entity3 , cursor = b"c" ),
597615 ]
598616 _datastore_run_query .return_value = utils .future_result (
599617 mock .Mock (
@@ -611,24 +629,91 @@ def test__next_batch(_datastore_run_query):
611629 assert iterator ._next_batch ().result () is None
612630 assert iterator ._index == 0
613631 assert len (iterator ._batch ) == 3
614- assert iterator ._batch [0 ].result_pb .entity == " entity1"
632+ assert iterator ._batch [0 ].result_pb .entity == entity1
615633 assert iterator ._batch [0 ].result_type == query_pb2 .EntityResult .FULL
616634 assert iterator ._batch [0 ].order_by is None
617635 assert not iterator ._has_next_batch
618636
637+ @staticmethod
638+ @mock .patch ("google.cloud.ndb._datastore_query._datastore_run_query" )
639+ def test__next_batch_cached_delete (_datastore_run_query , in_context ):
640+ entity1 = mock .Mock (
641+ key = entity_pb2 .Key (
642+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
643+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 42 )],
644+ )
645+ )
646+ entity2 = mock .Mock (
647+ key = entity_pb2 .Key (
648+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
649+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 43 )],
650+ )
651+ )
652+ entity3 = mock .Mock (
653+ key = entity_pb2 .Key (
654+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
655+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 44 )],
656+ )
657+ )
658+ entity_results = [
659+ mock .Mock (entity = entity1 , cursor = b"a" ),
660+ mock .Mock (entity = entity2 , cursor = b"b" ),
661+ mock .Mock (entity = entity3 , cursor = b"c" ),
662+ ]
663+ in_context .cache [key_module .Key ("ThisKind" , 43 )] = None
664+ _datastore_run_query .return_value = utils .future_result (
665+ mock .Mock (
666+ batch = mock .Mock (
667+ entity_result_type = query_pb2 .EntityResult .FULL ,
668+ entity_results = entity_results ,
669+ end_cursor = b"abc" ,
670+ more_results = query_pb2 .QueryResultBatch .NO_MORE_RESULTS ,
671+ )
672+ )
673+ )
674+
675+ query = query_module .QueryOptions ()
676+ iterator = _datastore_query ._QueryIteratorImpl (query )
677+ assert iterator ._next_batch ().result () is None
678+ assert iterator ._index == 0
679+ assert len (iterator ._batch ) == 2
680+ assert iterator ._batch [0 ].result_pb .entity == entity1
681+ assert iterator ._batch [0 ].result_type == query_pb2 .EntityResult .FULL
682+ assert iterator ._batch [0 ].order_by is None
683+ assert iterator ._batch [1 ].result_pb .entity == entity3
684+ assert not iterator ._has_next_batch
685+
619686 @staticmethod
620687 @pytest .mark .usefixtures ("in_context" )
621688 @mock .patch ("google.cloud.ndb._datastore_query._datastore_run_query" )
622689 def test__next_batch_has_more (_datastore_run_query ):
690+ entity1 = mock .Mock (
691+ key = entity_pb2 .Key (
692+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
693+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 42 )],
694+ )
695+ )
696+ entity2 = mock .Mock (
697+ key = entity_pb2 .Key (
698+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
699+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 43 )],
700+ )
701+ )
702+ entity3 = mock .Mock (
703+ key = entity_pb2 .Key (
704+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
705+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 44 )],
706+ )
707+ )
623708 entity_results = [
624- mock .Mock (entity = " entity1" , cursor = b"a" ),
625- mock .Mock (entity = " entity2" , cursor = b"b" ),
626- mock .Mock (entity = " entity3" , cursor = b"c" ),
709+ mock .Mock (entity = entity1 , cursor = b"a" ),
710+ mock .Mock (entity = entity2 , cursor = b"b" ),
711+ mock .Mock (entity = entity3 , cursor = b"c" ),
627712 ]
628713 _datastore_run_query .return_value = utils .future_result (
629714 mock .Mock (
630715 batch = mock .Mock (
631- entity_result_type = query_pb2 .EntityResult .FULL ,
716+ entity_result_type = query_pb2 .EntityResult .PROJECTION ,
632717 entity_results = entity_results ,
633718 end_cursor = b"abc" ,
634719 more_results = query_pb2 .QueryResultBatch .NOT_FINISHED ,
@@ -641,8 +726,8 @@ def test__next_batch_has_more(_datastore_run_query):
641726 assert iterator ._next_batch ().result () is None
642727 assert iterator ._index == 0
643728 assert len (iterator ._batch ) == 3
644- assert iterator ._batch [0 ].result_pb .entity == " entity1"
645- assert iterator ._batch [0 ].result_type == query_pb2 .EntityResult .FULL
729+ assert iterator ._batch [0 ].result_pb .entity == entity1
730+ assert iterator ._batch [0 ].result_type == query_pb2 .EntityResult .PROJECTION
646731 assert iterator ._batch [0 ].order_by is None
647732 assert iterator ._has_next_batch
648733 assert iterator ._query .start_cursor .cursor == b"abc"
@@ -655,10 +740,28 @@ def test__next_batch_has_more_w_offset_and_limit(_datastore_run_query):
655740
656741 https://github.com/googleapis/python-ndb/issues/236
657742 """
743+ entity1 = mock .Mock (
744+ key = entity_pb2 .Key (
745+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
746+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 42 )],
747+ )
748+ )
749+ entity2 = mock .Mock (
750+ key = entity_pb2 .Key (
751+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
752+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 43 )],
753+ )
754+ )
755+ entity3 = mock .Mock (
756+ key = entity_pb2 .Key (
757+ partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
758+ path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 44 )],
759+ )
760+ )
658761 entity_results = [
659- mock .Mock (entity = " entity1" , cursor = b"a" ),
660- mock .Mock (entity = " entity2" , cursor = b"b" ),
661- mock .Mock (entity = " entity3" , cursor = b"c" ),
762+ mock .Mock (entity = entity1 , cursor = b"a" ),
763+ mock .Mock (entity = entity2 , cursor = b"b" ),
764+ mock .Mock (entity = entity3 , cursor = b"c" ),
662765 ]
663766 _datastore_run_query .return_value = utils .future_result (
664767 mock .Mock (
@@ -677,7 +780,7 @@ def test__next_batch_has_more_w_offset_and_limit(_datastore_run_query):
677780 assert iterator ._next_batch ().result () is None
678781 assert iterator ._index == 0
679782 assert len (iterator ._batch ) == 3
680- assert iterator ._batch [0 ].result_pb .entity == " entity1"
783+ assert iterator ._batch [0 ].result_pb .entity == entity1
681784 assert iterator ._batch [0 ].result_type == query_pb2 .EntityResult .FULL
682785 assert iterator ._batch [0 ].order_by is None
683786 assert iterator ._has_next_batch
@@ -1466,15 +1569,16 @@ def test_entity_full_entity(model):
14661569 partition_id = entity_pb2 .PartitionId (project_id = "testing" ),
14671570 path = [entity_pb2 .Key .PathElement (kind = "ThisKind" , id = 42 )],
14681571 )
1469- entity = mock .Mock (key = key_pb )
1572+ entity_pb = mock .Mock (key = key_pb )
1573+ entity = mock .Mock (key = key_module .Key ("ThisKind" , 42 ))
14701574 model ._entity_from_protobuf .return_value = entity
14711575 result = _datastore_query ._Result (
14721576 _datastore_query .RESULT_TYPE_FULL ,
1473- mock .Mock (entity = entity , cursor = b"123" , spec = ("entity" , "cursor" )),
1577+ mock .Mock (entity = entity_pb , cursor = b"123" , spec = ("entity" , "cursor" )),
14741578 )
14751579
14761580 assert result .entity () is entity
1477- model ._entity_from_protobuf .assert_called_once_with (entity )
1581+ model ._entity_from_protobuf .assert_called_once_with (entity_pb )
14781582
14791583 @staticmethod
14801584 @pytest .mark .usefixtures ("in_context" )
0 commit comments