Skip to content

Commit 6a4331e

Browse files
author
Frank Natividad
committed
Add base code for realtime listener snippets
1 parent 8f28cb6 commit 6a4331e

File tree

2 files changed

+130
-0
lines changed

2 files changed

+130
-0
lines changed

firestore/cloud-client/snippets.py

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,112 @@ def cursor_paginate():
603603
return next_query
604604

605605

606+
def listen_document():
607+
db = firestore.Client()
608+
# [START listen_document]
609+
610+
# Create a callback on_snapshot function to capture changes
611+
def on_snapshot(doc_snapshot):
612+
print(u'Received doc snapshot: {}'.format(doc_snapshot.to_dict()))
613+
614+
doc_ref = db.collection(u'cities').document(u'SF')
615+
616+
# Watch the document
617+
doc_watch = doc_ref.on_snapshot(on_snapshot)
618+
# [END listen_document]
619+
620+
print(u'Creating document')
621+
data = {
622+
u'name': u'San Francisco',
623+
u'state': u'CA',
624+
u'country': u'USA',
625+
u'capital': False,
626+
u'population': 860000
627+
}
628+
db.collection(u'cities').document(u'SF').set(data)
629+
630+
# [START detach_listener]
631+
# Terminate watch on a document
632+
doc_watch.unsubscribe()
633+
# [START detach_listener]
634+
635+
636+
def listen_multiple():
637+
db = firestore.Client()
638+
# [START listen_multiple]
639+
640+
# Create a callback on_snapshot function to capture changes
641+
def on_snapshot(col_snapshot):
642+
print(u'Callback received query snapshot.')
643+
print(u'Current cities in California: ')
644+
for doc in col_snapshot.documents:
645+
print(u'{} => {}'.format(doc.id))
646+
647+
col_query = db.collection(u'cities').where(u'state', u'==', u'CA')
648+
649+
# Watch the collection query
650+
query_watch = col_query.on_snapshot(on_snapshot)
651+
652+
# [END listen_multiple]
653+
mtv_document = db.collection(u'cities').document(u'MTV')
654+
print(u'Creating document')
655+
mtv_document.set({
656+
u'name': u'Mountain View',
657+
u'state': u'CA',
658+
u'country': u'USA',
659+
u'capital': False,
660+
u'population': 80000
661+
})
662+
663+
print(u'Modifying document')
664+
mtv_document.update({
665+
u'name': u'Mountain View',
666+
u'state': u'CA',
667+
u'country': u'USA',
668+
u'capital': False,
669+
u'population': 90000
670+
})
671+
672+
print(u'Delete document')
673+
mtv_document.delete()
674+
query_watch.unsubscribe()
675+
676+
677+
def listen_for_changes():
678+
db = firestore.Client()
679+
# [START listen_for_changes]
680+
681+
# Create a callback on_snapshot function to capture changes
682+
def on_snapshot(col_snapshot, changes):
683+
print(u'Callback received query snapshot.')
684+
print(u'Current cities in California: ')
685+
for change in changes:
686+
if change.type.name == "ADDED":
687+
print(u'New city: {}'.format(change.document.data()))
688+
elif change.type.name == "MODIFIED":
689+
print(u'Modified city: {}'.format(change.document.data()))
690+
elif change.type.name == "REMOVED":
691+
print(u'Removed city: {}'.format(change.document.data()))
692+
693+
col_query = db.collection(u'cities').where(u'state', u'==', u'CA')
694+
695+
# Watch the collection query
696+
query_watch = col_query.on_snapshot(on_snapshot)
697+
698+
# [END listen_for_changes]
699+
print(u'Creating document')
700+
data = {
701+
u'name': u'San Francisco',
702+
u'state': u'CA',
703+
u'country': u'USA',
704+
u'capital': False,
705+
u'population': 860000
706+
}
707+
db.collection(u'cities').document(u'SF').set(data)
708+
709+
query_watch.unsubscribe()
710+
711+
606712
def cursor_multiple_conditions():
607713
db = firestore.Client()
608714
# [START cursor_multiple_conditions]

firestore/cloud-client/snippets_test.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,5 +220,29 @@ def test_delete_field(db):
220220
snippets.delete_field()
221221

222222

223+
def test_listen_document(capsys):
224+
snippets.listen_document()
225+
out, _ = capsys.readouterr()
226+
assert "SF" in out
227+
assert "TOK" in out
228+
assert "BJ" in out
229+
230+
231+
def test_listen_multiple(capsys):
232+
snippets.listen_multiple()
233+
out, _ = capsys.readouterr()
234+
assert "SF" in out
235+
assert "TOK" in out
236+
assert "BJ" in out
237+
238+
239+
def test_listen_for_changes(capsys):
240+
snippets.listen_for_changes()
241+
out, _ = capsys.readouterr()
242+
assert "SF" in out
243+
assert "TOK" in out
244+
assert "BJ" in out
245+
246+
223247
def test_delete_full_collection():
224248
snippets.delete_full_collection()

0 commit comments

Comments
 (0)