Skip to content

Commit 96de3e3

Browse files
committed
add test
1 parent 5c62282 commit 96de3e3

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

google-cloud-firestore/src/main/java/com/google/cloud/firestore/Order.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,13 +135,19 @@ public int compare(@Nonnull Value left, @Nonnull Value right) {
135135
}
136136

137137
private int compareStrings(Value left, Value right) {
138-
return left.getStringValue().compareTo(right.getStringValue());
138+
ByteString leftBytes = ByteString.copyFromUtf8(left.getStringValue());
139+
ByteString rightBytes = ByteString.copyFromUtf8(right.getStringValue());
140+
// return left.getStringValue().compareTo(right.getStringValue());
141+
return compareBytes(leftBytes, rightBytes);
139142
}
140143

141144
private int compareBlobs(Value left, Value right) {
142145
ByteString leftBytes = left.getBytesValue();
143146
ByteString rightBytes = right.getBytesValue();
147+
return compareBytes(leftBytes, rightBytes);
148+
}
144149

150+
private int compareBytes(ByteString leftBytes, ByteString rightBytes){
145151
int size = Math.min(leftBytes.size(), rightBytes.size());
146152
for (int i = 0; i < size; i++) {
147153
// Make sure the bytes are unsigned

google-cloud-firestore/src/test/java/com/google/cloud/firestore/it/ITQueryTest.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import java.util.LinkedHashMap;
3535
import java.util.List;
3636
import java.util.Map;
37+
import java.util.concurrent.CountDownLatch;
38+
import java.util.ArrayList;
3739
import java.util.concurrent.ExecutionException;
3840
import java.util.concurrent.TimeUnit;
3941
import java.util.concurrent.TimeoutException;
@@ -1113,4 +1115,45 @@ public void testAggregateQueryProfile() throws Exception {
11131115
assertThat(stats.getResultsReturned()).isEqualTo(1);
11141116
assertThat(stats.getExecutionDuration()).isGreaterThan(Duration.ZERO);
11151117
}
1118+
1119+
@Test
1120+
public void snapshotListenerSortsUnicodesSameWayAsServer() throws Exception {
1121+
CollectionReference col = createEmptyCollection();
1122+
1123+
firestore
1124+
.batch()
1125+
.set(col.document("a"), map("value", "Łukasiewicz"))
1126+
.set(col.document("b"), map("value", "Sierpiński"))
1127+
.set(col.document("c"), map("value", "岩澤"))
1128+
.set(col.document("d"), map("value", "🄟"))
1129+
.set(col.document("e"), map("value", "P"))
1130+
.set(col.document("f"), map("value", "︒"))
1131+
.set(col.document("g"), map("value", "🐵"))
1132+
1133+
.commit()
1134+
.get();
1135+
1136+
Query query = col.orderBy("value", Direction.ASCENDING);
1137+
1138+
QuerySnapshot snapshot = query.get().get();
1139+
List<String> queryOrder =
1140+
snapshot.getDocuments().stream().map(doc -> doc.getId()).collect(Collectors.toList());
1141+
1142+
CountDownLatch latch = new CountDownLatch(1);
1143+
List<String> listenerOrder = new ArrayList<>();
1144+
ListenerRegistration registration =
1145+
query.addSnapshotListener(
1146+
(value, error) -> {
1147+
listenerOrder.addAll(
1148+
value.getDocuments().stream()
1149+
.map(doc -> doc.getId())
1150+
.collect(Collectors.toList()));
1151+
latch.countDown();
1152+
});
1153+
latch.await();
1154+
registration.remove();
1155+
1156+
assertEquals(queryOrder, Arrays.asList("b", "a", "c", "f", "e", "d", "g"));
1157+
assertEquals(queryOrder, listenerOrder); // Assert order in the SDK
1158+
}
11161159
}

0 commit comments

Comments
 (0)