Skip to content

Commit dd879de

Browse files
committed
add more tests
1 parent bd6fbb5 commit dd879de

File tree

4 files changed

+184
-28
lines changed

4 files changed

+184
-28
lines changed

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/FirestoreTest.java

Lines changed: 180 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,48 +1655,208 @@ public void sdkOrdersQueryByDocumentIdTheSameWayOnlineAndOffline() {
16551655
}
16561656

16571657
@Test
1658-
public void snapshotListenerSortsStringFieldsAsGetQuery() {
1658+
public void snapshotListenerSortsUnicodeStringsAsServer() {
16591659
Map<String, Map<String, Object>> testDocs =
1660-
map(
1661-
"a", map("value", "Łukasiewicz"),
1662-
"b", map("value", "Sierpiński"),
1663-
"c", map("value", "岩澤"),
1664-
"d", map("value", "🄟"),
1665-
"e", map("value", "P"),
1666-
"f", map("value", "︒"),
1667-
"g", map("value", "🐵")
1668-
);
1660+
map(
1661+
"a", map("value", "Łukasiewicz"),
1662+
"b", map("value", "Sierpiński"),
1663+
"c", map("value", "岩澤"),
1664+
"d", map("value", "🄟"),
1665+
"e", map("value", "P"),
1666+
"f", map("value", "︒"),
1667+
"g", map("value", "🐵"));
16691668

16701669
CollectionReference colRef = testCollectionWithDocs(testDocs);
1671-
// Test query
16721670
Query orderedQuery = colRef.orderBy("value");
1673-
List<String> expectedDocIds =
1674-
Arrays.asList(
1675-
"b", "a", "c", "f", "e", "d", "g");
1671+
List<String> expectedDocIds = Arrays.asList("b", "a", "c", "f", "e", "d", "g");
16761672

16771673
QuerySnapshot getSnapshot = waitFor(orderedQuery.get());
16781674
List<String> getSnapshotDocIds =
1679-
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
1675+
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
1676+
1677+
EventAccumulator<QuerySnapshot> eventAccumulator = new EventAccumulator<QuerySnapshot>();
1678+
ListenerRegistration registration =
1679+
orderedQuery.addSnapshotListener(eventAccumulator.listener());
1680+
1681+
List<String> watchSnapshotDocIds = new ArrayList<>();
1682+
try {
1683+
QuerySnapshot watchSnapshot = eventAccumulator.await();
1684+
watchSnapshotDocIds =
1685+
watchSnapshot.getDocuments().stream()
1686+
.map(documentSnapshot -> documentSnapshot.getId())
1687+
.collect(Collectors.toList());
1688+
} finally {
1689+
registration.remove();
1690+
}
1691+
1692+
// Assert that get and snapshot listener requests sort docs in the same, expected order
1693+
assertTrue(getSnapshotDocIds.equals(expectedDocIds));
1694+
assertTrue(watchSnapshotDocIds.equals(expectedDocIds));
1695+
1696+
checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0]));
1697+
}
1698+
1699+
public void snapshotListenerSortsUnicodeStringsInArrayFieldsAsServer() {
1700+
Map<String, Map<String, Object>> testDocs =
1701+
map(
1702+
"a", map("value", Arrays.asList("Łukasiewicz")),
1703+
"b", map("value", Arrays.asList("Sierpiński")),
1704+
"c", map("value", Arrays.asList("岩澤")),
1705+
"d", map("value", Arrays.asList("🄟")),
1706+
"e", map("value", Arrays.asList("P")),
1707+
"f", map("value", Arrays.asList("︒")),
1708+
"g", map("value", Arrays.asList("🐵")));
1709+
1710+
CollectionReference colRef = testCollectionWithDocs(testDocs);
1711+
Query orderedQuery = colRef.orderBy("value");
1712+
List<String> expectedDocIds = Arrays.asList("b", "a", "c", "f", "e", "d", "g");
1713+
1714+
QuerySnapshot getSnapshot = waitFor(orderedQuery.get());
1715+
List<String> getSnapshotDocIds =
1716+
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
16801717

1681-
// Run query with snapshot listener
16821718
EventAccumulator<QuerySnapshot> eventAccumulator = new EventAccumulator<QuerySnapshot>();
16831719
ListenerRegistration registration =
1684-
orderedQuery.addSnapshotListener(eventAccumulator.listener());
1720+
orderedQuery.addSnapshotListener(eventAccumulator.listener());
16851721

16861722
List<String> watchSnapshotDocIds = new ArrayList<>();
16871723
try {
16881724
QuerySnapshot watchSnapshot = eventAccumulator.await();
16891725
watchSnapshotDocIds =
1690-
watchSnapshot.getDocuments().stream()
1691-
.map(documentSnapshot -> documentSnapshot.getId())
1692-
.collect(Collectors.toList());
1726+
watchSnapshot.getDocuments().stream()
1727+
.map(documentSnapshot -> documentSnapshot.getId())
1728+
.collect(Collectors.toList());
16931729
} finally {
16941730
registration.remove();
16951731
}
16961732

16971733
// Assert that get and snapshot listener requests sort docs in the same, expected order
16981734
assertTrue(getSnapshotDocIds.equals(expectedDocIds));
16991735
assertTrue(watchSnapshotDocIds.equals(expectedDocIds));
1736+
1737+
checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0]));
17001738
}
17011739

1740+
public void snapshotListenerSortsUnicodeStringsInMapAsServer() {
1741+
Map<String, Map<String, Object>> testDocs =
1742+
map(
1743+
"a", map("value", map("foo", "Łukasiewicz")),
1744+
"b", map("value", map("foo", "Sierpiński")),
1745+
"c", map("value", map("foo", "岩澤")),
1746+
"d", map("value", map("foo", "🄟")),
1747+
"e", map("value", map("foo", "P")),
1748+
"f", map("value", map("foo", "︒")),
1749+
"g", map("value", map("foo", "🐵")));
1750+
1751+
CollectionReference colRef = testCollectionWithDocs(testDocs);
1752+
Query orderedQuery = colRef.orderBy("value");
1753+
List<String> expectedDocIds = Arrays.asList("b", "a", "c", "f", "e", "d", "g");
1754+
1755+
QuerySnapshot getSnapshot = waitFor(orderedQuery.get());
1756+
List<String> getSnapshotDocIds =
1757+
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
1758+
1759+
EventAccumulator<QuerySnapshot> eventAccumulator = new EventAccumulator<QuerySnapshot>();
1760+
ListenerRegistration registration =
1761+
orderedQuery.addSnapshotListener(eventAccumulator.listener());
1762+
1763+
List<String> watchSnapshotDocIds = new ArrayList<>();
1764+
try {
1765+
QuerySnapshot watchSnapshot = eventAccumulator.await();
1766+
watchSnapshotDocIds =
1767+
watchSnapshot.getDocuments().stream()
1768+
.map(documentSnapshot -> documentSnapshot.getId())
1769+
.collect(Collectors.toList());
1770+
} finally {
1771+
registration.remove();
1772+
}
1773+
1774+
// Assert that get and snapshot listener requests sort docs in the same, expected order
1775+
assertTrue(getSnapshotDocIds.equals(expectedDocIds));
1776+
assertTrue(watchSnapshotDocIds.equals(expectedDocIds));
1777+
1778+
checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0]));
1779+
}
1780+
1781+
public void snapshotListenerSortsUnicodeStringsInMapKeyAsServer() {
1782+
Map<String, Map<String, Object>> testDocs =
1783+
map(
1784+
"a", map("value", map("Łukasiewicz", "foo")),
1785+
"b", map("value", map("Sierpiński", "foo")),
1786+
"c", map("value", map("岩澤", "foo")),
1787+
"d", map("value", map("🄟", "foo")),
1788+
"e", map("value", map("P", "foo")),
1789+
"f", map("value", map("︒", "foo")),
1790+
"g", map("value", map("🐵", "foo")));
1791+
1792+
CollectionReference colRef = testCollectionWithDocs(testDocs);
1793+
Query orderedQuery = colRef.orderBy("value");
1794+
List<String> expectedDocIds = Arrays.asList("b", "a", "c", "f", "e", "d", "g");
1795+
1796+
QuerySnapshot getSnapshot = waitFor(orderedQuery.get());
1797+
List<String> getSnapshotDocIds =
1798+
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
1799+
1800+
EventAccumulator<QuerySnapshot> eventAccumulator = new EventAccumulator<QuerySnapshot>();
1801+
ListenerRegistration registration =
1802+
orderedQuery.addSnapshotListener(eventAccumulator.listener());
1803+
1804+
List<String> watchSnapshotDocIds = new ArrayList<>();
1805+
try {
1806+
QuerySnapshot watchSnapshot = eventAccumulator.await();
1807+
watchSnapshotDocIds =
1808+
watchSnapshot.getDocuments().stream()
1809+
.map(documentSnapshot -> documentSnapshot.getId())
1810+
.collect(Collectors.toList());
1811+
} finally {
1812+
registration.remove();
1813+
}
1814+
1815+
// Assert that get and snapshot listener requests sort docs in the same, expected order
1816+
assertTrue(getSnapshotDocIds.equals(expectedDocIds));
1817+
assertTrue(watchSnapshotDocIds.equals(expectedDocIds));
1818+
1819+
checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0]));
1820+
}
1821+
1822+
public void snapshotListenerSortsUnicodeStringsInDocumentKeyAsServer() {
1823+
Map<String, Map<String, Object>> testDocs =
1824+
map(
1825+
"Łukasiewicz", map("value", "foo"),
1826+
"Sierpiński", map("value", "foo"),
1827+
"岩澤", map("value", "foo"),
1828+
"🄟", map("value", "foo"),
1829+
"P", map("value", "foo"),
1830+
"︒", map("value", "foo"),
1831+
"🐵", map("value", "foo"));
1832+
1833+
CollectionReference colRef = testCollectionWithDocs(testDocs);
1834+
Query orderedQuery = colRef.orderBy(FieldPath.documentId());
1835+
List<String> expectedDocIds = Arrays.asList("b", "a", "c", "f", "e", "d", "g");
1836+
1837+
QuerySnapshot getSnapshot = waitFor(orderedQuery.get());
1838+
List<String> getSnapshotDocIds =
1839+
getSnapshot.getDocuments().stream().map(ds -> ds.getId()).collect(Collectors.toList());
1840+
1841+
EventAccumulator<QuerySnapshot> eventAccumulator = new EventAccumulator<QuerySnapshot>();
1842+
ListenerRegistration registration =
1843+
orderedQuery.addSnapshotListener(eventAccumulator.listener());
1844+
1845+
List<String> watchSnapshotDocIds = new ArrayList<>();
1846+
try {
1847+
QuerySnapshot watchSnapshot = eventAccumulator.await();
1848+
watchSnapshotDocIds =
1849+
watchSnapshot.getDocuments().stream()
1850+
.map(documentSnapshot -> documentSnapshot.getId())
1851+
.collect(Collectors.toList());
1852+
} finally {
1853+
registration.remove();
1854+
}
1855+
1856+
// Assert that get and snapshot listener requests sort docs in the same, expected order
1857+
assertTrue(getSnapshotDocIds.equals(expectedDocIds));
1858+
assertTrue(watchSnapshotDocIds.equals(expectedDocIds));
1859+
1860+
checkOnlineAndOfflineResultsMatch(orderedQuery, expectedDocIds.toArray(new String[0]));
1861+
}
17021862
}

firebase-firestore/src/main/java/com/google/firebase/firestore/model/BasePath.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ private static int compareSegments(String lhs, String rhs) {
114114
} else if (isLhsNumeric && isRhsNumeric) { // both numeric
115115
return Long.compare(extractNumericId(lhs), extractNumericId(rhs));
116116
} else { // both string
117-
return lhs.compareTo(rhs);
117+
return Util.compareUtf8Strings(lhs, rhs);
118118
}
119119
}
120120

firebase-firestore/src/main/java/com/google/firebase/firestore/model/Values.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ public static int compare(Value left, Value right) {
230230
case TYPE_ORDER_SERVER_TIMESTAMP:
231231
return compareTimestamps(getLocalWriteTime(left), getLocalWriteTime(right));
232232
case TYPE_ORDER_STRING:
233-
return Util.compareStrings(left.getStringValue(), right.getStringValue());
234-
// return left.getStringValue().compareTo(right.getStringValue());
233+
return Util.compareUtf8Strings(left.getStringValue(), right.getStringValue());
235234
case TYPE_ORDER_BLOB:
236235
return Util.compareByteStrings(left.getBytesValue(), right.getBytesValue());
237236
case TYPE_ORDER_REFERENCE:
@@ -350,7 +349,7 @@ private static int compareMaps(MapValue left, MapValue right) {
350349
while (iterator1.hasNext() && iterator2.hasNext()) {
351350
Map.Entry<String, Value> entry1 = iterator1.next();
352351
Map.Entry<String, Value> entry2 = iterator2.next();
353-
int keyCompare = entry1.getKey().compareTo(entry2.getKey());
352+
int keyCompare = Util.compareUtf8Strings(entry1.getKey(), entry2.getKey());
354353
if (keyCompare != 0) {
355354
return keyCompare;
356355
}

firebase-firestore/src/main/java/com/google/firebase/firestore/util/Util.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
import io.grpc.Status;
2828
import io.grpc.StatusException;
2929
import io.grpc.StatusRuntimeException;
30-
31-
import java.nio.charset.Charset;
3230
import java.security.SecureRandom;
3331
import java.util.ArrayList;
3432
import java.util.Collection;
@@ -87,10 +85,9 @@ public static int compareIntegers(int i1, int i2) {
8785
}
8886
}
8987

90-
public static int compareStrings(String left, String right) {
88+
public static int compareUtf8Strings(String left, String right) {
9189
ByteString leftBytes = ByteString.copyFromUtf8(left);
9290
ByteString rightBytes = ByteString.copyFromUtf8(right);
93-
// return left.getStringValue().compareTo(right.getStringValue());
9491
return compareByteStrings(leftBytes, rightBytes);
9592
}
9693

0 commit comments

Comments
 (0)