Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -472,12 +472,13 @@ ApiFuture<List<WriteResult>> commit(@Nullable ByteString transactionId) {
new ApiFunction<CommitResponse, List<WriteResult>>() {
@Override
public List<WriteResult> apply(CommitResponse commitResponse) {
List<com.google.firestore.v1beta1.WriteResult> protoWriteResultList =
List<com.google.firestore.v1beta1.WriteResult> writeResults =
commitResponse.getWriteResultsList();

List<WriteResult> writeResultList = new ArrayList<>();
for (com.google.firestore.v1beta1.WriteResult protoWriteResult : protoWriteResultList) {
writeResultList.add(WriteResult.fromProto(protoWriteResult));
for (com.google.firestore.v1beta1.WriteResult writeResult : writeResults) {
writeResultList.add(
WriteResult.fromProto(writeResult, commitResponse.getCommitTime()));
}

return writeResultList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public Instant getUpdateTime() {
return this.updateTime;
}

static WriteResult fromProto(com.google.firestore.v1beta1.WriteResult protoWriteResult) {
Timestamp timestamp = protoWriteResult.getUpdateTime();
static WriteResult fromProto(
com.google.firestore.v1beta1.WriteResult writeResult, Timestamp commitTime) {
Timestamp timestamp = writeResult.hasUpdateTime() ? writeResult.getUpdateTime() : commitTime;
Instant instant = Instant.ofEpochSecond(timestamp.getSeconds(), timestamp.getNanos());
return new WriteResult(instant);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static com.google.cloud.firestore.LocalFirestoreHelper.NESTED_CLASS_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.SERVER_TIMESTAMP_PROTO;
import static com.google.cloud.firestore.LocalFirestoreHelper.SERVER_TIMESTAMP_TRANSFORM;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_DELETE_COMMIT_RESPONSE;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_MAP;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_OBJECT;
import static com.google.cloud.firestore.LocalFirestoreHelper.SINGLE_FIELD_PROTO;
Expand Down Expand Up @@ -255,7 +256,7 @@ public void notFound() throws Exception {

@Test
public void deleteDocument() throws Exception {
doReturn(SINGLE_WRITE_COMMIT_RESPONSE)
doReturn(SINGLE_DELETE_COMMIT_RESPONSE)
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public final class LocalFirestoreHelper {
public static final Map<String, Value> ALL_SUPPORTED_TYPES_PROTO;

public static final CommitRequest SINGLE_CREATE_COMMIT_REQUEST;
public static final ApiFuture<CommitResponse> SINGLE_DELETE_COMMIT_RESPONSE;
public static final ApiFuture<CommitResponse> SINGLE_WRITE_COMMIT_RESPONSE;

public static final Date DATE;
Expand Down Expand Up @@ -196,12 +197,15 @@ public T answer(InvocationOnMock invocation) {
};
}

public static ApiFuture<CommitResponse> commitResponse(int count) {
public static ApiFuture<CommitResponse> commitResponse(int adds, int deletes) {
CommitResponse.Builder commitResponse = CommitResponse.newBuilder();
commitResponse.getCommitTimeBuilder().setSeconds(0).setNanos(0);
for (int i = 0; i < count; ++i) {
for (int i = 0; i < adds; ++i) {
commitResponse.addWriteResultsBuilder().getUpdateTimeBuilder().setSeconds(i).setNanos(i);
}
for (int i = 0; i < deletes; ++i) {
commitResponse.addWriteResultsBuilder();
}
return ApiFutures.immediateFuture(commitResponse.build());
}

Expand Down Expand Up @@ -628,7 +632,8 @@ public boolean equals(Object o) {
.build();
ALL_SUPPORTED_TYPES_OBJECT = new AllSupportedTypes();

SINGLE_WRITE_COMMIT_RESPONSE = commitResponse(1);
SINGLE_WRITE_COMMIT_RESPONSE = commitResponse(/* adds= */ 1, /* deletes= */ 0);
SINGLE_DELETE_COMMIT_RESPONSE = commitResponse(/* adds= */ 0, /* deletes= */ 1);
SINGLE_CREATE_COMMIT_REQUEST = commit(create(SINGLE_FIELD_PROTO));

NESTED_CLASS_OBJECT = new NestedClass();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public Thread newThread(Runnable runnable) {
@Test
public void returnsValue() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -133,7 +133,7 @@ public void canReturnNull() throws Exception {
doReturn(beginResponse())
.doReturn(ApiFutures.immediateFailedFuture(new Exception()))
.doReturn(beginResponse(ByteString.copyFromUtf8("foo2")))
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -269,7 +269,7 @@ public void limitsRetriesWithSuccess() throws Exception {
.doReturn(beginResponse(ByteString.copyFromUtf8("foo5")))
.doReturn(ApiFutures.immediateFailedFuture(new Exception()))
.doReturn(beginResponse(ByteString.copyFromUtf8("foo6")))
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -305,7 +305,7 @@ public String updateCallback(Transaction transaction) {
@Test
public void getDocument() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -340,7 +340,7 @@ public DocumentSnapshot updateCallback(Transaction transaction)
@Test
public void getQuery() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -375,7 +375,7 @@ public QuerySnapshot updateCallback(Transaction transaction)
@Test
public void updateDocument() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -414,7 +414,7 @@ public String updateCallback(Transaction transaction) {
@Test
public void setDocument() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -449,7 +449,7 @@ public String updateCallback(Transaction transaction) {
@Test
public void createDocument() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down Expand Up @@ -484,7 +484,7 @@ public String updateCallback(Transaction transaction) {
@Test
public void deleteDocument() throws Exception {
doReturn(beginResponse())
.doReturn(commitResponse(0))
.doReturn(commitResponse(0, 0))
.when(firestoreMock)
.sendRequest(requestCapture.capture(), Matchers.<UnaryCallable<Message, Message>>any());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void before() {

@Test
public void updateDocument() throws Exception {
doReturn(commitResponse(4))
doReturn(commitResponse(4, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
Expand All @@ -92,7 +92,7 @@ public void updateDocument() throws Exception {

@Test
public void setDocument() throws Exception {
doReturn(commitResponse(2))
doReturn(commitResponse(2, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
Expand Down Expand Up @@ -120,7 +120,7 @@ public void setDocument() throws Exception {

@Test
public void createDocument() throws Exception {
doReturn(commitResponse(2))
doReturn(commitResponse(2, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
Expand All @@ -143,7 +143,7 @@ public void createDocument() throws Exception {

@Test
public void deleteDocument() throws Exception {
doReturn(commitResponse(2))
doReturn(commitResponse(2, 0))
.when(firestoreMock)
.sendRequest(
commitCapture.capture(), Matchers.<UnaryCallable<CommitRequest, CommitResponse>>any());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,11 @@ public void deleteDocument() throws Exception {
} catch (ExecutionException e) {
assertTrue(e.getMessage().contains("FAILED_PRECONDITION"));
}
documentReference.delete(Precondition.updatedAt(writeResult.getUpdateTime())).get();
writeResult =
documentReference.delete(Precondition.updatedAt(writeResult.getUpdateTime())).get();
DocumentSnapshot documentSnapshot = documentReference.get().get();
assertFalse(documentSnapshot.exists());
assertTrue(writeResult.getUpdateTime().getEpochSecond() > 0);
}

@Test
Expand Down