Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
4 changes: 0 additions & 4 deletions firestore/google/cloud/firestore_v1/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -836,10 +836,6 @@ def _get_update_mask(self, allow_empty_mask=False):
for field_path in self.top_level_paths:
if field_path not in self.transform_paths:
mask_paths.append(field_path.to_api_repr())
else:
prefix = FieldPath(*field_path.parts[:-1])
if prefix.parts:
mask_paths.append(prefix.to_api_repr())

return common_pb2.DocumentMask(field_paths=mask_paths)

Expand Down
56 changes: 56 additions & 0 deletions firestore/tests/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,38 @@ def test_document_set_merge(client, cleanup):
assert snapshot2.update_time == write_result2.update_time


def test_document_set_merge_w_server_timestamp(client, cleanup):
document_id = "for-set" + unique_resource_id("-")
document = client.document("i-did-it", document_id)
# Add to clean-up before API request (in case ``set()`` fails).
cleanup(document)

# 0. Make sure the document doesn't exist yet
snapshot = document.get()
assert not snapshot.exists

# 1. Use ``create()`` to create the document.
data1 = {"name": "Sam", "address": {"city": "SF", "state": "CA"}}
write_result1 = document.create(data1)
snapshot1 = document.get()
assert snapshot1.update_time == write_result1.update_time

# 2. Call ``set()`` to merge
data2 = {"address": {"city": "LA"}, "now": firestore.SERVER_TIMESTAMP}
write_result2 = document.set(data2, merge=True)
snapshot2 = document.get()
stored_data = snapshot2.to_dict()
server_now = stored_data["now"]
assert stored_data == {
"name": "Sam",
"address": {"city": "LA", "state": "CA"},
"now": server_now,
}
# Make sure the create time hasn't changed.
assert snapshot2.create_time == snapshot1.create_time
assert snapshot2.update_time == write_result2.update_time


def test_document_set_w_int_field(client, cleanup):
document_id = "set-int-key" + unique_resource_id("-")
document = client.document("i-did-it", document_id)
Expand Down Expand Up @@ -333,6 +365,30 @@ def test_update_document(client, cleanup):
document.update({"bad": "time-future"}, option=option6)


def test_update_document_w_server_timestamp(client, cleanup):
document_id = "for-update" + unique_resource_id("-")
document = client.document("made", document_id)
# Add to clean-up before API request (in case ``create()`` fails).
cleanup(document)

# 1. Update and create the document (with an option).
data = {"foo": {"bar": "baz"}, "scoop": {"barn": 981}, "other": True}
write_result2 = document.create(data)

# 2. Send an update without a field path (no option).
field_updates3 = {"foo.now": firestore.SERVER_TIMESTAMP}
write_result3 = document.update(field_updates3)
assert_timestamp_less(write_result2.update_time, write_result3.update_time)
snapshot3 = document.get()
stored_data = snapshot3.to_dict()
expected3 = {
"foo": stored_data["foo"],
"scoop": data["scoop"],
"other": data["other"],
}
assert stored_data == expected3


def check_snapshot(snapshot, document, data, write_result):
assert snapshot.reference is document
assert snapshot.to_dict() == data
Expand Down
10 changes: 1 addition & 9 deletions firestore/tests/unit/v1/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1951,7 +1951,6 @@ def test_with_merge_true_w_transform(self):
document_data["butter"] = SERVER_TIMESTAMP

write_pbs = self._call_fut(document_path, document_data, merge=True)

update_pb = self._make_write_w_document(document_path, **update_data)
self._update_document_mask(update_pb, field_paths=sorted(update_data))
transform_pb = self._make_write_w_transform(document_path, fields=["butter"])
Expand Down Expand Up @@ -1987,7 +1986,6 @@ def test_with_merge_field_w_transform_masking_simple(self):
document_data["butter"] = {"pecan": SERVER_TIMESTAMP}

write_pbs = self._call_fut(document_path, document_data, merge=["butter.pecan"])

update_pb = self._make_write_w_document(document_path)
transform_pb = self._make_write_w_transform(
document_path, fields=["butter.pecan"]
Expand Down Expand Up @@ -2107,14 +2105,8 @@ def _helper(self, option=None, do_transform=False, **write_kwargs):
field_updates[field_path2] = SERVER_TIMESTAMP

write_pbs = self._call_fut(document_path, field_updates, option)

map_pb = document_pb2.MapValue(fields={"yum": _value_pb(bytes_value=value)})

if do_transform:
field_paths = [field_path1, "blog"]
else:
field_paths = [field_path1]

field_paths = [field_path1]
expected_update_pb = write_pb2.Write(
update=document_pb2.Document(
name=document_path, fields={"bitez": _value_pb(map_value=map_pb)}
Expand Down