Skip to content

Commit 417c3d7

Browse files
authored
Merge be7e393 into 5cc1bf5
2 parents 5cc1bf5 + be7e393 commit 417c3d7

File tree

3 files changed

+96
-33
lines changed

3 files changed

+96
-33
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/bundle/BundleSerializer.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import com.google.firebase.firestore.core.Filter;
2323
import com.google.firebase.firestore.core.OrderBy;
2424
import com.google.firebase.firestore.core.Query;
25-
import com.google.firebase.firestore.core.Target;
2625
import com.google.firebase.firestore.model.DocumentKey;
2726
import com.google.firebase.firestore.model.FieldPath;
2827
import com.google.firebase.firestore.model.MutableDocument;
@@ -154,7 +153,20 @@ private BundledQuery decodeBundledQuery(JSONObject bundledQuery) throws JSONExce
154153
Query.LimitType limitType = decodeLimitType(bundledQuery);
155154

156155
return new BundledQuery(
157-
new Target(parent, collectionGroup, filters, orderBys, limit, startAt, endAt), limitType);
156+
new Query(
157+
parent,
158+
collectionGroup,
159+
filters,
160+
orderBys,
161+
limit,
162+
// Not using `limitType` because bundled queries are what the backend sees,
163+
// and there is no limit_to_last for the backend.
164+
// Limit type is applied when the query is read back instead.
165+
Query.LimitType.LIMIT_TO_FIRST,
166+
startAt,
167+
endAt)
168+
.toTarget(),
169+
limitType);
158170
}
159171

160172
private int decodeLimit(JSONObject structuredQuery) {

firebase-firestore/src/test/java/com/google/firebase/firestore/bundle/BundleReaderTest.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import static org.junit.Assert.assertThrows;
2525

2626
import com.google.firebase.firestore.core.Query;
27-
import com.google.firebase.firestore.core.Target;
2827
import com.google.firebase.firestore.model.DatabaseId;
2928
import com.google.firebase.firestore.model.ObjectValue;
3029
import com.google.firebase.firestore.model.ResourcePath;
@@ -57,28 +56,34 @@ public class BundleReaderTest {
5756
new NamedQuery(
5857
"limitQuery",
5958
new BundledQuery(
60-
new Target(
61-
ResourcePath.fromString("foo"),
62-
null,
63-
Collections.emptyList(),
64-
Collections.singletonList(orderBy("sort")),
65-
1,
66-
null,
67-
null),
59+
new Query(
60+
ResourcePath.fromString("foo"),
61+
null,
62+
Collections.emptyList(),
63+
Collections.singletonList(orderBy("sort")),
64+
1,
65+
Query.LimitType.LIMIT_TO_FIRST,
66+
null,
67+
null)
68+
.toTarget(),
6869
Query.LimitType.LIMIT_TO_FIRST),
6970
version(1590011379000001L));
7071
public static final NamedQuery LIMIT_TO_LAST_QUERY =
7172
new NamedQuery(
7273
"limitToLastQuery",
7374
new BundledQuery(
74-
new Target(
75-
ResourcePath.fromString("foo"),
76-
null,
77-
Collections.emptyList(),
78-
Collections.singletonList(orderBy("sort")),
79-
1,
80-
null,
81-
null),
75+
new Query(
76+
ResourcePath.fromString("foo"),
77+
null,
78+
Collections.emptyList(),
79+
Collections.singletonList(orderBy("sort")),
80+
1,
81+
// Note this is LIMIT_TO_FIRST because it is the expected
82+
// limit type in bundle files.
83+
Query.LimitType.LIMIT_TO_FIRST,
84+
null,
85+
null)
86+
.toTarget(),
8287
Query.LimitType.LIMIT_TO_LAST),
8388
version(1590011379000002L));
8489
public static final BundledDocumentMetadata DELETED_DOC_METADATA =

firebase-firestore/src/test/java/com/google/firebase/firestore/bundle/BundleSerializerTest.java

Lines changed: 60 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,10 @@
5151
@RunWith(RobolectricTestRunner.class)
5252
@Config(manifest = Config.NONE)
5353
public class BundleSerializerTest {
54-
// Note: This tests uses single-quoted JSON strings, which are accepted by org.json.JSONObject.
55-
// While they are invalid JSON, they allow us to no use non-escaped quotes in the test file.
54+
// Note: This tests uses single-quoted JSON strings, which are accepted by
55+
// org.json.JSONObject.
56+
// While they are invalid JSON, they allow us to no use non-escaped quotes in
57+
// the test file.
5658

5759
private static String TEST_PROJECT = "projects/project/databases/(default)/documents";
5860
private static String TEST_DOCUMENT = TEST_PROJECT + "/coll/doc";
@@ -568,9 +570,34 @@ public void testDecodesLimitQuery() throws JSONException {
568570

569571
@Test
570572
public void testDecodesLimitToLastQuery() throws JSONException {
571-
String json = "{ from: [ { collectionId: 'coll' } ], limit: {value: 5 } }";
572-
Query query = TestUtil.query("coll").limitToLast(5);
573-
assertDecodesNamedQuery(json, query);
573+
String queryJson =
574+
"{\n"
575+
+ " name: 'query-1',\n"
576+
+ " bundledQuery: {\n"
577+
+ " parent: '"
578+
+ TEST_PROJECT
579+
+ "',\n"
580+
+ " structuredQuery:\n"
581+
+ "{ from: [ { collectionId: 'coll' } ], limit: {value: 5 } }"
582+
+ ",\n"
583+
+ " limitType: 'LAST'\n"
584+
+ " },\n"
585+
+ " readTime: '2020-01-01T00:00:01.000000001Z'\n"
586+
+ "}";
587+
NamedQuery actualNamedQuery = serializer.decodeNamedQuery(new JSONObject(queryJson));
588+
589+
// Note we use limitToFirst instead of limitToLast to avoid order reverse.
590+
// Because this is what is saved in bundle files.
591+
Query query = TestUtil.query("coll").limitToFirst(5);
592+
Target target = query.toTarget();
593+
BundledQuery bundledQuery = new BundledQuery(target, Query.LimitType.LIMIT_TO_LAST);
594+
NamedQuery expectedNamedQuery =
595+
new NamedQuery(
596+
"query-1",
597+
bundledQuery,
598+
new SnapshotVersion(new com.google.firebase.Timestamp(1577836801, 1)));
599+
600+
assertEquals(expectedNamedQuery, actualNamedQuery);
574601
}
575602

576603
@Test
@@ -672,6 +699,33 @@ public void testDecodesBundledDocumentMetadata() throws JSONException {
672699
assertEquals(expectedMetadata, actualMetadata);
673700
}
674701

702+
@Test
703+
public void testDecodesTargetWithoutImplicitOrderByOnName() throws JSONException {
704+
String json =
705+
"{\"name\":\"myNamedQuery\",\"bundledQuery\":{\"parent\":\"projects/project/databases"
706+
+ "/(default)/documents\",\"structuredQuery\":{\"from\":[{\"collectionId\":\"foo\"}],"
707+
+ "\"limit\":{\"value\":10}},\"limitType\":\"FIRST\"},"
708+
+ "\"readTime\":{\"seconds\":\"1679674432\",\"nanos\":579934000}}";
709+
NamedQuery query = serializer.decodeNamedQuery(new JSONObject(json));
710+
assertEquals(
711+
TestUtil.query("foo").limitToFirst(10).toTarget(), query.getBundledQuery().getTarget());
712+
assertEquals(Query.LimitType.LIMIT_TO_FIRST, query.getBundledQuery().getLimitType());
713+
}
714+
715+
@Test
716+
public void testDecodesLimitToLastTargetWithoutImplicitOrderByOnName() throws JSONException {
717+
String json =
718+
"{\"name\":\"myNamedQuery\",\"bundledQuery\":{\"parent\":\"projects/project/databases"
719+
+ "/(default)/documents\",\"structuredQuery\":{\"from\":[{\"collectionId\":\"foo\"}],"
720+
+ "\"limit\":{\"value\":10}},\"limitType\":\"LAST\"},"
721+
+ "\"readTime\":{\"seconds\":\"1679674432\",\"nanos\":579934000}}";
722+
NamedQuery query = serializer.decodeNamedQuery(new JSONObject(json));
723+
assertEquals(
724+
// Note that `limitToFirst(10)` is expected.
725+
TestUtil.query("foo").limitToFirst(10).toTarget(), query.getBundledQuery().getTarget());
726+
assertEquals(Query.LimitType.LIMIT_TO_LAST, query.getBundledQuery().getLimitType());
727+
}
728+
675729
private void assertDecodesValue(String json, Value proto) throws JSONException {
676730
String documentJson =
677731
"{\n"
@@ -719,15 +773,7 @@ private void assertDecodesNamedQuery(String json, Query query) throws JSONExcept
719773
+ "}";
720774
NamedQuery actualNamedQuery = serializer.decodeNamedQuery(new JSONObject(queryJson));
721775

722-
Target target =
723-
new Target(
724-
query.getPath(),
725-
query.getCollectionGroup(),
726-
query.getFilters(),
727-
query.getExplicitOrderBy(),
728-
query.getLimit(),
729-
query.getStartAt(),
730-
query.getEndAt());
776+
Target target = query.toTarget();
731777
BundledQuery bundledQuery =
732778
new BundledQuery(
733779
target,

0 commit comments

Comments
 (0)