@@ -227,14 +227,44 @@ def test_for_query(self):
227227 snapshot_callback = self ._snapshot_callback
228228 snapshot_class_instance = DummyDocumentSnapshot
229229 document_reference_class_instance = DummyDocumentReference
230+ client = DummyFirestore ()
231+ parent = DummyCollection (client )
230232 modulename = "google.cloud.firestore_v1.watch"
231233 pb2 = DummyPb2 ()
232234 with mock .patch ("%s.firestore_pb2" % modulename , pb2 ):
233235 with mock .patch ("%s.Watch.ResumableBidiRpc" % modulename , DummyRpc ):
234236 with mock .patch (
235237 "%s.Watch.BackgroundConsumer" % modulename , DummyBackgroundConsumer
236238 ):
237- query = DummyQuery ()
239+ query = DummyQuery (parent = parent )
240+ inst = Watch .for_query (
241+ query ,
242+ snapshot_callback ,
243+ snapshot_class_instance ,
244+ document_reference_class_instance ,
245+ )
246+ self .assertTrue (inst ._consumer .started )
247+ self .assertTrue (inst ._rpc .callbacks , [inst ._on_rpc_done ])
248+ self .assertEqual (inst ._targets ["query" ], "dummy query target" )
249+
250+ def test_for_query_nested (self ):
251+ from google .cloud .firestore_v1 .watch import Watch
252+
253+ snapshot_callback = self ._snapshot_callback
254+ snapshot_class_instance = DummyDocumentSnapshot
255+ document_reference_class_instance = DummyDocumentReference
256+ client = DummyFirestore ()
257+ root = DummyCollection (client )
258+ grandparent = DummyDocument ("document" , parent = root )
259+ parent = DummyCollection (client , parent = grandparent )
260+ modulename = "google.cloud.firestore_v1.watch"
261+ pb2 = DummyPb2 ()
262+ with mock .patch ("%s.firestore_pb2" % modulename , pb2 ):
263+ with mock .patch ("%s.Watch.ResumableBidiRpc" % modulename , DummyRpc ):
264+ with mock .patch (
265+ "%s.Watch.BackgroundConsumer" % modulename , DummyBackgroundConsumer
266+ ):
267+ query = DummyQuery (parent = parent )
238268 inst = Watch .for_query (
239269 query ,
240270 snapshot_callback ,
@@ -693,18 +723,41 @@ def __init__(self, *document_path, **kw):
693723 self .__dict__ .update (kw )
694724
695725
696- class DummyQuery (object ): # pragma: NO COVER
697- def __init__ (self , ** kw ):
698- if "client" not in kw :
699- self ._client = DummyFirestore ()
700- else :
701- self ._client = kw ["client" ]
726+ class DummyDocument (object ):
727+ def __init__ (self , name , parent ):
728+ self ._name = name
729+ self ._parent = parent
702730
703- if "comparator" not in kw :
704- # don't really do the comparison, just return 0 (equal) for all
705- self ._comparator = lambda x , y : 1
706- else :
707- self ._comparator = kw ["comparator" ]
731+ @property
732+ def _document_path (self ):
733+ return "{}/documents/{}" .format (
734+ self ._parent ._client ._database_string , self ._name
735+ )
736+
737+
738+ class DummyCollection (object ):
739+ def __init__ (self , client , parent = None ):
740+ self ._client = client
741+ self ._parent = parent
742+
743+ def _parent_info (self ):
744+ if self ._parent is None :
745+ return "{}/documents" .format (self ._client ._database_string ), None
746+ return self ._parent ._document_path , None
747+
748+
749+ def _compare (x , y ): # pragma: NO COVER
750+ return 1
751+
752+
753+ class DummyQuery (object ):
754+ def __init__ (self , parent ):
755+ self ._comparator = _compare
756+ self ._parent = parent
757+
758+ @property
759+ def _client (self ):
760+ return self ._parent ._client
708761
709762 def _to_protobuf (self ):
710763 return ""
0 commit comments