Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Firestore: Fix sorting 'delete_changes' in 'Watch._compute_snapshot'. #8809

Merged
merged 4 commits into from
Aug 1, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion firestore/google/cloud/firestore_v1/watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ def modify_doc(new_document, updated_tree, updated_map):
key = functools.cmp_to_key(self._comparator)

# Deletes are sorted based on the order of the existing document.
delete_changes = sorted(delete_changes, key=key)
delete_changes = sorted(delete_changes)
for name in delete_changes:
change, updated_tree, updated_map = delete_doc(
name, updated_tree, updated_map
Expand Down
32 changes: 32 additions & 0 deletions firestore/tests/unit/v1/test_watch.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,38 @@ class DummyDoc(object):
doc_tree, doc_map, delete_changes, add_changes, update_changes
)
self.assertEqual(updated_map, doc_map) # no change

def test__compute_snapshot_deletes_w_real_comparator(self):
from google.cloud.firestore_v1.watch import WatchDocTree

doc_tree = WatchDocTree()

class DummyDoc(object):
update_time = mock.sentinel

def _comparator(doc1, doc2):
if doc1.field > doc2.field:
return 1
if doc1.field < doc2.field:
return -1
return 0
liamuk marked this conversation as resolved.
Show resolved Hide resolved

deleted_doc_1 = DummyDoc()
deleted_doc_2 = DummyDoc()
doc_tree = doc_tree.insert(deleted_doc_1, None)
doc_tree = doc_tree.insert(deleted_doc_2, None)
doc_map = {
"/deleted_1": deleted_doc_1,
"/deleted_2": deleted_doc_2,
}
liamuk marked this conversation as resolved.
Show resolved Hide resolved
delete_changes = ["/deleted_1", "/deleted_2"]
add_changes = []
update_changes = []
inst = self._makeOne(comparator=_comparator)
updated_tree, updated_map, applied_changes = inst._compute_snapshot(
doc_tree, doc_map, delete_changes, add_changes, update_changes
)
self.assertEqual(updated_map, {})

def test__reset_docs(self):
from google.cloud.firestore_v1.watch import ChangeType
Expand Down